Commit 27fc43fb1fd35110352c5e3c6591a1748003f081
1 parent
4d7e909a
Add a bunch of C++ conventions from GammaLib.
Showing
2 changed files
with
188 additions
and
2 deletions
doc/source/coding/cpp.rst
| @@ -16,6 +16,48 @@ ctools would make it impossible to use the code on many systems. | @@ -16,6 +16,48 @@ ctools would make it impossible to use the code on many systems. | ||
| 16 | Code lay-out | 16 | Code lay-out |
| 17 | ^^^^^^^^^^^^ | 17 | ^^^^^^^^^^^^ |
| 18 | 18 | ||
| 19 | +- Each function or method starts with a curly bracket in the line following | ||
| 20 | + the function or method name. The return type is in the same line as the | ||
| 21 | + function or method name, e.g.: | ||
| 22 | + | ||
| 23 | + .. code-block:: cpp | ||
| 24 | + | ||
| 25 | + int function(void) | ||
| 26 | + { | ||
| 27 | + int i = 0; | ||
| 28 | + ... | ||
| 29 | + return i; | ||
| 30 | + } | ||
| 31 | + | ||
| 32 | +- Code blocks should be encompassed in curly brackets, even if the block | ||
| 33 | + consists only of a single line. | ||
| 34 | + | ||
| 35 | +- The opening curly bracket of a block starts in the same line as the | ||
| 36 | + related statement, e.g.: | ||
| 37 | + | ||
| 38 | + .. code-block:: cpp | ||
| 39 | + | ||
| 40 | + for (int i = 0; i < 10; ++i) { | ||
| 41 | + sum += i; | ||
| 42 | + } | ||
| 43 | + | ||
| 44 | +- Separate code elements by spaces, e.g.: | ||
| 45 | + | ||
| 46 | + .. code-block:: cpp | ||
| 47 | + | ||
| 48 | + Yes: int i = 0; | ||
| 49 | + | ||
| 50 | + No: int i=0; | ||
| 51 | + | ||
| 52 | +- Align successive class definition on the member function name, e.g.: | ||
| 53 | + | ||
| 54 | + .. code-block:: cpp | ||
| 55 | + | ||
| 56 | + void log10GeV(const double& eng); | ||
| 57 | + void log10TeV(const double& eng); | ||
| 58 | + std::string print(void) const; | ||
| 59 | + | ||
| 60 | + | ||
| 19 | String quotes | 61 | String quotes |
| 20 | ^^^^^^^^^^^^^ | 62 | ^^^^^^^^^^^^^ |
| 21 | 63 | ||
| @@ -26,8 +68,152 @@ Comments | @@ -26,8 +68,152 @@ Comments | ||
| 26 | ^^^^^^^^ | 68 | ^^^^^^^^ |
| 27 | 69 | ||
| 28 | Naming Conventions | 70 | Naming Conventions |
| 29 | -^^^^^^^^^^^^^^^^^^ | 71 | +^^^^^^^^^^^^^^^^^^ |
| 72 | + | ||
| 73 | +- Prefix data members with ``m_``, e.g.: | ||
| 74 | + | ||
| 75 | + .. code-block:: cpp | ||
| 76 | + | ||
| 77 | + m_num | ||
| 78 | + m_response | ||
| 79 | + m_grid_length | ||
| 80 | + m_axis_dir_qual | ||
| 81 | + | ||
| 30 | 82 | ||
| 31 | Programming Recommendations | 83 | Programming Recommendations |
| 32 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ | 84 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 33 | 85 | ||
| 86 | +- Each class shall have the protected methods ``init_members``, | ||
| 87 | + ``copy_members``, and ``free_members`` to handle initialisation, | ||
| 88 | + copying, and eventually freeing of allocated memory for data members: | ||
| 89 | + | ||
| 90 | + .. code-block:: cpp | ||
| 91 | + | ||
| 92 | + // Initialise data members | ||
| 93 | + void Class::init_members(void) | ||
| 94 | + { | ||
| 95 | + m_elements = 10; | ||
| 96 | + m_array = new double[m_elements]; | ||
| 97 | + for (int i = 0; i < m_elements; ++i) { | ||
| 98 | + m_array[i] = 0.0; | ||
| 99 | + } | ||
| 100 | + ... | ||
| 101 | + } | ||
| 102 | + | ||
| 103 | + // Copy data members | ||
| 104 | + void Class::copy_members(const &Class class) | ||
| 105 | + { | ||
| 106 | + m_elements = class.m_elements; | ||
| 107 | + m_array = new double[m_elements]; | ||
| 108 | + for (int i = 0; i < m_elements; ++i) { | ||
| 109 | + m_array[i] = class.m_array[i]; | ||
| 110 | + } | ||
| 111 | + ... | ||
| 112 | + } | ||
| 113 | + | ||
| 114 | + // Free data members | ||
| 115 | + void Class::free_members(void) | ||
| 116 | + { | ||
| 117 | + if (m_array != NULL) delete [] m_array; | ||
| 118 | + ... | ||
| 119 | + } | ||
| 120 | + | ||
| 121 | +- Each class shall have at least a void constructor, a copy constructor, | ||
| 122 | + a destructor and an assignment operator. Additional constructors and | ||
| 123 | + operators can be implemented as required. The following example shows | ||
| 124 | + the basic implementation for these 4 methods. Due to the usage of the | ||
| 125 | + ``init_members``, ``copy_members``, and ``free_members``, most classes | ||
| 126 | + will have exactly this kind of syntax: | ||
| 127 | + | ||
| 128 | + .. code-block:: cpp | ||
| 129 | + | ||
| 130 | + // Void constructor | ||
| 131 | + ctnice::ctnice(void) | ||
| 132 | + { | ||
| 133 | + init_members(); | ||
| 134 | + return; | ||
| 135 | + } | ||
| 136 | + | ||
| 137 | + // Copy constructor | ||
| 138 | + ctnice::ctnice(const ctnice& nice) | ||
| 139 | + { | ||
| 140 | + init_members(); | ||
| 141 | + copy_members(nice); | ||
| 142 | + return; | ||
| 143 | + } | ||
| 144 | + | ||
| 145 | + // Destructor | ||
| 146 | + ctnice::~ctnice(void) | ||
| 147 | + { | ||
| 148 | + free_members(); | ||
| 149 | + return; | ||
| 150 | + } | ||
| 151 | + | ||
| 152 | + // Assignment operator | ||
| 153 | + ctnice& ctnice::operator=(const ctnice& nice) | ||
| 154 | + { | ||
| 155 | + if (this != &nice) { | ||
| 156 | + free_members(); | ||
| 157 | + init_members(); | ||
| 158 | + copy_members(nice); | ||
| 159 | + } | ||
| 160 | + return *this; | ||
| 161 | + } | ||
| 162 | + | ||
| 163 | +- Do not use macros. | ||
| 164 | + | ||
| 165 | +- Do not use ``#define`` directives for the declaration of constants. Use | ||
| 166 | + ``const`` instead. | ||
| 167 | + | ||
| 168 | +- Do not use ``std::strncpy``, ``std::memcpy`` or similar as these functions | ||
| 169 | + are corrupted on some systems. | ||
| 170 | + | ||
| 171 | +- If possible, pass arguments by reference. | ||
| 172 | + | ||
| 173 | +- Output arguments should be passed as pointers. | ||
| 174 | + | ||
| 175 | +- Use C++ (``std::string``) instead of C-style (``char*``) strings. | ||
| 176 | + | ||
| 177 | +- Use C++ casts instead of C-style casts. | ||
| 178 | + | ||
| 179 | +- Avoid using templates. | ||
| 180 | + | ||
| 181 | +- Do not use an integer for a floating point argument (i.e. write 10.0 | ||
| 182 | + instead of 10). Some older compilers give an error when using | ||
| 183 | + integers in some floating point functions, such as log10(). | ||
| 184 | + | ||
| 185 | +- Where possible (and appropriate), use ``std::vector`` containers instead | ||
| 186 | + of allocating memory. In other words: avoid direct memory allocation with | ||
| 187 | + ``new``. | ||
| 188 | + | ||
| 189 | +- Use the ``std::`` namespace prefix where possible; write for example | ||
| 190 | + | ||
| 191 | + .. code-block:: cpp | ||
| 192 | + | ||
| 193 | + std::sin(angle); | ||
| 194 | + std::cos(angle); | ||
| 195 | + | ||
| 196 | + You may not believe it, but droping the ``std::`` may on some systems | ||
| 197 | + lead to considerably slower code for trigonometric functions! | ||
| 198 | + | ||
| 199 | +- Use ``explicit`` for constructors with single arguments to prevent | ||
| 200 | + unintended type conversions. The only exception to this rule is the | ||
| 201 | + copy constructor or type conversion constructors. | ||
| 202 | + | ||
| 203 | +- Specify ``void`` for function or method definitions without arguments, | ||
| 204 | + e.g.: | ||
| 205 | + | ||
| 206 | + .. code-block:: cpp | ||
| 207 | + | ||
| 208 | + Yes: void function(void) | ||
| 209 | + | ||
| 210 | + No: void function() | ||
| 211 | + | ||
| 212 | +- Use pre-incrementation in loops (pre-incrementation is faster than | ||
| 213 | + post-incrementation), e.g.: | ||
| 214 | + | ||
| 215 | + .. code-block:: cpp | ||
| 216 | + | ||
| 217 | + for (int i = 0; i < 10; ++i) { | ||
| 218 | + sum += i; | ||
| 219 | + } |
doc/source/coding/general.rst
| @@ -27,7 +27,7 @@ Code lay-out | @@ -27,7 +27,7 @@ Code lay-out | ||
| 27 | sum += par.sum | 27 | sum += par.sum |
| 28 | 28 | ||
| 29 | - Each function or method must terminate with a single return statement | 29 | - Each function or method must terminate with a single return statement |
| 30 | - that is placed at the end. | 30 | + that is placed at the end of the function or method. |
| 31 | 31 | ||
| 32 | - Always use English words (class names, method and function names, | 32 | - Always use English words (class names, method and function names, |
| 33 | comments, documentation, etc.). | 33 | comments, documentation, etc.). |