Alexander Afanasyev | 766cea7 | 2014-04-24 19:16:42 -0700 | [diff] [blame] | 1 | ndn-cxx Code Style and Coding Guidelines |
| 2 | ======================================== |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 3 | |
| 4 | Based on |
| 5 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 6 | * "C++ Programming Style Guidelines" by Geotechnical Software Services, Copyright © 1996-2011. |
| 7 | The original document is available at `<http://geosoft.no/development/cppstyle.html>`__ |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 8 | |
Davide Pesavento | 0b2aa13 | 2020-12-11 23:00:02 -0500 | [diff] [blame] | 9 | * NDN Platform "C++, C, C#, Java and JavaScript Code Guidelines". |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 10 | The original document is available at `<https://named-data.net/codebase/platform/documentation/ndn-platform-development-guidelines/cpp-code-guidelines/>`__ |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 11 | |
Junxiao Shi | 28af1dc | 2014-11-06 15:53:32 -0700 | [diff] [blame] | 12 | 1. Code layout |
| 13 | -------------- |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 14 | |
| 15 | 1.1. The code layout should generally follow the GNU coding standard layout for C, |
| 16 | extended it to C++. |
| 17 | |
| 18 | * Do not use tabs for indentation. |
| 19 | * Indentation spacing is 2 spaces. |
| 20 | * Lines should be within a reasonable range. Lines longer than 100 columns should |
| 21 | generally be avoided. |
| 22 | |
| 23 | 1.2. Whitespace |
| 24 | |
| 25 | * Conventional operators (``if``, ``for``, ``while``, and others) should be |
| 26 | surrounded by a space character. |
| 27 | * Commas should be followed by a white space. |
| 28 | * Semicolons in for statments should be followed by a space character. |
| 29 | |
| 30 | Examples: |
| 31 | |
| 32 | .. code-block:: c++ |
| 33 | |
Davide Pesavento | 0b2aa13 | 2020-12-11 23:00:02 -0500 | [diff] [blame] | 34 | a = (b + c) * d; // NOT: a=(b+c)*d |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 35 | |
Davide Pesavento | 0b2aa13 | 2020-12-11 23:00:02 -0500 | [diff] [blame] | 36 | while (true) { // NOT: while(true) |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 37 | ... |
| 38 | |
Davide Pesavento | 0b2aa13 | 2020-12-11 23:00:02 -0500 | [diff] [blame] | 39 | doSomething(a, b, c, d); // NOT: doSomething(a,b,c,d); |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 40 | |
Davide Pesavento | 0b2aa13 | 2020-12-11 23:00:02 -0500 | [diff] [blame] | 41 | for (i = 0; i < 10; i++) { // NOT: for(i=0;i<10;i++){ |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 42 | ... |
| 43 | |
| 44 | 1.3. Namespaces should have the following form: |
| 45 | |
| 46 | .. code-block:: c++ |
| 47 | |
| 48 | namespace example { |
| 49 | |
| 50 | code; |
| 51 | moreCode; |
| 52 | |
| 53 | } // namespace example |
| 54 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 55 | Note that code inside the namespace is **not** indented. Avoid the following: |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 56 | |
| 57 | .. code-block:: c++ |
| 58 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 59 | // WRONG |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 60 | // |
| 61 | // namespace example { |
| 62 | // |
| 63 | // code; |
| 64 | // moreCode; |
| 65 | // |
| 66 | // } // namespace example |
| 67 | |
Davide Pesavento | af1d6cf | 2017-11-06 23:53:01 -0500 | [diff] [blame] | 68 | 1.4. Class declarations should have the following form: |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 69 | |
| 70 | .. code-block:: c++ |
| 71 | |
| 72 | class SomeClass : public BaseClass |
| 73 | { |
| 74 | public: |
| 75 | ... <public methods> ... |
Davide Pesavento | af1d6cf | 2017-11-06 23:53:01 -0500 | [diff] [blame] | 76 | |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 77 | protected: |
| 78 | ... <protected methods> ... |
Davide Pesavento | af1d6cf | 2017-11-06 23:53:01 -0500 | [diff] [blame] | 79 | |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 80 | private: |
| 81 | ... <private methods> ... |
| 82 | |
| 83 | public: |
| 84 | ... <public data> ... |
Davide Pesavento | af1d6cf | 2017-11-06 23:53:01 -0500 | [diff] [blame] | 85 | |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 86 | protected: |
| 87 | ... <protected data> ... |
Davide Pesavento | af1d6cf | 2017-11-06 23:53:01 -0500 | [diff] [blame] | 88 | |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 89 | private: |
| 90 | ... <private data> ... |
| 91 | }; |
| 92 | |
| 93 | ``public``, ``protected``, ``private`` may be repeated several times without |
Davide Pesavento | af1d6cf | 2017-11-06 23:53:01 -0500 | [diff] [blame] | 94 | interleaving (e.g., public, public, public, private, private) if this improves |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 95 | readability of the code. |
| 96 | |
| 97 | Nested classes can be defined in appropriate visibility section, either in methods |
| 98 | block, data block, or in a separate section (depending which one provides better code |
| 99 | readability). |
| 100 | |
| 101 | 1.5. Method and function definitions should have the following form: |
| 102 | |
| 103 | .. code-block:: c++ |
| 104 | |
| 105 | void |
| 106 | someMethod() |
| 107 | { |
| 108 | ... |
| 109 | } |
| 110 | |
| 111 | void |
| 112 | SomeClass::someMethod() |
| 113 | { |
| 114 | ... |
| 115 | } |
| 116 | |
| 117 | 1.6. The ``if-else`` class of statements should have the following form: |
| 118 | |
| 119 | .. code-block:: c++ |
| 120 | |
| 121 | if (condition) { |
| 122 | statements; |
| 123 | } |
| 124 | |
| 125 | if (condition) { |
| 126 | statements; |
| 127 | } |
| 128 | else { |
| 129 | statements; |
| 130 | } |
| 131 | |
| 132 | if (condition) { |
| 133 | statements; |
| 134 | } |
| 135 | else if (condition) { |
| 136 | statements; |
| 137 | } |
| 138 | else { |
| 139 | statements; |
| 140 | } |
| 141 | |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 142 | 1.7. A ``for`` statement should have the following form: |
| 143 | |
| 144 | .. code-block:: c++ |
| 145 | |
| 146 | for (initialization; condition; update) { |
| 147 | statements; |
| 148 | } |
| 149 | |
Davide Pesavento | af1d6cf | 2017-11-06 23:53:01 -0500 | [diff] [blame] | 150 | An empty ``for`` statement should have the following form: |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 151 | |
| 152 | .. code-block:: c++ |
| 153 | |
| 154 | for (initialization; condition; update) |
| 155 | ; |
| 156 | |
Davide Pesavento | af1d6cf | 2017-11-06 23:53:01 -0500 | [diff] [blame] | 157 | This emphasizes the fact that the ``for`` statement is empty and makes it obvious for |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 158 | the reader that this is intentional. Empty loops should be avoided however. |
| 159 | |
| 160 | 1.8. A ``while`` statement should have the following form: |
| 161 | |
| 162 | .. code-block:: c++ |
| 163 | |
| 164 | while (condition) { |
| 165 | statements; |
| 166 | } |
| 167 | |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 168 | 1.9. A ``do-while`` statement should have the following form: |
| 169 | |
| 170 | .. code-block:: c++ |
| 171 | |
| 172 | do { |
| 173 | statements; |
| 174 | } while (condition); |
| 175 | |
| 176 | 1.10. A ``switch`` statement should have the following form: |
| 177 | |
| 178 | .. code-block:: c++ |
| 179 | |
| 180 | switch (condition) { |
Wentao Shang | 3aa4f41 | 2015-08-18 21:12:50 -0700 | [diff] [blame] | 181 | case ABC: // 2 space indent |
| 182 | statements; // 4 space indent |
Davide Pesavento | fcd3e44 | 2023-03-10 18:44:11 -0500 | [diff] [blame] | 183 | [[fallthrough]]; |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 184 | |
Wentao Shang | 3aa4f41 | 2015-08-18 21:12:50 -0700 | [diff] [blame] | 185 | case DEF: |
| 186 | statements; |
| 187 | break; |
| 188 | |
| 189 | case XYZ: { |
| 190 | statements; |
| 191 | break; |
| 192 | } |
| 193 | |
| 194 | default: |
| 195 | statements; |
| 196 | break; |
| 197 | } |
| 198 | |
| 199 | When curly braces are used inside a ``case`` block, the braces must cover the entire |
| 200 | ``case`` block. |
| 201 | |
| 202 | .. code-block:: c++ |
| 203 | |
| 204 | switch (condition) { |
| 205 | // Correct style |
| 206 | case A0: { |
| 207 | statements; |
| 208 | break; |
| 209 | } |
| 210 | |
| 211 | // Correct style |
| 212 | case A1: { |
| 213 | statements; |
Davide Pesavento | fcd3e44 | 2023-03-10 18:44:11 -0500 | [diff] [blame] | 214 | [[fallthrough]]; |
Wentao Shang | 3aa4f41 | 2015-08-18 21:12:50 -0700 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | // Incorrect style: braces should cover the entire case block |
| 218 | case B: { |
| 219 | statements; |
| 220 | } |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 221 | statements; |
| 222 | break; |
| 223 | |
Wentao Shang | 3aa4f41 | 2015-08-18 21:12:50 -0700 | [diff] [blame] | 224 | default: |
| 225 | break; |
| 226 | } |
| 227 | |
| 228 | The following style is still allowed when none of the ``case`` blocks has curly braces. |
| 229 | |
| 230 | .. code-block:: c++ |
| 231 | |
| 232 | switch (condition) { |
| 233 | case ABC: // no indent |
| 234 | statements; // 2 space indent |
Davide Pesavento | fcd3e44 | 2023-03-10 18:44:11 -0500 | [diff] [blame] | 235 | [[fallthrough]]; |
Wentao Shang | 3aa4f41 | 2015-08-18 21:12:50 -0700 | [diff] [blame] | 236 | |
| 237 | case DEF: |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 238 | statements; |
| 239 | break; |
| 240 | |
| 241 | default: |
| 242 | statements; |
| 243 | break; |
| 244 | } |
| 245 | |
Davide Pesavento | fcd3e44 | 2023-03-10 18:44:11 -0500 | [diff] [blame] | 246 | The ``[[fallthrough]]`` annotation must be included whenever there is a |
| 247 | case without a break statement. Leaving the break out is a common error, |
Davide Pesavento | 1c597a1 | 2017-10-06 15:34:24 -0400 | [diff] [blame] | 248 | and it must be made clear that it is intentional when it is not there. |
Davide Pesavento | fcd3e44 | 2023-03-10 18:44:11 -0500 | [diff] [blame] | 249 | Moreover, modern compilers will warn when a case that falls through is |
| 250 | not explicitly annotated. |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 251 | |
| 252 | 1.11. A ``try-catch`` statement should have the following form: |
| 253 | |
| 254 | .. code-block:: c++ |
| 255 | |
| 256 | try { |
| 257 | statements; |
| 258 | } |
Davide Pesavento | cd183d2 | 2015-09-23 16:40:28 +0200 | [diff] [blame] | 259 | catch (const Exception& exception) { |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 260 | statements; |
| 261 | } |
| 262 | |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 263 | 1.12. The incompleteness of split lines must be made obvious. |
| 264 | |
| 265 | .. code-block:: c++ |
| 266 | |
| 267 | totalSum = a + b + c + |
| 268 | d + e; |
| 269 | function(param1, param2, |
| 270 | param3); |
| 271 | for (int tableNo = 0; tableNo < nTables; |
| 272 | tableNo += tableStep) { |
| 273 | ... |
| 274 | } |
| 275 | |
Davide Pesavento | af1d6cf | 2017-11-06 23:53:01 -0500 | [diff] [blame] | 276 | Split lines occur when a statement exceeds the column limit given in rule 1.1. It is |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 277 | difficult to give rigid rules for how lines should be split, but the examples above should |
Davide Pesavento | af1d6cf | 2017-11-06 23:53:01 -0500 | [diff] [blame] | 278 | give a general hint. In general: |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 279 | |
| 280 | * Break after a comma. |
| 281 | * Break after an operator. |
| 282 | * Align the new line with the beginning of the expression on the previous line. |
| 283 | |
| 284 | Exceptions: |
| 285 | |
Davide Pesavento | af1d6cf | 2017-11-06 23:53:01 -0500 | [diff] [blame] | 286 | * The following is standard practice with ``operator<<``: |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 287 | |
| 288 | .. code-block:: c++ |
| 289 | |
| 290 | std::cout << "Something here " |
| 291 | << "Something there" << std::endl; |
| 292 | |
| 293 | 1.13. When class variables need to be initialized in the constructor, the initialization |
| 294 | should take the following form: |
| 295 | |
| 296 | .. code-block:: c++ |
| 297 | |
| 298 | SomeClass::SomeClass(int value, const std::string& string) |
| 299 | : m_value(value) |
| 300 | , m_string(string) |
| 301 | ... |
| 302 | { |
| 303 | } |
| 304 | |
| 305 | Each initialization should be put on a separate line, starting either with the colon |
| 306 | for the first initialization or with comma for all subsequent initializations. |
| 307 | |
Junxiao Shi | 45c1384 | 2014-11-02 15:36:04 -0700 | [diff] [blame] | 308 | 1.14. A range-based ``for`` statement should have the following form: |
| 309 | |
| 310 | .. code-block:: c++ |
| 311 | |
| 312 | for (T i : range) { |
| 313 | statements; |
| 314 | } |
| 315 | |
Junxiao Shi | cf69818 | 2014-11-03 08:37:42 -0700 | [diff] [blame] | 316 | 1.15. A lambda expression should have the following form: |
| 317 | |
| 318 | .. code-block:: c++ |
| 319 | |
| 320 | [&capture1, capture2] (T1 arg1, T2 arg2) { |
| 321 | statements; |
| 322 | } |
| 323 | |
| 324 | [&capture1, capture2] (T1 arg1, T2 arg2) mutable { |
| 325 | statements; |
| 326 | } |
| 327 | |
| 328 | [this] (T arg) { |
| 329 | statements; |
| 330 | } |
| 331 | |
Junxiao Shi | cf69818 | 2014-11-03 08:37:42 -0700 | [diff] [blame] | 332 | [&] (T arg) { |
| 333 | statements; |
| 334 | } |
| 335 | |
| 336 | [=] (T arg) { |
| 337 | statements; |
| 338 | } |
| 339 | |
Davide Pesavento | 0b2aa13 | 2020-12-11 23:00:02 -0500 | [diff] [blame] | 340 | If the lambda has no parameters, ``()`` should be omitted. |
| 341 | |
| 342 | .. code-block:: c++ |
| 343 | |
| 344 | [&capture1, capture2] { |
| 345 | statements; |
| 346 | } |
| 347 | |
| 348 | Trailing return types should be omitted whenever possible. Add it only when the compiler |
| 349 | cannot deduce the return type automatically, or when it improves readability. Note that |
Davide Pesavento | af1d6cf | 2017-11-06 23:53:01 -0500 | [diff] [blame] | 350 | ``()`` is required by the C++ standard when ``mutable`` or a trailing return type is used. |
Junxiao Shi | cf69818 | 2014-11-03 08:37:42 -0700 | [diff] [blame] | 351 | |
| 352 | .. code-block:: c++ |
| 353 | |
| 354 | [] (T arg) -> int { |
| 355 | statements; |
| 356 | } |
| 357 | |
| 358 | [] () -> int { |
| 359 | statements; |
| 360 | } |
| 361 | |
| 362 | If the function body has only one line, and the whole lambda expression can fit in one line, |
| 363 | the following form is also acceptable: |
| 364 | |
| 365 | .. code-block:: c++ |
| 366 | |
| 367 | [&capture1, capture2] (T1 arg1, T2 arg2) { statement; } |
| 368 | |
Davide Pesavento | 0b2aa13 | 2020-12-11 23:00:02 -0500 | [diff] [blame] | 369 | A no-op lambda can be written in a more compact form: |
Junxiao Shi | cf69818 | 2014-11-03 08:37:42 -0700 | [diff] [blame] | 370 | |
| 371 | .. code-block:: c++ |
| 372 | |
| 373 | []{} |
| 374 | |
Junxiao Shi | 8b12a5a | 2014-11-25 10:42:47 -0700 | [diff] [blame] | 375 | 1.16. List initialization should have the following form: |
| 376 | |
| 377 | .. code-block:: c++ |
| 378 | |
| 379 | T object{arg1, arg2}; |
| 380 | |
| 381 | T{arg1, arg2}; |
| 382 | |
| 383 | new T{arg1, arg2}; |
| 384 | |
| 385 | return {arg1, arg2}; |
| 386 | |
| 387 | function({arg1, arg2}, otherArgument); |
| 388 | |
| 389 | object[{arg1, arg2}]; |
| 390 | |
| 391 | T({arg1, arg2}) |
| 392 | |
Davide Pesavento | 0b2aa13 | 2020-12-11 23:00:02 -0500 | [diff] [blame] | 393 | T object = {arg1, arg2}; |
| 394 | |
Junxiao Shi | 8b12a5a | 2014-11-25 10:42:47 -0700 | [diff] [blame] | 395 | class Class |
| 396 | { |
| 397 | private: |
Davide Pesavento | 0b2aa13 | 2020-12-11 23:00:02 -0500 | [diff] [blame] | 398 | T m_member{arg1, arg2}; |
Junxiao Shi | 8b12a5a | 2014-11-25 10:42:47 -0700 | [diff] [blame] | 399 | static T s_member = {arg1, arg2}; |
| 400 | }; |
| 401 | |
Junxiao Shi | 8b12a5a | 2014-11-25 10:42:47 -0700 | [diff] [blame] | 402 | An empty braced-init-list is written as ``{}``. For example: |
| 403 | |
| 404 | .. code-block:: c++ |
| 405 | |
| 406 | T object{}; |
Junxiao Shi | 8b12a5a | 2014-11-25 10:42:47 -0700 | [diff] [blame] | 407 | T object = {}; |
| 408 | |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 409 | 2. Naming Conventions |
| 410 | --------------------- |
| 411 | |
| 412 | 2.1. C++ header files should have the extension ``.hpp``. Source files should have the |
| 413 | extension ``.cpp`` |
| 414 | |
| 415 | File names should be all lower case. If the class name |
| 416 | is a composite of several words, each word in a file name should be separated with a |
| 417 | dash (-). A class should be declared in a header file and defined in a source file |
| 418 | where the name of the files match the name of the class. |
| 419 | |
| 420 | :: |
| 421 | |
| 422 | my-class.hpp, my-class.cpp |
| 423 | |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 424 | 2.2. Names representing types must be written in English in mixed case starting with upper case. |
| 425 | |
| 426 | .. code-block:: c++ |
| 427 | |
| 428 | class MyClass; |
| 429 | class Line; |
| 430 | class SavingsAccount; |
| 431 | |
| 432 | 2.3. Variable names must be written in English in mixed case starting with lower case. |
| 433 | |
| 434 | .. code-block:: c++ |
| 435 | |
| 436 | MyClass myClass; |
| 437 | Line line; |
| 438 | SavingsAccount savingsAccount; |
| 439 | int theAnswerToLifeTheUniverseAndEverything; |
| 440 | |
| 441 | 2.4. Named constants (including enumeration values) must be all uppercase using underscore |
| 442 | to separate words. |
| 443 | |
| 444 | .. code-block:: c++ |
| 445 | |
| 446 | const int MAX_ITERATIONS = 25; |
| 447 | const std::string COLOR_RED = "red"; |
| 448 | static const double PI = 3.14; |
| 449 | |
| 450 | In some cases, it is a better (or is the only way for complex constants in header-only |
Davide Pesavento | af1d6cf | 2017-11-06 23:53:01 -0500 | [diff] [blame] | 451 | classes) to implement the value as a method. |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 452 | |
| 453 | .. code-block:: c++ |
| 454 | |
Davide Pesavento | af1d6cf | 2017-11-06 23:53:01 -0500 | [diff] [blame] | 455 | static int // declare constexpr if possible |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 456 | getMaxIterations() |
| 457 | { |
| 458 | return 25; |
| 459 | } |
| 460 | |
| 461 | 2.5. Names representing methods or functions must be commands starting with a verb and |
| 462 | written in mixed case starting with lower case. |
| 463 | |
| 464 | .. code-block:: c++ |
| 465 | |
| 466 | std::string |
| 467 | getName() |
| 468 | { |
| 469 | ... |
| 470 | } |
| 471 | |
| 472 | double |
| 473 | computeTotalWidth() |
| 474 | { |
| 475 | ... |
| 476 | } |
| 477 | |
| 478 | 2.6. Names representing namespaces should be all lowercase. |
| 479 | |
| 480 | .. code-block:: c++ |
| 481 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 482 | namespace model::analyzer { |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 483 | |
| 484 | ... |
| 485 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 486 | } // namespace model::analyzer |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 487 | |
Davide Pesavento | af1d6cf | 2017-11-06 23:53:01 -0500 | [diff] [blame] | 488 | 2.7. Names representing generic template types should be a single uppercase letter. |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 489 | |
| 490 | .. code-block:: c++ |
| 491 | |
| 492 | template<class T> ... |
| 493 | template<class C, class D> ... |
| 494 | |
Davide Pesavento | af1d6cf | 2017-11-06 23:53:01 -0500 | [diff] [blame] | 495 | However, when a template parameter represents a certain concept and is expected |
| 496 | to have a certain interface, the name should be explicitly spelled out. |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 497 | |
| 498 | .. code-block:: c++ |
| 499 | |
Davide Pesavento | af1d6cf | 2017-11-06 23:53:01 -0500 | [diff] [blame] | 500 | template<class InputIterator> ... |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 501 | template<class Packet> ... |
| 502 | |
| 503 | 2.8. Abbreviations and acronyms must not be uppercase when used as name. |
| 504 | |
| 505 | .. code-block:: c++ |
| 506 | |
| 507 | exportHtmlSource(); // NOT: exportHTMLSource(); |
| 508 | openDvdPlayer(); // NOT: openDVDPlayer(); |
| 509 | |
| 510 | 2.9. Global variables should have ``g_`` prefix |
| 511 | |
| 512 | .. code-block:: c++ |
| 513 | |
| 514 | g_mainWindow.open(); |
| 515 | g_applicationContext.getName(); |
| 516 | |
| 517 | In general, the use of global variables should be avoided. Consider using singleton |
| 518 | objects instead. |
| 519 | |
Davide Pesavento | 0b2aa13 | 2020-12-11 23:00:02 -0500 | [diff] [blame] | 520 | 2.10. All non-static data members of a class should be prefixed with ``m_`` unless they |
| 521 | are public. Similarly, non-public static data members should be prefixed with ``s_``. |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 522 | |
| 523 | .. code-block:: c++ |
| 524 | |
| 525 | class SomeClass |
| 526 | { |
| 527 | private: |
| 528 | int m_length; |
| 529 | |
| 530 | static std::string s_name; |
| 531 | }; |
| 532 | |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 533 | 2.11. Variables with a large scope should have long (explicit) names, variables with a small |
| 534 | scope can have short names. |
| 535 | |
| 536 | Scratch variables used for temporary storage or indices are best kept short. A |
| 537 | programmer reading such variables should be able to assume that its value is not used |
| 538 | outside of a few lines of code. Common scratch variables for integers are ``i``, |
| 539 | ``j``, ``k``, ``m``, ``n`` and for characters ``c`` and ``d``. |
| 540 | |
| 541 | 2.12. The name of the object is implicit, and should be avoided in a method name. |
| 542 | |
| 543 | .. code-block:: c++ |
| 544 | |
| 545 | line.getLength(); // NOT: line.getLineLength(); |
| 546 | |
| 547 | The latter seems natural in the class declaration, but proves superfluous in use, as |
| 548 | shown in the example. |
| 549 | |
| 550 | 2.13. The terms ``get/set`` must be used where an attribute is accessed directly. |
| 551 | |
| 552 | .. code-block:: c++ |
| 553 | |
| 554 | employee.getName(); |
| 555 | employee.setName(name); |
| 556 | |
| 557 | matrix.getElement(2, 4); |
| 558 | matrix.setElement(2, 4, value); |
| 559 | |
| 560 | 2.14. The term ``compute`` can be used in methods where something is computed. |
| 561 | |
| 562 | .. code-block:: c++ |
| 563 | |
| 564 | valueSet.computeAverage(); |
| 565 | matrix.computeInverse() |
| 566 | |
| 567 | Give the reader the immediate clue that this is a potentially time-consuming operation, |
| 568 | and if used repeatedly, he might consider caching the result. Consistent use of the term |
| 569 | enhances readability. |
| 570 | |
| 571 | 2.15. The term ``find`` can be used in methods where something is looked up. |
| 572 | |
| 573 | .. code-block:: c++ |
| 574 | |
| 575 | vertex.findNearestVertex(); |
| 576 | matrix.findMinElement(); |
| 577 | |
| 578 | Give the reader the immediate clue that this is a simple look up method with a minimum |
| 579 | of computations involved. Consistent use of the term enhances readability. |
| 580 | |
| 581 | 2.16. Plural form should be used on names representing a collection of objects. |
| 582 | |
| 583 | .. code-block:: c++ |
| 584 | |
| 585 | vector<Point> points; |
| 586 | int values[]; |
| 587 | |
| 588 | Enhances readability since the name gives the user an immediate clue of the type of |
| 589 | the variable and the operations that can be performed on its elements. |
| 590 | |
| 591 | 2.17. The prefix ``n`` should be used for variables representing a number of objects. |
| 592 | |
| 593 | .. code-block:: c++ |
| 594 | |
| 595 | nPoints, nLines |
| 596 | |
| 597 | The notation is taken from mathematics where it is an established convention for |
| 598 | indicating a number of objects. |
| 599 | |
Eric Newberry | 436e46f | 2018-06-10 21:45:57 -0700 | [diff] [blame] | 600 | 2.18. The suffix ``Num`` or ``No`` should be used for variables representing an entity number. |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 601 | |
| 602 | .. code-block:: c++ |
| 603 | |
Eric Newberry | 436e46f | 2018-06-10 21:45:57 -0700 | [diff] [blame] | 604 | tableNum, tableNo, employeeNum, employeeNo |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 605 | |
| 606 | 2.19. The prefix ``is``, ``has``, ``need``, or similar should be used for boolean variables and |
| 607 | methods. |
| 608 | |
| 609 | .. code-block:: c++ |
| 610 | |
| 611 | isSet, isVisible, isFinished, isFound, isOpen |
| 612 | needToConvert, needToFinish |
| 613 | |
| 614 | 2.20. Complement names must be used for complement operations, reducing complexity by |
| 615 | symmetry. |
| 616 | |
| 617 | :: |
| 618 | |
Davide Pesavento | 0b2aa13 | 2020-12-11 23:00:02 -0500 | [diff] [blame] | 619 | get/set, add/remove, create/destroy, start/stop, insert/erase, |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 620 | increment/decrement, old/new, begin/end, first/last, up/down, min/max, |
| 621 | next/previous (and commonly used next/prev), open/close, show/hide, |
| 622 | suspend/resume, etc. |
| 623 | |
Davide Pesavento | 0b2aa13 | 2020-12-11 23:00:02 -0500 | [diff] [blame] | 624 | The pair ``insert/erase`` is preferred. ``insert/delete`` can also be used if it |
| 625 | does not conflict with the ``delete`` keyword of C++. |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 626 | |
| 627 | 2.21. Variable names should not include reference to variable type (do not use Hungarian |
| 628 | notation). |
| 629 | |
| 630 | .. code-block:: c++ |
| 631 | |
| 632 | Line* line; // NOT: Line* pLine; |
| 633 | // NOT: Line* linePtr; |
| 634 | |
| 635 | size_t nPoints; // NOT lnPoints |
| 636 | |
| 637 | char* name; // NOT szName |
| 638 | |
| 639 | 2.22. Negated boolean variable names should be avoided. |
| 640 | |
| 641 | .. code-block:: c++ |
| 642 | |
| 643 | bool isError; // NOT: isNoError |
| 644 | bool isFound; // NOT: isNotFound |
| 645 | |
| 646 | 2.23. Enumeration constants recommended to prefix with a common type name. |
| 647 | |
| 648 | .. code-block:: c++ |
| 649 | |
| 650 | enum Color { |
| 651 | COLOR_RED, |
| 652 | COLOR_GREEN, |
| 653 | COLOR_BLUE |
| 654 | }; |
| 655 | |
| 656 | 2.24. Exceptions can be suffixed with either ``Exception`` (e.g., ``SecurityException``) or |
| 657 | ``Error`` (e.g., ``SecurityError``). |
| 658 | |
Davide Pesavento | 923ba44 | 2019-02-12 22:00:38 -0500 | [diff] [blame] | 659 | The recommended method is to declare an exception class ``Exception`` or ``Error`` as |
| 660 | a nested type inside the class from which the exception is thrown. For example, when |
| 661 | defining a class ``Foo`` that can throw errors, one can write the following: |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 662 | |
| 663 | .. code-block:: c++ |
| 664 | |
| 665 | #include <stdexcept> |
| 666 | |
| 667 | class Foo |
| 668 | { |
Junxiao Shi | 68b5385 | 2018-07-25 13:56:38 -0600 | [diff] [blame] | 669 | public: |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 670 | class Error : public std::runtime_error |
| 671 | { |
| 672 | public: |
Junxiao Shi | 68b5385 | 2018-07-25 13:56:38 -0600 | [diff] [blame] | 673 | // You can inherit constructors from std::runtime_error like this: |
| 674 | using std::runtime_error::runtime_error; |
| 675 | |
Davide Pesavento | 923ba44 | 2019-02-12 22:00:38 -0500 | [diff] [blame] | 676 | // Additional constructors, if desired, can be declared as usual: |
Junxiao Shi | 68b5385 | 2018-07-25 13:56:38 -0600 | [diff] [blame] | 677 | Error(const std::string& what, const std::exception& inner) |
| 678 | : std::runtime_error(what + ": " + inner.what()) |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 679 | { |
| 680 | } |
| 681 | }; |
| 682 | }; |
| 683 | |
| 684 | In addition to that, if class Foo is a base class or interface for some class |
| 685 | hierarchy, then child classes should should define their own ``Error`` or |
| 686 | ``Exception`` classes that are inherited from the parent's Error class. |
| 687 | |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 688 | 2.25. Functions (methods returning something) should be named after what they return and |
| 689 | procedures (void methods) after what they do. |
| 690 | |
| 691 | Increase readability. Makes it clear what the unit should do and especially all the |
| 692 | things it is not supposed to do. This again makes it easier to keep the code clean of |
| 693 | side effects. |
| 694 | |
| 695 | 3. Miscellaneous |
| 696 | ---------------- |
| 697 | |
| 698 | 3.1. Exceptions can be used in the code, but should be used only in exceptional cases and |
| 699 | not in the primary processing path. |
| 700 | |
| 701 | 3.2. Header files must contain an include guard. |
| 702 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 703 | For example, a header file named ``module/class-name.hpp`` or |
| 704 | ``src/module/class-name.hpp`` should have a header guard in the following form: |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 705 | |
| 706 | .. code-block:: c++ |
| 707 | |
| 708 | #ifndef APP_MODULE_CLASS_NAME_HPP |
| 709 | #define APP_MODULE_CLASS_NAME_HPP |
| 710 | ... |
| 711 | #endif // APP_MODULE_CLASS_NAME_HPP |
| 712 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 713 | The macro name should reflect the path of the header file relative to the root of the |
| 714 | source tree, in order to prevent naming conflicts. The header guard should be prefixed |
| 715 | with the application/library name to avoid conflicts with other packages and libraries. |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 716 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 717 | 3.3. Include directives for system headers and other external libraries should use |
| 718 | ``<angle brackets>``. Header files in the same source code repository should be included |
| 719 | using ``"quotes"``. |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 720 | |
| 721 | .. code-block:: c++ |
| 722 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 723 | #include "ndn-cxx/util/random.hpp" |
| 724 | |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 725 | #include <string> |
| 726 | #include <boost/lexical_cast.hpp> |
| 727 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 728 | All of a project's header files should be included with their path relative to |
| 729 | the project's source directory. The use of UNIX directory shortcuts ``.`` |
| 730 | (the current directory) and ``..`` (the parent directory) is discouraged. |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 731 | |
Junxiao Shi | 4f92d87 | 2017-07-25 22:04:48 +0000 | [diff] [blame] | 732 | 3.4. Include statements should be grouped. Same-project headers should be included first. |
Davide Pesavento | af1d6cf | 2017-11-06 23:53:01 -0500 | [diff] [blame] | 733 | Leave an empty line between groups of include statements. Sort alphabetically within a group. |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 734 | For example, the include section of ``ndn-cxx/foo/bar.cpp`` may look like this: |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 735 | |
| 736 | .. code-block:: c++ |
| 737 | |
Junxiao Shi | 24c5a00 | 2018-12-12 04:47:15 +0000 | [diff] [blame] | 738 | #include "ndn-cxx/impl/pending-interest.hpp" |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 739 | #include "ndn-cxx/util/random.hpp" |
Junxiao Shi | 4f92d87 | 2017-07-25 22:04:48 +0000 | [diff] [blame] | 740 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 741 | #include <cstdlib> |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 742 | #include <fstream> |
| 743 | #include <iomanip> |
| 744 | |
| 745 | #include <boost/lexical_cast.hpp> |
| 746 | #include <boost/regex.hpp> |
| 747 | |
Davide Pesavento | 0b2aa13 | 2020-12-11 23:00:02 -0500 | [diff] [blame] | 748 | 3.5. Definitions that are local to only one ``.cpp`` file should be declared inside that |
| 749 | file and be placed in an unnamed namespace or declared ``static``. |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 750 | |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 751 | 3.6. Implicit conversion is generally allowed. |
| 752 | |
| 753 | Implicit conversion between integer and floating point numbers can cause problems and |
| 754 | should be avoided. |
| 755 | |
Davide Pesavento | 0b2aa13 | 2020-12-11 23:00:02 -0500 | [diff] [blame] | 756 | Implicit conversion in constructors that can be called with a single argument is usually |
| 757 | undesirable. Therefore, all single-argument constructors should be marked ``explicit``, |
| 758 | unless implicit conversion is desirable. In that case, a comment should document the |
| 759 | reason for this. |
| 760 | As an exception, copy and move constructors should not be explicit, since they do not |
| 761 | perform type conversion. |
| 762 | Constructors that cannot be called with a single argument may omit ``explicit``. |
| 763 | Constructors that take a single ``std::initializer_list`` parameter should also omit |
| 764 | ``explicit``, in order to support copy-initialization. |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 765 | |
Davide Pesavento | 0b2aa13 | 2020-12-11 23:00:02 -0500 | [diff] [blame] | 766 | Avoid C-style casts. |
| 767 | Use ``static_cast``, ``dynamic_cast``, ``const_cast``, ``reinterpret_cast``, or |
| 768 | ``bit_cast`` instead where appropriate. |
| 769 | Use ``static_pointer_cast``, ``dynamic_pointer_cast``, or ``const_pointer_cast`` |
| 770 | when dealing with ``shared_ptr``. |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 771 | |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 772 | 3.7. Variables should be initialized where they are declared. |
| 773 | |
| 774 | This ensures that variables are valid at any time. Sometimes it is impossible to |
Davide Pesavento | af1d6cf | 2017-11-06 23:53:01 -0500 | [diff] [blame] | 775 | initialize a variable to a valid value where it is declared. |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 776 | |
| 777 | .. code-block:: c++ |
| 778 | |
| 779 | int x, y, z; |
| 780 | getCenter(&x, &y, &z); |
| 781 | |
| 782 | In these cases it should be left uninitialized rather than initialized to some phony |
| 783 | value. |
| 784 | |
Davide Pesavento | af1d6cf | 2017-11-06 23:53:01 -0500 | [diff] [blame] | 785 | 3.8. In most cases, class data members should not be declared ``public``. |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 786 | |
Davide Pesavento | af1d6cf | 2017-11-06 23:53:01 -0500 | [diff] [blame] | 787 | Public data members violate the concepts of information hiding and encapsulation. |
| 788 | Use private variables and public accessor methods instead. |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 789 | |
| 790 | Exceptions to this rule: |
| 791 | |
Davide Pesavento | 0b2aa13 | 2020-12-11 23:00:02 -0500 | [diff] [blame] | 792 | * When the class is essentially a passive data structure with no or minimal behavior |
| 793 | (equivalent to a C struct, also known as POD type). In this case, all fields should |
| 794 | be public and the keyword ``struct`` should be used instead of ``class``. |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 795 | |
Davide Pesavento | af1d6cf | 2017-11-06 23:53:01 -0500 | [diff] [blame] | 796 | * When the class is used only inside the compilation unit, e.g., when implementing pImpl |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 797 | idiom (aka Bridge pattern) or similar cases. |
| 798 | |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 799 | 3.9. C++ pointers and references should have their reference symbol next to the type rather |
| 800 | than to the name. |
| 801 | |
| 802 | .. code-block:: c++ |
| 803 | |
| 804 | float* x; // NOT: float *x; |
| 805 | int& y; // NOT: int &y; |
| 806 | |
| 807 | 3.10. Implicit test for 0 should not be used other than for boolean variables and pointers. |
| 808 | |
| 809 | .. code-block:: c++ |
| 810 | |
Davide Pesavento | 0b2aa13 | 2020-12-11 23:00:02 -0500 | [diff] [blame] | 811 | if (nLines != 0) // NOT: if (nLines) |
| 812 | |
| 813 | int* ptr = ... |
| 814 | if (ptr) // OK |
| 815 | if (ptr != nullptr) // also OK |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 816 | |
Davide Pesavento | bbca1b9 | 2015-12-25 20:06:00 +0100 | [diff] [blame] | 817 | 3.11. *(removed)* |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 818 | |
| 819 | 3.12. Loop variables should be initialized immediately before the loop. |
| 820 | |
| 821 | .. code-block:: c++ |
| 822 | |
Davide Pesavento | af1d6cf | 2017-11-06 23:53:01 -0500 | [diff] [blame] | 823 | bool isDone = false; // NOT: bool isDone = false; |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 824 | while (!isDone) { // // other stuff |
Davide Pesavento | af1d6cf | 2017-11-06 23:53:01 -0500 | [diff] [blame] | 825 | ... // while (!isDone) { |
| 826 | } // ... |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 827 | // } |
| 828 | |
Davide Pesavento | af1d6cf | 2017-11-06 23:53:01 -0500 | [diff] [blame] | 829 | 3.13. The form ``while (true)`` should be used for infinite loops. |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 830 | |
| 831 | .. code-block:: c++ |
| 832 | |
| 833 | while (true) { |
| 834 | ... |
| 835 | } |
| 836 | |
| 837 | // NOT: |
| 838 | for (;;) { // NO! |
Davide Pesavento | 0b2aa13 | 2020-12-11 23:00:02 -0500 | [diff] [blame] | 839 | ... |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 840 | } |
| 841 | while (1) { // NO! |
Davide Pesavento | 0b2aa13 | 2020-12-11 23:00:02 -0500 | [diff] [blame] | 842 | ... |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 843 | } |
| 844 | |
| 845 | 3.14. Complex conditional expressions must be avoided. Introduce temporary boolean variables |
| 846 | instead. |
| 847 | |
| 848 | .. code-block:: c++ |
| 849 | |
| 850 | bool isFinished = (elementNo < 0) || (elementNo > maxElement); |
| 851 | bool isRepeatedEntry = elementNo == lastElement; |
| 852 | if (isFinished || isRepeatedEntry) { |
| 853 | ... |
| 854 | } |
| 855 | |
| 856 | // NOT: |
| 857 | // if ((elementNo < 0) || (elementNo > maxElement) || elementNo == lastElement) { |
| 858 | // ... |
| 859 | // } |
| 860 | |
| 861 | By assigning boolean variables to expressions, the program gets automatic |
Davide Pesavento | af1d6cf | 2017-11-06 23:53:01 -0500 | [diff] [blame] | 862 | documentation. The construction will be easier to read, debug, and maintain. |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 863 | |
| 864 | 3.15. The conditional should be put on a separate line. |
| 865 | |
| 866 | .. code-block:: c++ |
| 867 | |
| 868 | if (isDone) // NOT: if (isDone) doCleanup(); |
| 869 | doCleanup(); |
| 870 | |
| 871 | This is for debugging purposes. When writing on a single line, it is not apparent |
| 872 | whether the test is really true or not. |
| 873 | |
| 874 | 3.16. Assignment statements in conditionals must be avoided. |
| 875 | |
| 876 | .. code-block:: c++ |
| 877 | |
| 878 | File* fileHandle = open(fileName, "w"); |
| 879 | if (!fileHandle) { |
| 880 | ... |
| 881 | } |
| 882 | |
| 883 | // NOT |
| 884 | // if (!(fileHandle = open(fileName, "w"))) { |
| 885 | // .. |
| 886 | // } |
| 887 | |
| 888 | 3.17. The use of magic numbers in the code should be avoided. Numbers other than 0 and 1 |
| 889 | should be considered declared as named constants instead. |
| 890 | |
| 891 | If the number does not have an obvious meaning by itself, the readability is enhanced |
Davide Pesavento | af1d6cf | 2017-11-06 23:53:01 -0500 | [diff] [blame] | 892 | by introducing a named constant instead. A different approach is to introduce a method |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 893 | from which the constant can be accessed. |
| 894 | |
Davide Pesavento | af1d6cf | 2017-11-06 23:53:01 -0500 | [diff] [blame] | 895 | 3.18. Floating point literals should always be written with a decimal point, at least one |
| 896 | decimal, and without omitting 0 before the decimal point. |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 897 | |
| 898 | .. code-block:: c++ |
| 899 | |
| 900 | double total = 0.0; // NOT: double total = 0; |
| 901 | double someValue = 0.1; // NOT double someValue = .1; |
| 902 | double speed = 3.0e8; // NOT: double speed = 3e8; |
| 903 | double sum; |
| 904 | ... |
| 905 | sum = (a + b) * 10.0; |
| 906 | |
| 907 | 3.19. ``goto`` should not be used. |
| 908 | |
Davide Pesavento | af1d6cf | 2017-11-06 23:53:01 -0500 | [diff] [blame] | 909 | ``goto`` statements violate the idea of structured code. Only in very few cases (for |
| 910 | instance, breaking out of deeply nested structures) should ``goto`` be considered, |
| 911 | and only if the alternative structured counterpart is proven to be less readable. |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 912 | |
Junxiao Shi | 03b15b3 | 2014-10-30 21:10:25 -0700 | [diff] [blame] | 913 | 3.20. ``nullptr`` should be used to represent a null pointer, instead of "0" or "NULL". |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 914 | |
| 915 | 3.21. Logical units within a block should be separated by one blank line. |
| 916 | |
| 917 | .. code-block:: c++ |
| 918 | |
| 919 | Matrix4x4 matrix = new Matrix4x4(); |
| 920 | |
| 921 | double cosAngle = Math.cos(angle); |
| 922 | double sinAngle = Math.sin(angle); |
| 923 | |
| 924 | matrix.setElement(1, 1, cosAngle); |
| 925 | matrix.setElement(1, 2, sinAngle); |
| 926 | matrix.setElement(2, 1, -sinAngle); |
| 927 | matrix.setElement(2, 2, cosAngle); |
| 928 | |
| 929 | multiply(matrix); |
| 930 | |
| 931 | Enhance readability by introducing white space between logical units of a block. |
| 932 | |
| 933 | 3.22. Variables in declarations can be left aligned. |
| 934 | |
| 935 | .. code-block:: c++ |
| 936 | |
| 937 | AsciiFile* file; |
| 938 | int nPoints; |
| 939 | float x, y; |
| 940 | |
| 941 | Enhance readability. The variables are easier to spot from the types by alignment. |
| 942 | |
| 943 | 3.23. Use alignment wherever it enhances readability. |
| 944 | |
| 945 | .. code-block:: c++ |
| 946 | |
| 947 | value = (potential * oilDensity) / constant1 + |
| 948 | (depth * waterDensity) / constant2 + |
| 949 | (zCoordinateValue * gasDensity) / constant3; |
| 950 | |
| 951 | minPosition = computeDistance(min, x, y, z); |
| 952 | averagePosition = computeDistance(average, x, y, z); |
| 953 | |
| 954 | There are a number of places in the code where white space can be included to enhance |
| 955 | readability even if this violates common guidelines. Many of these cases have to do |
| 956 | with code alignment. General guidelines on code alignment are difficult to give, but |
| 957 | the examples above should give a general clue. |
| 958 | |
| 959 | 3.24. All comments should be written in English. |
| 960 | |
Davide Pesavento | af1d6cf | 2017-11-06 23:53:01 -0500 | [diff] [blame] | 961 | In an international environment, English is the preferred language. |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 962 | |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 963 | 3.25. Use ``//`` for all comments, including multi-line comments. |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 964 | |
| 965 | .. code-block:: c++ |
| 966 | |
| 967 | // Comment spanning |
| 968 | // more than one line. |
| 969 | |
| 970 | Since multilevel C-commenting is not supported, using ``//`` comments ensure that it |
| 971 | is always possible to comment out entire sections of a file using ``/* */`` for |
| 972 | debugging purposes etc. |
| 973 | |
| 974 | There should be a space between the ``//`` and the actual comment, and comments should |
| 975 | always start with an upper case letter and end with a period. |
| 976 | |
| 977 | However, method and class documentation comments should use ``/** */`` style for |
Junxiao Shi | bd2cedb | 2017-07-05 18:51:52 +0000 | [diff] [blame] | 978 | Doxygen, JavaDoc and JSDoc. License boilerplate should use ``/* */`` style. |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 979 | |
| 980 | 3.26. Comments should be included relative to their position in the code. |
| 981 | |
| 982 | .. code-block:: c++ |
| 983 | |
| 984 | while (true) { |
| 985 | // Do something |
| 986 | something(); |
| 987 | } |
| 988 | |
| 989 | // NOT: |
| 990 | while (true) { |
| 991 | // Do something |
| 992 | something(); |
| 993 | } |
| 994 | |
| 995 | This is to avoid that the comments break the logical structure of the program. |
Junxiao Shi | ae61aac | 2014-11-04 14:57:38 -0700 | [diff] [blame] | 996 | |
| 997 | 3.27. Use ``BOOST_ASSERT`` and ``BOOST_ASSERT_MSG`` for runtime assertions. |
| 998 | |
| 999 | .. code-block:: c++ |
| 1000 | |
| 1001 | int x = 1; |
| 1002 | int y = 2; |
| 1003 | int z = x + y; |
| 1004 | BOOST_ASSERT(z - y == x); |
| 1005 | |
Davide Pesavento | 0b2aa13 | 2020-12-11 23:00:02 -0500 | [diff] [blame] | 1006 | The expression passed to ``BOOST_ASSERT`` must not have side effects, |
| 1007 | because it may not be evaluated in release builds. |
Junxiao Shi | ae61aac | 2014-11-04 14:57:38 -0700 | [diff] [blame] | 1008 | |
Davide Pesavento | 0b2aa13 | 2020-12-11 23:00:02 -0500 | [diff] [blame] | 1009 | 3.28. Use ``static_assert`` for compile-time assertions. |
Junxiao Shi | ae61aac | 2014-11-04 14:57:38 -0700 | [diff] [blame] | 1010 | |
Junxiao Shi | ae61aac | 2014-11-04 14:57:38 -0700 | [diff] [blame] | 1011 | .. code-block:: c++ |
| 1012 | |
| 1013 | class BaseClass |
| 1014 | { |
| 1015 | }; |
| 1016 | |
| 1017 | class DerivedClass : public BaseClass |
| 1018 | { |
| 1019 | }; |
| 1020 | |
| 1021 | static_assert(std::is_base_of<BaseClass, DerivedClass>::value, |
| 1022 | "DerivedClass must inherit from BaseClass"); |
Junxiao Shi | c0a8c3b | 2014-11-08 12:09:05 -0700 | [diff] [blame] | 1023 | |
Davide Pesavento | 0b2aa13 | 2020-12-11 23:00:02 -0500 | [diff] [blame] | 1024 | 3.29. The ``auto`` type specifier may be used for local variables if a human reader can |
| 1025 | easily deduce the actual type, or if it makes the code safer. |
Junxiao Shi | c0a8c3b | 2014-11-08 12:09:05 -0700 | [diff] [blame] | 1026 | |
| 1027 | .. code-block:: c++ |
| 1028 | |
| 1029 | std::vector<int> intVector; |
Davide Pesavento | 0b2aa13 | 2020-12-11 23:00:02 -0500 | [diff] [blame] | 1030 | auto i = intVector.find(4); // OK |
Junxiao Shi | c0a8c3b | 2014-11-08 12:09:05 -0700 | [diff] [blame] | 1031 | |
Davide Pesavento | 0b2aa13 | 2020-12-11 23:00:02 -0500 | [diff] [blame] | 1032 | auto stringSet = std::make_shared<std::set<std::string>>(); // OK |
Junxiao Shi | c0a8c3b | 2014-11-08 12:09:05 -0700 | [diff] [blame] | 1033 | |
Davide Pesavento | 0b2aa13 | 2020-12-11 23:00:02 -0500 | [diff] [blame] | 1034 | std::vector<std::string> strings; |
| 1035 | for (const auto& str : strings) { // OK, iterating over the elements of a container |
| 1036 | std::cout << str; |
Junxiao Shi | c0a8c3b | 2014-11-08 12:09:05 -0700 | [diff] [blame] | 1037 | } |
Davide Pesavento | 0b2aa13 | 2020-12-11 23:00:02 -0500 | [diff] [blame] | 1038 | |
| 1039 | obj.onEvent([] (auto&&...) { std::cout << "hi!"; }); // OK, unused lambda parameters |
| 1040 | |
| 1041 | auto x = foo(); // BAD unless foo() is declared nearby or has a well-known prototype |
Junxiao Shi | a76bbc9 | 2015-03-23 11:05:37 -0700 | [diff] [blame] | 1042 | |
Davide Pesavento | de2a1c2 | 2016-12-11 15:46:13 -0500 | [diff] [blame] | 1043 | 3.30. Use the ``override`` or ``final`` specifier when overriding a virtual |
| 1044 | member function or a virtual destructor. |
Junxiao Shi | a76bbc9 | 2015-03-23 11:05:37 -0700 | [diff] [blame] | 1045 | |
Davide Pesavento | 0b2aa13 | 2020-12-11 23:00:02 -0500 | [diff] [blame] | 1046 | ``virtual`` must not be used along with ``final`` so that the compiler can generate |
| 1047 | an error when a final function does not override. |
Davide Pesavento | de2a1c2 | 2016-12-11 15:46:13 -0500 | [diff] [blame] | 1048 | |
Davide Pesavento | 0b2aa13 | 2020-12-11 23:00:02 -0500 | [diff] [blame] | 1049 | ``virtual`` should not be used along with ``override`` for consistency with ``final``. |
Junxiao Shi | a76bbc9 | 2015-03-23 11:05:37 -0700 | [diff] [blame] | 1050 | |
| 1051 | .. code-block:: c++ |
| 1052 | |
| 1053 | class Stream |
| 1054 | { |
| 1055 | public: |
| 1056 | virtual void |
| 1057 | open(); |
| 1058 | }; |
| 1059 | |
| 1060 | class InputStream : public Stream |
| 1061 | { |
| 1062 | public: |
Davide Pesavento | de2a1c2 | 2016-12-11 15:46:13 -0500 | [diff] [blame] | 1063 | void |
Junxiao Shi | a76bbc9 | 2015-03-23 11:05:37 -0700 | [diff] [blame] | 1064 | open() override; |
| 1065 | }; |
| 1066 | |
| 1067 | class Console : public InputStream |
| 1068 | { |
| 1069 | public: |
Davide Pesavento | de2a1c2 | 2016-12-11 15:46:13 -0500 | [diff] [blame] | 1070 | void |
| 1071 | open() final; |
Junxiao Shi | a76bbc9 | 2015-03-23 11:05:37 -0700 | [diff] [blame] | 1072 | }; |
| 1073 | |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 1074 | 3.31. The recommended way to throw an exception derived from ``std::exception`` is to use |
Davide Pesavento | 923ba44 | 2019-02-12 22:00:38 -0500 | [diff] [blame] | 1075 | ``NDN_THROW`` or one of the other ``NDN_THROW_*`` macros. |
Davide Pesavento | 0b2aa13 | 2020-12-11 23:00:02 -0500 | [diff] [blame] | 1076 | |
| 1077 | Exceptions thrown using these macros will be augmented with additional diagnostic |
| 1078 | information, including the file name, line number, and function name from which |
| 1079 | the exception was thrown. |
| 1080 | |
| 1081 | The extended diagnostic information contained in the exception can be printed with |
| 1082 | ``boost::diagnostic_information()``. |
| 1083 | |
| 1084 | .. code-block:: c++ |
| 1085 | |
| 1086 | #include <boost/exception/diagnostic_information.hpp> |
| 1087 | #include <iostream> |
| 1088 | |
| 1089 | try { |
| 1090 | operationThatMayThrow(); |
| 1091 | } |
| 1092 | catch (const std::exception& e) { |
| 1093 | std::cerr << boost::diagnostic_information(e); |
| 1094 | } |