CodeStyle » History » Version 9
Alex Afanasyev, 02/17/2014 11:57 AM
1 | 1 | Junxiao Shi | # NFD code style guidelines |
---|---|---|---|
2 | |||
3 | NFD adopts [NDN Platform C++, C, C#, Java and JavaScript Code Guidelines](http://named-data.net/codebase/platform/documentation/ndn-platform-development-guidelines/cpp-code-guidelines/), with the following exceptions: |
||
4 | |||
5 | 8 | Junxiao Shi | * (amended 10) |
6 | Global variables should have `g_` prefix |
||
7 | 1 | Junxiao Shi | |
8 | 8 | Junxiao Shi | * (amended 11) |
9 | **All** class variables should have `m_` prefix. |
||
10 | Static class variables should have `s_` prefix. |
||
11 | |||
12 | * (amended 31) |
||
13 | Exceptions can be used in the code, but should be used only in **exceptional** cases and not in the primary processing path. |
||
14 | |||
15 | Exceptions can be suffixed with either `Exception` (eg. SecurityException) or `Error` (eg. SecurityError). Alternatively (and it is a recommended method), one should declare exception class `Exception` or `Error` as an inner class, from which the exception is thrown. For example, when declaring class Foo that can throw errors, one can write the following: |
||
16 | |||
17 | 5 | Alex Afanasyev | #include <stdexcept> |
18 | 1 | Junxiao Shi | |
19 | class Foo |
||
20 | 5 | Alex Afanasyev | { |
21 | 1 | Junxiao Shi | struct Error : std::runtime_exception |
22 | { |
||
23 | Error(const std::string& what) : std::runtime_error(what) {} |
||
24 | }; |
||
25 | }; |
||
26 | |||
27 | 8 | Junxiao Shi | In addition to that, if class Foo is a base class or interface for some class hierarchy, then child classes should should define their own `Error` or `Exception` classes that are inherited from the parent's Error class. |
28 | 1 | Junxiao Shi | |
29 | 8 | Junxiao Shi | * (amended 33) |
30 | We will use only `.cpp` and `.hpp` extensions |
||
31 | 1 | Junxiao Shi | |
32 | 8 | Junxiao Shi | * (removed 35) |
33 | Lines should be within a reasonable range. >100 column-lines should be generally avoided. |
||
34 | |||
35 | * (removed 44) |
||
36 | Implicit conversion is generally allowed. |
||
37 | |||
38 | Implicit conversion between integer and floating point numbers can cause problems and should be avoided. |
||
39 | |||
40 | Implicit conversion in single-argument constructor is usually undesirable. Therefore, all single-argument constructors should be marked 'explicit', unless implicit conversion is desirable. In that case, a comment should document the reason. |
||
41 | |||
42 | Avoid C-style casts. Use `static_cast`, `dynamic_cast`, `reinterpret_cast`, `const_cast` instead where appropriate. |
||
43 | |||
44 | * (amended 68) |
||
45 | All three presented styles ARE acceptable. First and third ARE recommended (these are actually GNU styles). |
||
46 | |||
47 | * (amended 69) |
||
48 | The class declarations should have the following form: |
||
49 | |||
50 | 3 | Alex Afanasyev | class SomeClass : public BaseClass |
51 | 4 | Junxiao Shi | { |
52 | 3 | Alex Afanasyev | public: |
53 | 4 | Junxiao Shi | ... <public methods> ... |
54 | 3 | Alex Afanasyev | protected: |
55 | ... <protected methods> ... |
||
56 | private: |
||
57 | 1 | Junxiao Shi | ... <private methods> ... |
58 | |||
59 | public: |
||
60 | ... <public data> ... |
||
61 | 3 | Alex Afanasyev | protected: |
62 | ... <protected data> ... |
||
63 | private: |
||
64 | ... <private data> ... |
||
65 | }; |
||
66 | |||
67 | ``public``, ``protected``, ``private`` may be repeated several times without interleaving (e.g. public, public, public, private, private) if this allows better readability of the code. |
||
68 | |||
69 | 8 | Junxiao Shi | * (amended 70) |
70 | When declaring/defining function/method, the return type should be put on a separate line before function/method name. |
||
71 | |||
72 | Method and function definitions should have the following form: |
||
73 | 3 | Alex Afanasyev | |
74 | void |
||
75 | someMethod() |
||
76 | { |
||
77 | ... |
||
78 | 1 | Junxiao Shi | } |
79 | 9 | Alex Afanasyev | |
80 | * (amended 76) No space requirement before : in switch statements |
||
81 | |||
82 | switch (condition) { |
||
83 | case ABC: |
||
84 | statements; |
||
85 | // Fallthrough |
||
86 | |||
87 | case DEF: |
||
88 | statements; |
||
89 | break; |
||
90 | |||
91 | case XYZ: |
||
92 | statements; |
||
93 | break; |
||
94 | |||
95 | default: |
||
96 | statements; |
||
97 | break; |
||
98 | } |