Changes between Version 55 and Version 56 of Coding_Conventions
- Timestamp:
- Aug 17, 2022, 3:12:55 PM (21 months ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
Coding_Conventions
v55 v56 220 220 1. 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`. 221 221 1. 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.222 1. Include system libraries and third party libraries, in case-insensitive alphabetical order. 223 223 224 224 Example, for the file `source/simulation2/Simulation2.cpp` (this is not the actual list, but an example that should cover all cases): … … 247 247 248 248 #include <iomanip> 249 #include <boost/optional.hpp> 249 250 #include <string> 250 251 }}} … … 267 268 }}} 268 269 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”] 270 271 271 272 * Don't use RTTI (`dynamic_cast` etc). ''Exception:'' `source/tools/atlas/AtlasUI/` can use RTTI. … … 276 277 * Prefer global variables over singletons, because then they're not trying to hide their ugliness. 277 278 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. 281 280 282 281 * 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. 283 282 284 * Be sure to be aware of [CodeAndMemoryPerformance Code And Memory Performance] guidelines285 286 * Use "for range" loop instead of "std::for_each".287 {{{ 288 #!cpp 289 // Avoid290 std::vector<T> anyVector; 291 std::for_each(anyVector.begin(), anyVector.end(), [] (const T& element){292 // code293 } 294 295 // Better296 for (const T& element : anyVector)297 { 298 // code283 * 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 289 for (size_t i = 0; i != 6; ++i) 290 { 291 // This loop never ends 292 ++i; 293 } 294 // God 295 for (const auto i : std::ranges::iota(0, 6)) 296 { 297 ++i; //compile-time error; 299 298 } 300 299 }}} … … 313 312 }}} 314 313 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. 319 315 320 316 * Favour early returns where possible. The following: … … 340 336 } 341 337 }}} 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] 342 342 343 343