Project

General

Profile

CodeStyle » History » Version 10

Alex Afanasyev, 02/20/2014 09:53 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 10 Alex Afanasyev
*   (amended 8) Names representing generic template types should be a single uppercase letter
6
7
        template<class T> ...
8
        template<class C, class D> ...
9
10
    However, when template parameter represents a certain concept and expected to have a certain interface, the name should be explicitly spelled out:
11
12
        template<class FaceBase> ...
13
        template<class Packet> ...
14
15 8 Junxiao Shi
*   (amended 10)
16
    Global variables should have `g_` prefix
17 1 Junxiao Shi
18 8 Junxiao Shi
*   (amended 11)
19
    **All** class variables should have `m_` prefix.
20
    Static class variables should have `s_` prefix.
21
22
*   (amended 31)
23
    Exceptions can be used in the code, but should be used only in **exceptional** cases and not in the primary processing path.
24
25
    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:
26
27 5 Alex Afanasyev
        #include <stdexcept>
28 1 Junxiao Shi
        
29
        class Foo
30 5 Alex Afanasyev
        {
31 1 Junxiao Shi
            struct Error : std::runtime_exception
32
            {
33
                Error(const std::string& what) : std::runtime_error(what) {}
34
            };
35
        };
36
37 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.
38 1 Junxiao Shi
39 8 Junxiao Shi
*   (amended 33)
40
    We will use only `.cpp` and `.hpp` extensions
41 1 Junxiao Shi
42 8 Junxiao Shi
*   (removed 35)
43
    Lines should be within a reasonable range. >100 column-lines should be generally avoided.
44
45
*   (removed 44)
46
    Implicit conversion is generally allowed.
47
48
    Implicit conversion between integer and floating point numbers can cause problems and should be avoided.
49
50
    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.
51
52
    Avoid C-style casts.  Use `static_cast`, `dynamic_cast`, `reinterpret_cast`, `const_cast` instead where appropriate.
53
54
*   (amended 68)
55
    All three presented styles ARE acceptable.  First and third ARE recommended (these are actually GNU styles).
56
57
*   (amended 69)
58
    The class declarations should have the following form:
59
60 3 Alex Afanasyev
        class SomeClass : public BaseClass 
61 4 Junxiao Shi
        { 
62 3 Alex Afanasyev
        public: 
63 4 Junxiao Shi
          ... <public methods> ...
64 3 Alex Afanasyev
        protected: 
65
          ... <protected methods> ...
66
        private: 
67 1 Junxiao Shi
          ... <private methods> ...
68
        
69
        public: 
70
          ... <public data> ...
71 3 Alex Afanasyev
        protected: 
72
          ... <protected data> ...
73
        private: 
74
          ... <private data> ...
75
        };
76
77
    ``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.
78
79 8 Junxiao Shi
*   (amended 70)
80
    When declaring/defining function/method, the return type should be put on a separate line before function/method name.
81
82
    Method and function definitions should have the following form:
83 3 Alex Afanasyev
84
        void
85
        someMethod() 
86
        { 
87
          ... 
88 1 Junxiao Shi
        }
89 9 Alex Afanasyev
90
*   (amended 76) No space requirement before : in switch statements
91
92
        switch (condition) { 
93
          case ABC:
94
            statements; 
95
            // Fallthrough 
96
        
97
          case DEF: 
98
            statements; 
99
            break;
100
        
101
          case XYZ: 
102
            statements; 
103
            break; 
104
        
105
          default: 
106
            statements; 
107
            break;
108
        }