1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
|
# SuperTux Coding Standards
## Language
C++14 is the main langauge used for this project. GCC, Clang and MSVC are supported.
For better backward compatibilty with older compiler, namely gcc5,
some C++14 features are restricted:
* generic lambda functions are not allowed, e.g. `[](auto foo){}`
* tuple constructors have to be explicit, e.g. `std::tuple<int, int>{5, 6}`, not `{5, 6}`
## Repository Structure
Properly separate between generic engine code and game specific code whenever
feasible.
Third party libraries are not allowed in `src/`, they go to `external/`.
Third party libraries that are imported into `external/` as git
submodule have to be forked and included into the SuperTux
organisation on Github, not directly included from upstream.
## File Formating
Do not have spaces at the end of lines.
Files should always end with `/* EOF */` and a newline or a similar
marker approprimate for the given language. This marker can be left
out for fileformats that have an end tag, e.g. `</html>`.
Aim for one file per class, small helper classes in the same file are ok.
## Includes
The path in `#include` directives must not contain `..`.
All paths for includes from SuperTux must be relative to the `src/`
directory and use `#include "..."`.
Use the `#include <>` syntax for libraries in `external/`, use `cmake`
to set the include path properly.
The order of includes shall be as follows, each of those subgroups
shall be ordered alphabetically:
* include of header file when in a .cpp file
* include of the base class in a header file of a derived class
* system includes
* external includes
* local includes
Conditional includes should be indented.
```c++
#ifdef FOOBAR
# include "foobar.hpp"
#endif
```
Include guards are of the form:
```c++
#ifndef HEADER_SUPERTUX_{PATH}_{FILE}_HPP
#define HEADER_SUPERTUX_{PATH}_{FILE}_HPP
```
`tools/fix_include_guards.sh` is a little script that will help to fix
include guards on file renames.
## Variables
Prefix member variable names with `m_`, global variables with `g_`, and static
variables with `s_`. DynamicScopeRefs are prefixed with `d_`.
## Classes
Mark all classes as `final` unless they are specifically designed
with polymorphism in mind.
Mark all functions that override a virtual function in a base class with `override`.
Write simple getters/setters inside a header file on a single line.
Properly separate data members and member functions. Do not mix them in the same
`public`/`protected`/`private` section.
List virtual functions before non-virtual functions.
The order of declarations in a class shall be as follows:
```c++
class Foo final
{
public:
protected:
private:
// type declarations, needs to come first as later stuff might depend on them
public:
protected:
private:
// static stuff
public:
protected:
private:
// constructors
// destructor
public:
protected:
private:
// virtual member functions
// non-virtual member functions
public:
protected:
private:
// member variables with an m_ prefix
private:
// non-copyable footer
Foo(const Foo&) = delete;
Foo& operator=(const Foo&) = delete;
};
```
## Pointers
Do not use raw pointers and `new`/`delete`, use
`std::unique_ptr<>`/`std::make_unique<>` instead.
Only use `std::smart_ptr<>` when sharing of data is required, prefer
`std::unique_ptr<>` when possible.
Pass and return values as value, `&` or `const&`, only use `*` when
the value is expected to be `nullptr`.
Do not pass values as `const std::unique_ptr<T>&` or `const
std::shared_ptr<T>&`, dereference the pointer and pass as `const&`.
To check for nullptr, use an `if` statements with initializer when possible:
```c++
if (auto* ptr = get_ptr()) {
// code here
}
```
## auto
Don't use `auto` for basic types (`int`, `float`, `std::string`, ...)
Only use `auto` when the exact type is unnecessary to know (e.g.
iterators) or obvious from the context, (e.g. `auto foo =
Foo::create()`)
Capture pointers as `auto*`, not just `auto`.
Ues `const auto&` for loops to avoid copies.
## Namespaces
Namespaces should be written in the form:
```c++
namespace my_namespace {
...
} // namespace my_namespace
```
With no newline before the `{`. Do not indent the content inside the
namespace. The namespace itself should be all lowercase.
## Compiler Warnings and Errors
Compile with a maximum warning level and with `-Werror`. This can be accomplished with:
```console
cmake .. -DCMAKE_BUILD_TYPE=Release -DWARNINGS=ON -DWERROR=ON
```
This requires, among other things:
* use of `final` and `override` keywords
* use of `static_cast` and `reinterpret_cast`, not old style C casts
* all member variables have to be initialized in the constructor
* all `int`/`float` conversion has to be explicit
## Comments
Avoid comments unless they explain something important and
non-obvious. Document *why* the code does something, not *what* it
does.
Prefer to use good function and variable names to create
self-documenting code.
Use the `//` syntax for regular comments, even multiline, don't use `/* */`.
For Doxygen (code documentation), use the `/** */` syntax, don't use
`/**<` and other styles of comments.
For translator information, use the `// l10n:` syntax.
Don't do `*` prefix decorations in comments, keep things simple and
compact:
```c++
/*
* Don't do this
*/
```
Instead:
```c++
// Do this
```
or:
```c++
/** Also ok when it's a Doxygen comment */
```
## Spaces
Use a space after `if`/`while`/`switch`/`for`:
`for (int i = 0; i < len; ++i) ...`
`if (a > b) ...`
`while (a > b) ...`
`switch (myenum) ...`
But don't use a space after a function name:
`myfunc ()` // don't do this
`myfunc()` // do this
## Line breaks
Write simple getters/setters inside a header file on a single line:
```c++
Vector get_pos() const { retun m_pos; }
```
Don't include more complex functions inside a header file, unless it's
necessary (template, performance).
Declare functions like:
```c++
ReturnType
ClassName::function_name()
{
...
}
```
We don't have any hard rules on linebreaks before `{` inside
functions, but generally prefer the no-linebreak version when doing
simple one-liners and the other one when doing more complex stuff:
```c++
if (foo) {
one_line_function_call()
}
```
```c++
if (foo)
{
long();
complex();
series();
of();
calls();
}
```
## Other Information
More general info on good practices can be found in [Google's C++
Style Guide](https://google.github.io/styleguide/cppguide.html) and in
the [C++ Core Guidelines](http://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines),
note however that we do not strictly follow either of them and divert from
them in some points, so just take them as general guidelines, not hard rules.
|