-
Notifications
You must be signed in to change notification settings - Fork 0
/
coding-style.txt
254 lines (179 loc) · 6.15 KB
/
coding-style.txt
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
These coding styles are derived from the GNOME coding styles and have been
adapted to better fit with C++.
So, please read
http://developer.gnome.org/doc/guides/programming-guidelines/code-style.html
as an introduction.
Be consistent
As a rule of thumb, please try to use the style that was there before you,
otherwise the code will be non consistent.
Lines and functions length
1. Lines should be 80 characters long, at most.
Lots of people out there divide their screen in 2 vertical rectangles,
each vertical rectangle being itself divided in two horizontal rectangles.
So don't assume everybody looks at only one file at a time, full screen.
Or don't assume everybody has huge screens.
Generally speaking, be consitent. Do not make your lines longer than the
lines already written in other parts of the code.
2. Similar things applies to functions length. Be consistent with the other parts of the code.
3. No function should contain more than 4 levels of indentation.
Indenting
1. Use spaces to indent. Tabs should not appear in code files
(with the exception of files that require tabs, e.g. Makefiles).
2. The indent size is 4 spaces.
3. Code editors should be configured to expand tabs that you type to 4 spaces.
Naming conventions
1. function names are lowercase with underscores to separate words,
like this : do_something_good () .
2. Macros and constants are uppercase, with underscore to separate
words like: DO_SOMETHING () for a macro or NUMBER_OF_CHARS for a constant.
3. user defined types (classes, struct, enum names) are mixed upper and lowercase,
like class ConfManager ;
4. don't pollute the global namespace. Every symbol should at least be in the 'nemiver' namespace
(or in namespaces that are themselves in the nemiver namespace).
5. To help our fellow emacs users, don't use the keyword 'namespace'
to define namespace, like "namespace foo { //blah}", otherwise, emacs indents
//blah. Use the macro NEMIVER_BEGIN_NAMESPACE (foo) and NEMIVER_END_NAMESPACE (foo), instead.
This helps emacs not to endent the content of the namespace.
6. Generaly, C++ data members should be prefixed by m_.
Functions
1. in function definitions, function name should start on column one.
Right:
void
foo ()
{
}
Wrong:
void foo ()
{
}
2. rule 1/ does not apply in function declarations.
Right:
void foo () ;
3. function arguments (parameters) should be prefixed by a "a_" or a "an_".
Right:
void foo (int a_param) ;
void bar (int a_param, char an_other_param) ;
Wrong:
void foo (int param) ;
Braces
1. Function definitions — open and close braces should be on lines by
themselves. Do not put the open brace on the same line as the function
signature. For example:
Right:
void
foo ()
{
// do stuff
}
Wrong:
void foo () {
// do stuff
}
Wrong:
void
foo () {
// do stuff
}
2. Other braces, including for, while, do, switch statements and class
definitions — the open brace should go on the same line as the as the control structure.
Right:
for (int i = 0; i < 10; i++) {
// do stuff
}
Wrong:
for (int i = 0; i < 10; i++)
{
// do stuff
}
3. If/else statements — as above, but if there is an else clause,
the close brace should go on the same line as the else.
Right:
if (time_to_go_buy_something) {
go_buy (&something);
} else if (time_to_go_home) {
// comment on else case
gone = true;
}
Wrong:
if (time_to_go_buy_something)
{
go_buy (&something);
// comment on else case
} else if (timeToGoHome)
gone = true;
if (time_to_go_buy_something) {
}
else
// comment on else case
if (time_to_go_buy_something)
gone = true;
Parentheses
1. Function declarations and calls — use one space between the name and
the open parenthese, no space inside the parentheses, nor before commas
that separate arguments. Do use a single space after commas that
separate arguments.
Right:
int my_function (int arg1, float arg2);
void no_arg_function ();
Wrong:
int my_function(int arg1, float arg2);
int my_function ( int arg1 , float arg2 );
2. Control structures, such as if, while, do and switch — use a single
space before the open paren, but no spaces inside the parentheses.
Null, false
1. The null pointer value should be written as 0.
2. True and false values of type bool, or just generic true/false values,
should be written as true and false.
3. Tests for null pointers, false values and 0 values should all be done directly,
not through an inequality or equality comparison.
Right:
// test for true
if (foo->is_something ()) {
// code
}
// test for false
if (!foo->is_something ()) {
// code
}
// test for non-null
if (ptr) {
// code
}
// test for null
if (!ptr) {
// code
}
// test for nonzero
if (count) {
// code
}
// test for zero
if (!count) {
// code
}
Wrong:
if (foo->isSomething () == true) {
// code
}
if (foo->isSomething () != false) {
// code
}
if (p == NULL) {
// code
}
if (nil != p) {
// code
}
if (count == 0) {
// code
}
Generic stuff
1. variables of builtin types must always be initialized at
declaration time.
Right:
int foo = 0 ;
Wrong:
int foo ;
2. always provide default constructors for user defined types (classes).
If you don't want to be used with the default constructor, declare
the default constructor private.