Skip to content

Coding conventions

Alexander Grund edited this page Sep 9, 2015 · 9 revisions

Compilation

  • Read the README.md in the repository root for instructions on how to compile on Linux, Windows/Visual Studio, ...
  • 64Bit compilation on MSVC is currently not supported
  • Compilation MUST finish without any warnings

Naming

  • Classes/Structs: UpperCamelCase
  • Parameters, local variables: lowerCamelCase
  • Member variables: Suffix "_" (e.g. myMember_)
  • Do not shadow variables (e.g. using the name of a member as a parameter)

General

  • Small include files
    • Use forward declarations instead of including headers
    • Include what you use (e.g. when using std::vector, include <vector>)
    • Once class/struct per file, avoid "collections" of structs even when they belong partly together
  • Check STL/Boost before rolling your own implementations
  • Implementing any of the Big 3 (copy ctor, copy assignment, destructor) requires implementing the others as well. For classes, that should not be copied (e.g. having open handles) declare copy ctor AND copy assignment in private region and do not implement it. Put a comment there.
  • Comment functions you write at least with a summary in the header. use JavaDoc syntax (Either /// or /** foo bar */)
  • Make sure assumptions are met ->
    • Use static assert (boost) to check what is possible during compile-time
    • Use exceptions for enforcing stuff at runtime (e.g. parameter values, switch(enum) defaults)
    • Use asserts for general conditions that should be met, but are less critical or performance-wise expensive to check

Safety

  • Use RAII idiom
    • Encapsulate all resources once they are acquired
    • Use STL collections (vector, list, ...)
    • Use smart pointers (boost unique_ptr, intrusive_shared_ptr, shared_pointer, in that order)
  • delete[] should never be used. Use C++ structs (e.g. vector)
  • Use boost::arrayinstead of plain C-arrays
  • Do NOT use magic constants. All constants should be made named consts (or defines). For iterations, use iterators or actual sizes
  • Do not use printf/sprint/snprintf,fprintf,... They can easily cause buffer overflows. Use C++ streams instead

Clone this wiki locally