-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpp_coding_standard.html
376 lines (233 loc) · 8.33 KB
/
cpp_coding_standard.html
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
367
368
369
370
371
372
373
374
375
376
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<title>Sparist Coding Standard</title>
<style>
body {
font-family: "Helvetica", serif;
color: #333;
}
h1, h2, h3, h4, h5, h6, p, ul, ol, dl, li, dt, dd {
margin-bottom: 0;
margin-top: 0;
padding-bottom: 0;
padding-top: 0;
}
p, li, dt, dd {
padding-top: 10pt;
}
h1, h2, h3, h4, h5, h6 {
padding-top: 15pt;
}
dt {
font-weight: bold;
}
dt:after {
content: ":";
}
h1 {
font-size: 4.1em;
}
h2 {
font-size: 2.5em;
}
h3 {
font-size: 1.7em;
}
h4 {
font-size: 1.3em;
}
h5 {
font-size: 1.1em;
}
h6 {
font-size: 1em;
}
h2 {
counter-increment: h2;
counter-reset: h3;
}
h3 {
counter-increment: h3;
counter-reset: h4;
}
h4 {
counter-increment: h4;
counter-reset: h5;
}
h5 {
counter-increment: h5;
}
h2:before {
content:counter(h2)": ";
}
h3:before {
content:counter(h2)"."counter(h3)": ";
}
h4:before {
content:counter(h2)"."counter(h3)"."counter(h4)": ";
}
h5:before {
content:counter(h2)"."counter(h3)"."counter(h4)"."counter(h5)": ";
}
</style>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
</head>
<body>
<h1>Sparist C++ Coding Standard</h1>
<p>This document defines the coding standard for all <a href="http://sparist.com">Sparist</a> C++ projects.</p>
<h2>Rules and Recommendations</h2>
<p>All rules and recommendations defined by the <a href="https://www.securecoding.cert.org/confluence/display/cplusplus">CERT C++ Secure Coding Standard</a> must be followed.
<h2>Terms</h2>
<p>The following terms are used to represent syntactic concepts in C++ for the purposes of this document.</p>
<dl>
<dt>List</dt>
<dd>A sequence of elements delimited by one of the following sets of <dfn>delimiter symbols</dfn>:<ul>
<li><code>,</code></li>
<li><code>;</code></li>
<li><code>?</code> or <code>:</code></li>
<li><code>&&</code> or <code>||</code></li>
</ul></dd>
<dt>Enclosure</dt>
<dd>A list wrapped by one of the following sets of <dfn>enclosure symbols</dfn>:<ul>
<li><code>{</code> and <code>}</code></li>
<li><code>(</code> and <code>)</code></li>
<li><code>[</code> and <code>]</code></li>
</ul></dd>
<dt>Composite</dt>
<dd>One of the following:<ul>
<li><code>class</code></li>
<li><code>struct</code></li>
<li><code>union</code></li>
<li><code>enum</code></li>
</ul></dd>
<dt>Label</dt>
<dd>A <code>case</code> label, <code>goto</code> label, or access specifier.</dd>
</dl>
<h2>Style</h2>
<p>This section attempts to codify the style used in the code. For any case in which the rules are unclear, refer to the code.</p>
<h3>Line Indentation</h3>
<p>Indent by one tab character for each enclosure that the line is nested within.</p>
<p>Each label is considered part of an enclosure, not inside it, and has the same indentation as the enclosure symbols.</p>
<h3>Empty Lines</h3>
<p>Declarations, in declaration scope, are surrounded by an empty line.</p>
<p>Use empty lines sparingly. There should never be two consecutive empty lines.</p>
<h3>Line Breaks</h3>
<p>Rely on soft wrap for line wrapping. Line breaks only, and always, occur in the following cases:</p>
<ul>
<li>Before and after each element of a list that contains more than one element.</li>
<li>Between the declaration and definition portions of a template definition.</li>
<li>Between the declaration and definition portions of a macro definition.</li>
</ul>
<h3>Spaces</h3>
<p>Spaces occur before and after the following expression elements:</p>
<ul>
<li>Binary and ternary operators, excluding the following:<ul>
<li>Member (<code>.</code>) and pointer member (<code>-></code>) operators.</li>
<li>Comma (<code>,</code>) operators, which have a space after and not before.</li>
</ul></li>
</ul>
<p>Spaces occur before and after the following non-expression elements:</p>
<ul>
<li>Reserved words that do not act like a value (e.g. <code>this</code>, <code>nullptr</code>) or function (e.g. <code>sizeof</code>, <code>typeid</code>, *<code>_cast</code>, <code>static_assert</code>).</li>
<li>Asterisks (<code>*</code>) in pointer declarations.</li>
<li>Ampersands (<code>&</code>) in reference declarations.</li>
<li>Enclosures, excluding function parameter lists, macro parameter lists, and template argument lists.</li>
</ul>
<p>Any extraneous white-space (more than one space consecutively, or space at the end of a line) must be removed.</p>
<h3>Parentheses</h3>
<p>Use parentheses:</p>
<ul>
<li>To enclose a list (if possible), thereby allowing indentation for clarity.</li>
<li>To explicitly indicate precedence when not clearly reflected by spacing.</li>
</ul>
<h3>Modifiers/Qualifiers</h3>
<p>Each modifier/qualifier follows whatever it modifies/qualifies.</p>
<h3>Names</h3>
<p>The following rules apply to all names:</p>
<ul>
<li>Capitalize the first letter of each word, and treat acronyms as words.</li>
<li>Do not abbreviate or add extra words unnecessarily.</li>
</ul>
<p>The following additional rules apply to specific types of names.</p>
<h4>Variables</h4>
<h5>Composite</h5>
<p>Prefix with <code>this</code>.</p>
<h5>Function</h5>
<p>Prefix with <code>the</code>.</p>
<h4>Template Parameters</h4>
<h5>Composite</h5>
<p>Prefix with <code>This</code>.</p>
<h5>Function</h5>
<p>Prefix with <code>The</code>.</p>
<h4>Macros</h4>
<p>Macro names end in an underscore.</p>
<p>Namespace membership is enforced by prefixing the macro name with the namespace names (each followed by an underscore).</p>
<h3>Files</h3>
<p>File names are lower case, with spaces replaced with underscores.</p>
<p>Each file contains one public declaration for which the file is named.</p>
<h3>Folders</h3>
<p>Each namespace is a folder containing everything in the namespace.</p>
<h3>Classes</h3>
<h4>Keyword</h4>
<p>Use <code>class</code> keyword for C++-style classes. Use <code>struct</code> and <code>union</code> for C-style composites.</p>
<h4>Access Specifiers</h4>
<p>Always use explicit access specifiers for base classes and members.</p>
<h4>Declaration Order</h4>
<h5>Base Classes</h5>
<h6>Primary</h6>
<p>Ordered for optimal memory layout.</p>
<h6>Secondary</h6>
<p>Ordered by:</p>
<ol>
<li>Public</li>
<li>Protected</li>
<li>Private</li>
</ol>
<h6>Tertiary</h6>
<p>Ordered alphanumerically.</p>
<h5>Members</h5>
<h6>Primary</h6>
<p>Ordered by:</p>
<ol>
<li>Friends</li>
<li>Public</li>
<li>Protected</li>
<li>Private</li>
</ol>
<h6>Secondary</h6>
<p>Ordered by:</p>
<ol>
<li>Static</li>
<li>Non-Static</li>
</ol>
<h6>Tertiary</h6>
<p>Ordered by:</p>
<ol>
<li>Types</li>
<li>Destructor</li>
<li>Constructors</li>
<li>Operator Overloads</li>
<li>Functions</li>
<li>Values (ordered for optimal memory layout)</li>
</ol>
<h6>Quaternary</h6>
<p>Ordered alphanumerically.</p>
<h3>Functions</h3>
<p>Declare functions in the header file.</p>
<p>Define functions in the source file with <code>inline</code>, and include the source file in the header file. Check if the include guard is defined to determine whether the source file is being included by the compiler (not defined) or the header file (defined).</p>
<p>Always mark virtual functions as <code>virtual</code> (for <a href="http://doxygen.org">Doxygen</a>).</p>
<h3>Strings</h3>
<p>All strings are <a href="http://utf8everywhere.org">UTF-8</a> unless impossible. Exceptions must be clearly documented.</p>
<h3>Conditionals</h3>
<p>Always enclose the conditional statement body in curly braces, even if the body is empty.</p>
<h3>Comments</h3>
<p>Use line-style comments for full-line comments, and block-style comments for inline comments.</p>
<p>Use line-style Doxygen comments for commands that act on the code itself (e.g. <code>\cond</code>), and block-style Doxygen comments for documentation.</p>
<h3>IDE Markup</h3>
<p>Use Xcode <code>MARK</code> markup. Refer to existing code to see how it is used.</p>
</body>
</html>