Project

General

Profile

Coding Styleguide » History » Version 1

quintus, 03/20/2020 10:46 PM

1 1 quintus
# Coding Styleguide
2
3
Those who have worked on the TSC project will remember the dreaded discussion about coding style. This project will avoid the trap by using a consistent coding style right from the start. It is outlined in this document.
4
5
The game's source code is formatted according to the [1TBS](https://en.wikipedia.org/wiki/Indentation_style#Variant:_1TBS_(OTBS)) style, which is a variant of K&R, with slight adjustments. Details are documented below.
6
7
In general, try to keep your code readable. Specifically, it is often useful to leave empty lines to separate logically grouped statements from one another.
8
9
## Indentation, line length
10
11
Source code is indented with 4 spaces, tabs are not used. Lines should be broken around 80 characters, and the resulting continuation lines should be indented so that it makes sense to look at. Example:
12
13
~~~~~ c++
14
if (somecondition) {
15
    thisIsAVeryLongFunctionName(this_is_a_parameter,
16
                                this_is_another_parameter,
17
                                third_parameter);
18
}
19
~~~~~
20
21
## Commentary
22
23
Semantically, comments should normally reflect why code is written the way it is. Normally it is not required to explain what code does, unless it is an exceptionally complex part. Syntactically, use `//` for one and two lines of comments. Starting with the third line, comments should use the bock syntax `/* ... */`. In the block syntax, align each star with the one on the preceeding line. Terminate the comment block on the last line of the comment.
24
25
~~~~~ c++
26
// Careful: this is used in foobar() as well
27
28
/* This code is designed to specifically fit the purpose of an example.
29
 * It was not at all easy to come up with all this text, but for the
30
 * sake of an example, it was required to do so. */
31
~~~~~
32
33
### Documentation comments
34
35
Where Doxygen is used to generate documentation, use Doxygen's `///` markers for short and the `/**` markers for long documentation. Other than that, the above advice applies.
36
37
## Case of identifiers
38
39
* Macros are ALL_IN_CAPS.
40
* Structure and class identifiers use CamelCase.
41
* Member function identifiers are CamelCase as well.
42
* Member variables are snake_case. This includes static member variables, even if they are constant.
43
* Local variables use snake_case.
44
45
## Abbreviated Hungarian Notation
46
47
Identifiers of variables and constants begin with a short sequence of characters that encodes some important information about the variable in question. This is called [Hungarian Notation](https://en.wikipedia.org/wiki/Hungarian_notation), but in full, it is cumbersome to read and leads to long identifier names. The following prefix characters have been chosen with respect to two goals: Make variable scope immediately visible, and warn of "unusual" types.
48
49
| Prefix | Meaning                                                          |
50
|--------+------------------------------------------------------------------|
51
|        | No prefix: Local variable                                        |
52
| m      | Member variable                                                  |
53
| s      | File-local variable                                              |
54
| g      | Global variable                                                  |
55
| p      | Variable holds a pointer (both raw and managed pointers)         |
56
| a      | Variable holds a raw array (not: vector or other C++ containers) |
57
58
The scope prefix comes before the type prefix. Thus, `mp_foo` is a member variable holding a pointer, and `ga_argv` is a global variable holding a raw C array.
59
60
## Compound Statements
61
62
The opening brace resides on the same line as the statement it applies to, regardless of whether this is a function, a control flow statement, or a class or enum declaration. The closing brace has a line on its own to ensure it is easily spottable where a block ends.
63
64
~~~~~~~~~~~ c++
65
class Foo {
66
};
67
68
if (condition) {
69
    // ...
70
}
71
72
while (condition) {
73
    // ...
74
}
75
76
void main() {
77
    // ...
78
}
79
~~~~~~~~~~~
80
81
The rare case of a terminal `while` has the `while` after the closing brace on the same line.
82
83
~~~~~~ c++
84
{
85
    // ...
86
} while (condition)
87
~~~~~~~
88
89
## Brace Cuddling
90
91
In an if/elsif/else statement, braces are cuddled to keep code compact.
92
93
~~~~~~~~~~ c++
94
if (condition1) {
95
    // ...
96
} else if (condition2) {
97
    // ...
98
} else {
99
    // ...
100
}
101
~~~~~~~~~~~
102
103
## Braces around short statements
104
105
It is okay to leave breaces out for one-line `if`, `while`, etc. statements. If the surrounding code grows sufficiently complex so that the entirety of such a statement cannot be determined with a single look (e.g. long `else` conditional), use braces even in those parts where the syntax allows to leave the braces out. It helps clarity in that case.
106
107
~~~~~~~~~ c++
108
// Short: good
109
if (condition)
110
    doit();
111
112
// Longer, but still okay
113
if (condition1)
114
    doit();
115
else
116
    doother();
117
118
// This is too complex, use braces everywhere to clarify
119
if (condition1) {
120
    doit();
121
} else {
122
    doother1();
123
    if (condition2) {
124
        something();
125
    }
126
    else {
127
        andmore();
128
    }
129
}
130
~~~~~~~~~
131
132
## Parantheses and spacing
133
134
Between a keyword and the opening paranthesis is exactly one space. Between the closing paranthesis and the opening curly brace is exactly one space as well. *There is no space between a function name and the opening paranthesis of its argument list*, neither in declaration nor in calling of a function.
135
136
~~~~~~~~ c++
137
void foo() { // No space between function name and (, but one space between ) and {
138
    if (condition) { // One space between keyword if and (, and one space between ) and {
139
        // ...
140
    }
141
}
142
~~~~~~~~