1. Readability first. (Your code should be your documentation most of the time.)
2. Crash/Assert early. Don't wait until the worst case happens to make the crash condition.
3. Follow IDE's auto formatted style unless you have really good reasons not to do so. (Ctrl + K + D in VC++)
4. Learn from existing code.
2. Use camel casing for local variable names and function parameters.
voidSomeMethod(constint someParameter)
{
int someNumber;
}
3. Use verb-object pairs for method names
classSomeClass
{
public:// a. Use pascal casing for public methodsvoidDoSomehting();
private:// a. Use camel casing for other methodsvoiddoSomething();
};
4. Use ALL_CAPS_SEPARATED_BY_UNDERSCORE for constants and defines.
constexprint SOME_CONSTANT = 1;
5. Use all lowercase letters for namespaces.
namespaceabc
{
}
6. Prefix boolean variables with b.
bool bFired; // for local and public member variablebool mbFired; // for private class member variable
17. There must be a blank line between includes and body.
18. Use #pragma once at the beginning of every header file.
19. Use Visual Studio default for tabs. If you are not using Visual Studio, use real tabs that are equal to 4 spaces.
20. Declare local variables as close as possible to the first line where it is being used.
21. Always place an opening curly braces({) in a new line.
22. Add curly braces even if there's only one line in the scope.
if (bSomething)
{
return;
}
23. Use precision specification for floating point values unless there is an explicit need for a double.
float f = 0.5f;
24. Always have a default case for a switch statement.
switch (number)
{
case0:
break;
default:
break;
}
25. Always add predefined FALLTHROUGH for switch case fall through unless there is no code in the case statement. This will be replaced by [[fallthrough]] attribute coming in for C++17 after.
26. If default case must not happen in a switch case, always add Assert(false). In our assert implementation, this will add optimization hint for release build.
27. Use consts as much as possible even for local variable and function parameters.
28. Any member functions that doesn't modify the object must be const.
intGetAge() const;
29. Do not return const value type. Const return is only for reference and pointers.
30. Names of recursive functions end with Recursive.
voidFibonacciRecursive();
31. Order of class variables and methods must be as follows:
a. list of friend classes
b. public methods
c. protected methods
d. private methods
e. protected variables
f. private variables
32. Function overloading must be avoided in most cases
37. When a class spans across multiple files, these files have a name that starts with the name of the class, followed by an underscore and the subsection name.
38. Platform specific class for "reverse OOP" pattern uses similar nameing convention.
classRenderer;
Renderer.h // all renderer interfaces called by games
Renderer.cpp // Renderer's Implementations which are to all platforms
Renderer_gl.h // RendererGL interfaces called by Renderer
Renderer_gl.cpp // RendererGL implementations
39. Use our own version of Assert instead of standard c assert.
40. Use assert for any assertion you have. Assert is not recoverable. This can be replaced by compiler optimization hint keyword __assume for the release build.
41. Any memory allocation must be done through our own New and Delete keyword.
42. Memory operations such as memset, memcpy and memmove also must be done through our own MemSet, MemCpy and MemMove.
43. Generally prefer reference(&) over pointers unless you need nullptr for any reason.
44. Use pointers for out parameters. Also prefix the function parameters with out.
45. The above out parameters must not be null. (Use assert, not if statement)
// BAD:int counter, index;
// GOOD:int counter;
int index;
61. Do not use const member variables in a struct or class simply to prevent modification after initialization. Same rule is applied to reference(&) member variables.
62. Take advantage of NRVO, when you are returning a local object. This means you need to have only one return statement inside your function. This applies only when you return an object by value.
2. Modern Language Features
1. override and final keywords are mandatory.
2. Use enum class always.
enumclasseDirection
{
North,
South
};
3. Use static_assert over Assert, if possible.
4. Use nullptr over NULL.
5. Use unique_ptr when a object lifetime is solely handled inside a class. (i.e. new in constructor delete in destructor)
6. Range-based for are recommended where applicable.
7. Do not use auto unless it is for a iterator or new keyword is on the same line, showing which object is created clearly.
8. Do not manually perform return value optimization using std::move. It breaks automatic NRVO optimization.
9. Move constructor and move assignment operator are allowed.
10. Use constexpr instead of const for simple constant variables.