Changes between Version 56 and Version 57 of Coding_Conventions
- Timestamp:
- 08/17/22 21:01:56 (2 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
Coding_Conventions
v56 v57 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 and third party libraries, in case-insensitive alphabetical order.222 1. Include system 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>250 249 #include <string> 251 250 }}} … … 268 267 }}} 269 268 270 * [https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Res-lib Prefer the standard library to other libraries and to “handcrafted code”]269 * Use STL when appropriate. 271 270 272 271 * Don't use RTTI (`dynamic_cast` etc). ''Exception:'' `source/tools/atlas/AtlasUI/` can use RTTI. … … 277 276 * Prefer global variables over singletons, because then they're not trying to hide their ugliness. 278 277 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. 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.) 280 281 281 282 * 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. 282 283 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 // Bad289 for (size_t i = 0; i != 6; ++i) 290 {291 // This loop never ends292 ++i; 293 } 294 // God295 for (const auto i : std::ranges::iota(0, 6))296 { 297 ++i; //compile-time error;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 298 299 } 299 300 }}} … … 312 313 }}} 313 314 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. 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. 315 319 316 320 * Favour early returns where possible. The following: … … 336 340 } 337 341 }}} 338 339 * Be sure to be aware of [CodeAndMemoryPerformance Code And Memory Performance] guidelines340 341 * There aro also the [https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines C++ Core Guidelines]342 342 343 343