|
| 1 | +# Code Styles and Guidelines |
| 2 | + |
| 3 | +## General |
| 4 | + |
| 5 | +* Use UTF-8 encoding |
| 6 | +* Windows should use CRLF line breaks, MacOS and Linux(Unix-like OSes) should use LF line breaks |
| 7 | +* Use `enum class` over `enum` |
| 8 | + https://stackoverflow.com/questions/18335861/why-is-enum-class-preferred-over-plain-enum |
| 9 | + https://www.geeksforgeeks.org/enum-classes-in-c-and-their-advantage-over-enum-datatype/ |
| 10 | +* Line break at EOF |
| 11 | + https://stackoverflow.com/questions/729692/why-should-text-files-end-with-a-newline |
| 12 | + Force it in VSCode |
| 13 | + https://stackoverflow.com/questions/44704968/visual-studio-code-insert-newline-at-the-end-of-files |
| 14 | +* Write portable code by following C++ standards and avoid using `#pragma` directives |
| 15 | +* Add line breaks wherever it makes sense |
| 16 | +* Use primary operators instead of alternative ones([ref](https://en.cppreference.com/w/cpp/language/operator_alternative)) |
| 17 | + |
| 18 | +## Includes |
| 19 | + |
| 20 | +Use `#ifndef` for include guards instead of `#pragma once` |
| 21 | + |
| 22 | +Include guard format: `<NAMESPACE>_<FILENAME>_<FILE_EXTENSION>` |
| 23 | + |
| 24 | +E.g.: Include guard for `GameObject.hpp` should be `GAME_OBJECT_HPP` |
| 25 | + |
| 26 | +Internal header should use `""` and external headers should use `<>` |
| 27 | + |
| 28 | +Unless specified, included headers should be the following order: |
| 29 | + |
| 30 | +1. definition header(if needed) |
| 31 | +2. system header |
| 32 | +3. external header |
| 33 | +4. internal header |
| 34 | + |
| 35 | +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 |
| 36 | + |
| 37 | +E.g. includes for `Player.cpp` should be |
| 38 | + |
| 39 | +```cpp= |
| 40 | +#include "Player.hpp" |
| 41 | +
|
| 42 | +#include <string> |
| 43 | +#include <vector> |
| 44 | +
|
| 45 | +#include <spdlog/spdlog.h> |
| 46 | +
|
| 47 | +#include "Math/Vector2.hpp" |
| 48 | +
|
| 49 | +// ... |
| 50 | +``` |
| 51 | + |
| 52 | +See also: https://clangd.llvm.org/guides/include-cleaner |
| 53 | + |
| 54 | +## Class |
| 55 | + |
| 56 | +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. |
| 57 | + |
| 58 | +1. Functions |
| 59 | + 1. Constructor |
| 60 | + 1. Default |
| 61 | + 2. Parameterized |
| 62 | + 3. Copy |
| 63 | + 4. Move |
| 64 | + 2. Destructor |
| 65 | + 3. Operator overload |
| 66 | + 1. Copy assignment |
| 67 | + 2. Move assignment |
| 68 | + 4. Getter |
| 69 | + 5. Setter |
| 70 | + 6. Other |
| 71 | +2. Variables |
| 72 | + |
| 73 | +Follow C++ rule of three/five/zero |
| 74 | + https://en.cppreference.com/w/cpp/language/rule_of_three |
| 75 | + |
| 76 | +## File Types |
| 77 | + |
| 78 | +C source file: `.c` |
| 79 | +C header file: `.h` |
| 80 | +C++ source file: `.cpp` |
| 81 | +C++ header file: `.hpp` |
| 82 | + |
| 83 | +Vertex Shader: `.vert` |
| 84 | +Fragment Shader: `.frag` |
| 85 | + |
| 86 | +## File and Folder Naming |
| 87 | + |
| 88 | +Source and header files should be `PascalCase` if it defines a class or struct, otherwise it should be `snake_case` |
| 89 | + |
| 90 | +Top level folders should only consist of a single word and be `lowercase` (e.g. `src/`, `include/`, `lib/`) |
| 91 | + |
| 92 | +Source and header file folders should be `PascalCase` and be the same name as its namespace |
| 93 | + |
| 94 | +E.g.: `include/Math/Vector2.hpp` should be: |
| 95 | + |
| 96 | +```cpp= |
| 97 | +#ifndef MATH_VECTOR2_HPP |
| 98 | +#define MATH_VECTOR2_HPP |
| 99 | +
|
| 100 | +namespace Math { |
| 101 | +class Vector2 { |
| 102 | +public: |
| 103 | + Vector2(); |
| 104 | +
|
| 105 | +} |
| 106 | +} |
| 107 | +
|
| 108 | +#endif |
| 109 | +
|
| 110 | +``` |
| 111 | + |
| 112 | +## Variable Naming Convention |
| 113 | + |
| 114 | +Lower items override higher items if rules collide |
| 115 | + |
| 116 | +| | Case | Prefix | Suffix | |
| 117 | +| ------------------- | ------------ | ------ | ------ | |
| 118 | +| Namespace | `PascalCase` | | | |
| 119 | +| Class | `PascalCase` | | | |
| 120 | +| Class member | `PascalCase` | `m_` | | |
| 121 | +| Static class member | `PascalCase` | `s_` | | |
| 122 | +| Struct | `PascalCase` | | | |
| 123 | +| Struct member | `camelCase` | | | |
| 124 | +| Enum | `PascalCase` | | | |
| 125 | +| Enum element | `UPPER_CASE` | | | |
| 126 | +| Function/Method | `camelCase` | | | |
| 127 | +| Parameter | `camelCase` | | | |
| 128 | +| Variable | `camelCase` | | | |
| 129 | +| Type Alias/Typedef | `PascalCase` | | | |
| 130 | +| Global constant | `UPPER_CASE` | | | |
| 131 | +| Macro | `UPPER_CASE` | | | |
| 132 | +| Template parameter | `UPPER_CASE` | | | |
0 commit comments