Skip to content

CPP Style Guide

Cedric Sirianni edited this page Nov 2, 2023 · 2 revisions

Follow these rules when writing C++:

Key Points

  • All headers should have #define guards
  • Use headers over forward declarations at the top of a file
  • Never put anything in the std namespace
  • Avoid global variables
  • Never use using namespace
  • Use header files with namespaces (e.g. database)
  • Document any public functions, classes, methods, or members using Doxygen in the header file
  • Don't document private stuff or stuff in an anonymous namespace
  • Use const reference whenever possible
  • Avoid global variables
  • No virtual functions in constructors
  • Only use a struct for passive data. Put everything else in a class
  • Prefer struct over pair or tuple
  • Use return values over output parameters
  • Avoid functions over 40 lines
  • Use nullptr instead of NULL
  • Member variables should have a trailing '_'
  • Use auto if it makes the code cleaner

Naming Conventions

  • Variable names are snake_case
  • Function names are camelCase
  • Class names are PascalCase

Imports

Include directives should be in the following order:

  1. C system headers
  2. C++ std headers
  3. Library headers (spdlog, etc.)
  4. Project headers
Clone this wiki locally