aboutsummaryrefslogtreecommitdiff
path: root/contributing.md
blob: 4c347bbdeaf83c1975eb60212b7ec3dc4ca26c79 (plain)
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
# Contributing new code

- Please do the following *before* sending a pull request:
  - Merge upstream code (if any) back into your own branch
  - Run formatters/linters

# Git

- Push as often as possible
- Development is done on separate branches, these follow a pattern of
  `name/feature` (i.e. `loek/dll-so-poc` or `jaro/class2`)
- The master branch is considered stable, and should always contain a
  working/compiling version of the project

- TODO: tagging / versions


# Code style

- ASCII only

  ```cpp
  // Good
  std::string message = "Hello, world!";
  
  // Bad
  std::string message = "こんにちは世界";
  ```

- Class names are always singular

  ```cpp
  // Good
  class Car {};
  
  // Bad
  class Cars {};
  ```

- Explanatory comments are placed above the line(s) they are explaining

  ```cpp
  // Good
  // This function adds two numbers
  int add(int a, int b) {
      return a + b;
  }
  
  // Bad
  int add(int a, int b) {
      return a + b; // This function adds two numbers
  }
  ```

- Source files should only contain comments that plainly state what the code is supposed to do

  ```cpp
  // Good
  // Initialize the engine
  engine.init();
  
  // Bad
  // Initialize the engine with default settings and prepare it for running
  engine.init();
  ```

- Explanatory comments in headers may be used to clarify implementation design decisions

  ```cpp
  // Good
  // This class handles the rendering process
  class Renderer {
      // This method initializes the rendering context
      void init_context();
  };
  
  // Bad
  class Renderer {
      void init_context(); // This method initializes the rendering context
  };
  ```

- Formatting nitty-gritty is handled by clang-format/clang-tidy (run `make format` in the root folder of this repository to format all sources files)


- Header includes are split into paragraphs separated by a blank line. The order is:
  1. system headers (using `<`brackets`>`)
  2. relative headers NOT in the same folder as the current file
  3. relative headers in the same folder as the current file

  ```cpp
  // Good
  #include <iostream>
  
  #include "utils/helper.h"
  
  #include "main.h"
  
  // Bad
  #include "main.h"
  #include <iostream>
  #include "utils/helper.h"
  ```

- When using libraries of which the header include order is important, make sure to separate the include statements using a blank line (clang-format may sort include statements, but does not sort across empty lines).

  ```cpp
  // Good
  #include <iostream>
  
  #include <boost/algorithm/string.hpp>
  
  // Bad
  #include <iostream>
  #include <boost/algorithm/string.hpp>
  ```

- All engine-related code is implemented under the `crepe` namespace, user-facing APIs under `crepe::api` (the folder structure should also reflect this).


- `using namespace` may not be used in header files, only in source files.

  ```cpp
  // Good
  // header.h
  namespace crepe {
      void init();
  }
  
  // source.cpp
  #include "header.h"
  using namespace crepe;
  void init() {}
  
  // Bad
  // header.h
  using namespace crepe;
  void init();
  ```

- Do not (indirectly) include private *dependency* headers in API header files, as these are no longer accessible when the engine is installed

  ```cpp
  // Good
  // api.h
  namespace crepe::api {
      void start();
  }
  
  // Bad
  // api.h
  #include "private_dependency.h"
  namespace crepe::api {
      void start();
  }
  ```

- Getter and setter functions are appropriately prefixed with `get_` and `set_`.

  ```cpp
  // Good
  class Car {
  public:
      int get_speed() const;
      void set_speed(int speed);
  private:
      int speed;
  };
  
  // Bad
  class Car {
  public:
      int speed() const;
      void speed(int speed);
  private:
      int speed;
  };
  ```

- Doxygen commands are used with a backslash instead of an at-sign (i.e. `\brief` instead of `@brief`)

  ```cpp
  // Good
  /// \brief This function adds two numbers
  int add(int a, int b);
  
  // Bad
  /// @brief This function adds two numbers
  int add(int a, int b);
  ```

- A singleton's instance is always accessed using a getter function that instantiates its own class as a static variable within the getter function scope, instead of storing the instance as a member variable directly:

  ```cpp
  class Bad {
      static Bad instance;
      Bad & get_instance() { return instance; }
  };

  class Good {
      Good & get_instance() {
          static Good instance;
          return instance;
      }
  };
  ```

- Member variable default values should be directly defined in the class declaration instead of using the constructor.

  ```cpp
  // Good
  class Car {
  private:
      int speed = 0;
  };
  
  // Bad
  class Car {
  private:
      int speed;
      Car() : speed(0) {}
  };
  ```

- Header files declare either a single class or symbols within a single namespace.

  ```cpp
  // Good
  // car.h
  namespace crepe {
      class Car {};
  }
  
  // Bad
  // car.h
  namespace crepe {
      class Car {};
      class Engine {};
  }
  ```

- Use of the `auto` type is not allowed, with the following exceptions:
  - When naming the item type in a range-based for loop
  - When calling template factory methods that explicitly name the return type in the function call signature
  - When fetching a singleton instance

  ```cpp
  // Good
  for (auto item : items) {}
  
  auto instance = Singleton::get_instance();
  
  // Bad
  auto speed = car.get_speed();
  ```

- Only use member initializer lists for non-trivial types.

  ```cpp
  // Good
  class Car {
  public:
      Car() : engine("V8") {}
  private:
      std::string engine;
  };
  
  // Bad
  class Car {
  public:
      Car() : speed(0) {}
  private:
      int speed;
  };
  ```

- C++-style structs should define default values for all non-trivial fields.

  ```cpp
  // Good
  struct Car {
      std::string model = "Unknown";
  };
  
  // Bad
  struct Car {
      std::string model;
      Car() : model("Unknown") {}
  };
  ```

- Declare incomplete classes instead of including the relevant header where possible (i.e. if you only need a reference or pointer type).

  ```cpp
  // Good
  class Engine;
  class Car {
      Engine* engine;
  };
  
  // Bad
  #include "engine.h"
  class Car {
      Engine* engine;
  };
  ```

- Template functions are only declared in a `.h` header, and defined in a matching `.hpp` header.

  ```cpp
  // Good
  // add.h
  template <typename T>
  T add(T a, T b);
  
  // add.hpp
  #include "add.h"
  template <typename T>
  T add(T a, T b) {
      return a + b;
  }
  
  // Bad
  // add.h
  template <typename T>
  T add(T a, T b) {
      return a + b;
  }
  ```

- Where possible, end (initializer) lists with a trailing comma (e.g. with structs, enums)

  ```cpp
  // Good
  enum Color {
      Red,
      Green,
      Blue,
  };
  
  // Bad
  enum Color {
      Red,
      Green,
      Blue
  };
  ```

## CMakeLists specific

- Make sure list arguments (e.g. sources, libraries) given to commands (e.g.
  `target_sources`, `target_link_libraries`) are on separate lines. This makes
  resolving merge conflicts when multiple sources were added by different
  people to the same CMakeLists.txt easier.

# Documentation

- All documentation is written in U.S. English

# Libraries

- External libraries should be included as Git submodules under the `lib/`
  subdirectory
- When adding new submodules, please set the `shallow` option to `true` in the
  [.gitmodules](./.gitmodules) file