- Use UTF-8 encoding
- Windows should use CRLF line breaks, MacOS and Linux(Unix-like OSes) should use LF line breaks
- Use
enum classoverenumhttps://stackoverflow.com/questions/18335861/why-is-enum-class-preferred-over-plain-enum https://www.geeksforgeeks.org/enum-classes-in-c-and-their-advantage-over-enum-datatype/ - Line break at EOF https://stackoverflow.com/questions/729692/why-should-text-files-end-with-a-newline Force it in VSCode https://stackoverflow.com/questions/44704968/visual-studio-code-insert-newline-at-the-end-of-files
- Write portable code by following C++ standards and avoid using
#pragmadirectives - Add line breaks wherever it makes sense
- Use primary operators instead of alternative ones(ref)
Use #ifndef for include guards instead of #pragma once
Include guard format: <NAMESPACE>_<FILENAME>_<FILE_EXTENSION>
E.g.: Include guard for GameObject.hpp should be GAME_OBJECT_HPP
Internal header should use "" and external headers should use <>
Unless specified, included headers should be the following order:
- definition header(if needed)
- system header
- external header
- internal header
Different categories are separated with line breaks. If headers are in the same category, headers for the same libraries should be grouped together and be in alphabetical order
E.g. includes for Player.cpp should be
#include "Player.hpp"
#include <string>
#include <vector>
#include <spdlog/spdlog.h>
#include "Math/Vector2.hpp"
// ...
See also: https://clangd.llvm.org/guides/include-cleaner
Member functions and variables declaration should be in the following order, access specifiers should be in the order of public -> protected -> private. Static members should be declared above normal members.
- Functions
- Constructor
- Default
- Parameterized
- Copy
- Move
- Destructor
- Operator overload
- Copy assignment
- Move assignment
- Getter
- Setter
- Other
- Constructor
- Variables
Follow C++ rule of three/five/zero https://en.cppreference.com/w/cpp/language/rule_of_three
C source file: .c
C header file: .h
C++ source file: .cpp
C++ header file: .hpp
Vertex Shader: .vert
Fragment Shader: .frag
Source and header files should be PascalCase if it defines a class or struct, otherwise it should be snake_case
Top level folders should only consist of a single word and be lowercase (e.g. src/, include/, lib/)
Source and header file folders should be PascalCase and be the same name as its namespace
E.g.: include/Math/Vector2.hpp should be:
#ifndef MATH_VECTOR2_HPP
#define MATH_VECTOR2_HPP
namespace Math {
class Vector2 {
public:
Vector2();
}
}
#endif
Lower items override higher items if rules collide
| Case | Prefix | Suffix | |
|---|---|---|---|
| Namespace | PascalCase |
||
| Class | PascalCase |
||
| Class member | PascalCase |
m_ |
|
| Static class member | PascalCase |
s_ |
|
| Struct | PascalCase |
||
| Struct member | camelCase |
||
| Enum | PascalCase |
||
| Enum element | UPPER_CASE |
||
| Function/Method | camelCase |
||
| Parameter | camelCase |
||
| Variable | camelCase |
||
| Type Alias/Typedef | PascalCase |
||
| Global constant | UPPER_CASE |
||
| Macro | UPPER_CASE |
||
| Template parameter | UPPER_CASE |