Changes between Version 55 and Version 56 of Coding_Conventions


Ignore:
Timestamp:
Aug 17, 2022, 3:12:55 PM (21 months ago)
Author:
phosit
Comment:

Changes discussed in https://wildfiregames.com/forum/topic/89564-changing-c-coding-conventions

Legend:

Unmodified
Added
Removed
Modified
  • Coding_Conventions

    v55 v56  
    2202201. For `.cpp` files that contain function implementations, headers that declare those functions should be included here. For instance, `#include "Simulation2.h"` for `Simulation2.cpp`, or `#include "ICmpPosition.h"` for `CCmpPosition.cpp`.
    2212211. Include the rest of the files that come from 0 A.D. Write the complete path relative to `source/`, for instance `#include "simulation2/Simulation2.h"`. Use case-insensitive alphabetical order.
    222 1. Include system libraries, in case-insensitive alphabetical order.
     2221. Include system libraries and third party libraries, in case-insensitive alphabetical order.
    223223
    224224Example, for the file `source/simulation2/Simulation2.cpp` (this is not the actual list, but an example that should cover all cases):
     
    247247
    248248#include <iomanip>
     249#include <boost/optional.hpp>
    249250#include <string>
    250251}}}
     
    267268}}}
    268269
    269  * Use STL when appropriate.
     270 * [https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Res-lib Prefer the standard library to other libraries and to “handcrafted code”]
    270271
    271272 * Don't use RTTI (`dynamic_cast` etc). ''Exception:'' `source/tools/atlas/AtlasUI/` can use RTTI.
     
    276277   * Prefer global variables over singletons, because then they're not trying to hide their ugliness.
    277278
    278  * Use `nullptr` for pointers (instead of `NULL`).
    279 
    280  * Don't do "`if (p) delete p;`". (That's redundant since "`delete nullptr;`" is safe and does nothing.)
     279 * Use `nullptr` for pointers (instead of `NULL`). This convention should be implicit but stated hear because there are still many places were `NULL` is used.
    281280
    282281 * If deleting a pointer, and it's not in a destructor, and it's not being immediately assigned a new value, use "`SAFE_DELETE(p)`" (which is equivalent to "`delete p; p = nullptr;`") to avoid dangling pointers to deleted memory.
    283282
    284  * Be sure to be aware of [CodeAndMemoryPerformance Code And Memory Performance] guidelines
    285 
    286  * Use "for range" loop instead of "std::for_each".
    287 {{{
    288 #!cpp
    289 // Avoid
    290 std::vector<T> anyVector;
    291 std::for_each(anyVector.begin(), anyVector.end(), [] (const T& element){
    292    // code
    293 }
    294 
    295 // Better
    296 for (const T& element : anyVector)
    297 {
    298    // code
     283 * A loop should not be "controled" by the body:
     284   * don't use `break` or `return` statements.
     285   * don't mutate the iterator variable. Can be enforced by using range-based for loops.
     286{{{
     287#!cpp
     288// Bad
     289for (size_t i = 0; i != 6; ++i)
     290{
     291    // This loop never ends
     292    ++i;
     293}
     294// God
     295for (const auto i : std::ranges::iota(0, 6))
     296{
     297    ++i; //compile-time error;
    299298}
    300299}}}
     
    313312}}}
    314313
    315  * In `c++11`, it is possible to use the `auto` type-specifier, wherein the appropriate data-type is determined at compile time. Although potentially useful, overuse can cause serious problems in the long-run. Therefore, this code feature should be used in moderation. Specifically:
    316    - It should be clear from reading the code what type the `auto` is replacing. (Add a comment if necessary.)
    317    - It should only ever be used as a replacement for an iterator type.
    318    - It should only be used if the data-type-specifier it is standing in for is long. As a rule of thumb, if the line would be shorter than your chosen suitable line width (see convention on avoiding wide lines under [#Formatting formatting] above) without it, don't use it.
     314 * [https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Res-auto Use `auto` to avoid redundant repetition of type names.] Be aware that overuse of `auto` can also cause problems in the long-run.
    319315
    320316 * Favour early returns where possible. The following:
     
    340336}
    341337}}}
     338
     339 * Be sure to be aware of [CodeAndMemoryPerformance Code And Memory Performance] guidelines
     340
     341 * There aro also the [https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines C++ Core Guidelines]
    342342
    343343