2018-06-13 20:22:46 +00:00
|
|
|
# Code Style Guidelines
|
2018-06-01 19:24:46 +00:00
|
|
|
|
|
|
|
## Basics
|
|
|
|
|
|
|
|
Basic statements:
|
|
|
|
|
|
|
|
```c++
|
2021-05-04 21:32:38 +00:00
|
|
|
void global_function(int arg1,
|
|
|
|
const int arg2, // You can use "const" preferably
|
|
|
|
const int arg3, ...)
|
2018-06-01 19:24:46 +00:00
|
|
|
{
|
|
|
|
int value;
|
|
|
|
const int constValue = 0;
|
|
|
|
|
2021-05-04 21:32:38 +00:00
|
|
|
// We prefer to use "var = (condition ? ...: ...)" instead of
|
|
|
|
// "var = condition ? ...: ...;" to make clear about the
|
|
|
|
// ternary operator limits.
|
|
|
|
int conditionalValue1 = (condition ? 1: 2);
|
|
|
|
int conditionalValue2 = (condition ? longVarName:
|
|
|
|
otherLongVarName);
|
|
|
|
|
|
|
|
// If a condition will return, we prefer the "return"
|
|
|
|
// statement in its own line to avoid missing the "return"
|
|
|
|
// keyword when we read code.
|
|
|
|
if (condition)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// You can use braces {} if the condition has multiple lines
|
|
|
|
// or the if-body has multiple lines.
|
|
|
|
if (condition1 ||
|
|
|
|
condition2) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-06-01 19:24:46 +00:00
|
|
|
if (condition) {
|
|
|
|
...
|
2021-05-04 21:32:38 +00:00
|
|
|
...
|
|
|
|
...
|
2018-06-01 19:24:46 +00:00
|
|
|
}
|
|
|
|
|
2021-05-04 21:32:38 +00:00
|
|
|
// We prefer to avoid whitespaces between "var=initial_value"
|
|
|
|
// or "var<limit" to see better the "; " separation. Anyway it
|
|
|
|
// can depend on the specific condition/case, etc.
|
2018-06-01 19:24:46 +00:00
|
|
|
for (int i=0; i<10; ++i) {
|
|
|
|
...
|
2021-05-04 21:32:38 +00:00
|
|
|
// Same case as in if-return.
|
|
|
|
if (condition)
|
|
|
|
break;
|
|
|
|
...
|
2018-06-01 19:24:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
while (condition) {
|
|
|
|
...
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
...
|
2021-01-14 15:45:32 +00:00
|
|
|
} while (condition);
|
2018-06-01 19:24:46 +00:00
|
|
|
|
|
|
|
switch (condition) {
|
|
|
|
case 1:
|
|
|
|
...
|
|
|
|
break;
|
|
|
|
case 2: {
|
|
|
|
int varInsideCase;
|
|
|
|
...
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
## Namespaces
|
|
|
|
|
|
|
|
Define namespaces with lower case:
|
|
|
|
|
|
|
|
```c++
|
|
|
|
namespace app {
|
|
|
|
|
|
|
|
...
|
|
|
|
|
|
|
|
} // namespace app
|
|
|
|
```
|
|
|
|
|
|
|
|
## Classes
|
|
|
|
|
|
|
|
Define classes with `CapitalCase` and member functions with `camelCase`:
|
|
|
|
|
|
|
|
```c++
|
|
|
|
class ClassName {
|
|
|
|
public:
|
2021-05-04 21:32:38 +00:00
|
|
|
ClassName()
|
|
|
|
: m_memberVarA(1),
|
|
|
|
m_memberVarB(2),
|
|
|
|
m_memberVarC(3) {
|
|
|
|
...
|
|
|
|
}
|
|
|
|
|
2018-06-01 19:24:46 +00:00
|
|
|
virtual ~ClassName();
|
|
|
|
|
2021-05-04 21:32:38 +00:00
|
|
|
// We can return in the same line for getter-like functions
|
2018-06-01 19:24:46 +00:00
|
|
|
int memberVar() const { return m_memberVar; }
|
|
|
|
void setMemberVar();
|
|
|
|
|
|
|
|
protected:
|
2021-05-04 21:32:38 +00:00
|
|
|
virtual void onEvent1() { } // Do nothing functions can be defined as "{ }"
|
|
|
|
virtual void onEvent2() = 0;
|
2018-06-01 19:24:46 +00:00
|
|
|
|
|
|
|
private:
|
2021-05-04 21:32:38 +00:00
|
|
|
int m_memberVarA;
|
|
|
|
int m_memberVarB;
|
|
|
|
int m_memberVarC;
|
|
|
|
int m_memberVarD = 4; // We can initialize variables here too
|
|
|
|
};
|
|
|
|
|
|
|
|
class Special : public ClassName {
|
|
|
|
public:
|
|
|
|
Special();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
void onEvent2() override {
|
|
|
|
...
|
|
|
|
}
|
2018-06-01 19:24:46 +00:00
|
|
|
};
|
|
|
|
```
|
|
|
|
|
2021-08-27 20:42:44 +00:00
|
|
|
## Const
|
|
|
|
|
|
|
|
* [NL.26: Use conventional const notation](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#nl26-use-conventional-const-notation)
|
|
|
|
|
2018-06-01 19:24:46 +00:00
|
|
|
## C++11
|
|
|
|
|
2020-09-20 16:16:14 +00:00
|
|
|
We are using some modern C++ (C++11, C++14, etc.) features, mainly:
|
2018-06-01 19:24:46 +00:00
|
|
|
|
|
|
|
* Use `nullptr` instead of `NULL` macro
|
2020-09-20 16:16:14 +00:00
|
|
|
* Use `auto` for complex types, iterators, or when the variable type
|
|
|
|
is obvious (e.g. `auto s = new Sprite;`)
|
2018-06-01 19:24:46 +00:00
|
|
|
* Use range-based for loops (`for (const auto& item : values) { ... }`)
|
|
|
|
* Use template alias (`template<typename T> alias = orig<T>;`)
|
|
|
|
* Use non-generic lambda functions
|
2020-07-07 22:06:48 +00:00
|
|
|
* Use `std::shared_ptr`, `std::unique_ptr`, or `base::Ref`
|
2020-04-24 15:07:52 +00:00
|
|
|
* Use `base::clamp` (no `std::clamp` yet)
|
|
|
|
* Use `static constexpr T v = ...;`
|
2018-06-01 19:24:46 +00:00
|
|
|
* You can use `<atomic>`, `<thread>`, `<mutex>`, and `<condition_variable>`
|
2020-07-07 22:06:48 +00:00
|
|
|
* Prefer `using T = ...;` instead of `typedef ... T`
|
2020-04-24 15:07:52 +00:00
|
|
|
* We use gcc 9.2 or clang 9.0 on Linux, so check the features available in
|
2021-05-29 13:59:06 +00:00
|
|
|
https://en.cppreference.com/w/cpp/compiler_support
|