C++ style guide. See this page. There are a lot of very reasonable recommendations to adopt for other programming languages.

Scoping

  • Place a function’s variables in the narrowest scope possible.
  • Initialise variables in the declaration; declare variables close to where they’re used.
    • This means that variables needed within if, while, and for should be declared within those statements.
    • Except for objects, because the constructor/destructor would be invoked several times.

Classes

  • Only use a struct for passive objects that carry data. Everything else should be a class.
  • Structs are preferred to pairs or tuples whenever the elements can have meaningful names.

Functions

  • Functions should be small and focused. Don’t exceed 40 lines by splitting them up.

Other C++ features

  • Friends should be defined in the same file.