-
Notifications
You must be signed in to change notification settings - Fork 168
/
coding_style_guide.cpp
220 lines (170 loc) · 5.48 KB
/
coding_style_guide.cpp
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
//
// Realm C++ coding standard - by example
//
/*
* The layout of the code is enforced by the use of clang-format - currently in version 16. The layout is
* defined by the .clang-format configuration file in the project root. You can ensure that a code change
* complies with the formatting rules in the following ways:
*
* Before files are added to the staging area:
*
* $ git clang-format -f
*
* This will immediately modify the changes you have made to comply with the standard (if changes are needed).
*
* After you have added files to the staging area:
*
* $ git add -u
* $ git clang-format
*
* This will modify the work-area files to comply with the standard, and you can easily inspect the changes made
* by a 'git diff' command. If you are happy with the changes, add them by 'git add -u'.
*
* After you have made your commit:
*
* $ git commit
* $ git clang-format HEAD^
*
* This will modify the work-area files to comply with the standard, and you can easily inspect the changes made
* by a 'git diff' command. If you are happy with the changes, add them by 'git add -u' and amend the commit with
* 'git commit --amend'.
*
* 'git clang-format' can be used in other ways - use 'git clang-format -h' to get info.
*/
// Lines should never exceed 118 characters --------------------------------------------------------------------------
// Code should only include regular ASCII encoded characters.
// Comments may include UTF-8 encoded characters.
// Macro names use uppercase and have "REALM_" as prefix. Non-macro
// names never use all uppercase.
#define REALM_MY_MACRO 1
// A function name uses lowercase and its parts are separated by
// underscores.
my_type my_func()
{
// Put the opening brace of a function body in the next line below
// the function prototype. This also applies to class member
// functions.
// Put all other opening braces on the same line as the syntax
// element to which the brace is subordinate.
// Use 4 spaces per indentation level (no tabs please).
if (...) {
// ...
}
else {
// ...
}
// Always put subordinate statements on a new line (to ease
// debugging).
if (...)
return ...;
// No space between type and '*' or '&'
int* foo1 = ...;
int& foo2 = ...;
// 'const' goes before the type
const int foo3 = ...;
// ... but not when 'const' operates on a pointer type
const int* foo3 = ...; // 'const' operates on 'int' not 'int*'
int* const foo4 = ...; // 'const' operates on 'int*'
int* const* const foo5 = ...;
}
void my_func_2()
{
// This indentation and brace placement style agrees with K&R
// style except for the 'extra' indentation of 'cases' in a switch
// statement.
switch (...) {
case type_Foo: {
// ...
break;
}
case type_FooBar: {
// ...
break;
}
}
try {
// ...
}
catch (...) {
// ...
}
}
// A name space name uses lowercase and its parts are separated by
// underscores.
namespace my_namespace {
// No indentation inside name spaces.
// A Class name uses CamelCase with uppercase initial.
template <class T>
class MyClass : public Base {
public:
// Public member variables do not have a 'm_' prefix.
int baz;
MyClass(...)
: Base(...)
, m_bar(7)
{
// ...
}
private:
// Static member variables have prefix 's_'.
static int s_foo;
// Regular member variables have prefix 'm_'.
int m_bar;
};
} // namespace my_namespace
// Names of values of an enumeration are composed of two parts
// separated by an underscore. The first part is a common lowercase
// prefix. The second part identifies the value and uses CamelCase
// with uppercase initial.
enum mode {
mode_Foo,
mode_FooBar,
};
// Order of class members (roughly):
class MyClass2 {
public:
// Types
// Static variables
// Regular variables
// Static functions
// Regular functions
protected:
// Same as 'public'
private:
// Same as 'public'
// Friends
};
// Use literals when possible
char* str = nullptr; // don't use 0, NULL
bool enable_feature = true;
bool is_last = false;
// Use of 'auto' keyword:
//
// 'auto' should *not* be used for trivial cases where the type declaration
// is short, non-templated, and non-derived (type_t, int64_t, std::string,
// etc.
// About FIXMEs:
//
// A FIXME conveys information about a known issue or shortcoming. It
// may also include information on how to fix the problem, and on
// possible conflicts with anticipated future features.
//
// A FIXME is often added in the following situations:
//
// - While working on, or studying a particular part of the code you
// uncover an issue or a shortcoming. Additionally, you may have
// gained an understanding of how to fix it.
//
// - While implementing a new feature, you are forced to cut a corner,
// but you have some good ideas about how to continue later, and/or
// you may have knowledge about a certain planned feature that would
// require a more complete solution.
//
// A FIXME is generally not about a bug or an error, and it should
// generally not be considered a task either. Is is simply a memo to
// oneself or to some other developer who is going to work on the code
// at some later point in time.
//
// A FIXME should never be deleted unless by somebody who understands
// the meaning of it and knows that the problem is fixed, or has
// otherwise disappeared.