Table of contents:
General
Add a copyright notice to the beginning of each file.
Use #pragma once
instead of include guards.
Use classes to define objects with both state and behavior, structs to group related properties together, and global functions otherwise.
Arrange class members in the following order: enums, classes/structs, variables, constructors/destructors, commands, getters, setters.
Prefer to use using namespace
in source files, never use them in header files.
Prefer smart pointers to raw pointers.
Naming
Use Pascal case for class names, e.g. FooBar
.
Use Camel case for function names and struct members, e.g. fooBar
.
Use Camel case with a leading underscore for class member variables, e.g. _fooBar
.
Prefix constants with a letter "k", e.g. kFooBar
.
Prefix global variables with a letter "g", e.g. g_fooBar
.
Formatting
Indentation
Use 4 spaces for indentation.
Do not pad class/struct member names to make columns.
Pointers and References
void *foo;
int &bar;
Conditional Statements
if (foo) {
baz();
} else if (bar) {
baz();
} else {
baz();
}
Switch Statement
switch (foo) {
case 0:
bar();
break;
default:
baz();
break;
}
Class Declaration
namespace foo {
class Foo;
class Bar {
public:
void foo();
};
} // namespace foo
Comments
Use Javadoc-style comments for class, function and variable declarations.
/**
* Calculates the square of a number.
*
* @param x number to calculate the square of
* @return square of a number
*/
int square(int x);
Use C++-style comments for code folding and implementation notes.
// Test methods
void testSquare() {
// Arrange
int x = 2;
// Act
int xx = square(x);
// Assert
assert(x == 4);
}
// END Test methods
Includes
Group and arrange includes in the following order:
- Standard library
- Boost
- Third-party libraries
- Project headers that are higher in the folder hierarchy
- Project headers that are lower in the folder hierarchy