-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathCODINGSTYLE
176 lines (135 loc) · 4.64 KB
/
CODINGSTYLE
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
AStyle
======
AStyle is a code beautifier tool. Code should be compliant with the following pattern provided by AStyle :
AStyle -r --indent=tab --indent-classes --indent-col1-comments --style=attach --keep-one-line-statements '*.hpp' '*.cpp'
Example
=======
namespace OtherNamespace {
class MyForwardDeclaredType;
}
namespace Signals {
/**
* A class that does cool stuff.
* Some general comments can be added if needed, and possibly sample code for public headers.
*/
template <typename T>
class MyClass {
public:
typedef T value_type;
enum MyEnum {EnumTypeA = 0, EnumTypeB = 1};
/**
* Public methods come first. No need to comment if not useful.
*/
explicit MyClass(const std::string &s);
~MyClass();
/**
* Document member function, but do not go overboard.
*/
void myMemberFunction() const;
/*
* NEVER use public variables; except static const POD.
*/
static const double myConst;
protected:
int myVar; //small comments like this
private:
/**
* Large comments like this
*/
std::vector<T> myVect;
};
const double MyClass::myConst = 42.0;
template <typename T>;
MyClass<T>::MyClass(const std::string &s)
: myVar(3) {
if (s.size()) {
for (int i = 0; i < s.size(); ++i) {
// ...
}
} else if (myVar) {
// ...
} else {
// ...
}
}
}
Indentation and spacing
=======================
* Tabs. Additional spaces on defines, large comments, or variable names aligning.
* public, protected, private are on the first column.
* Opening brace on the same line as function/control structure/initializer list.
* else goes on the same line as the closing brace.
* Closing brace on new line.
* Empty loops like this: while (...) {
}
* The declaring template keyword goes on its own line.
* Equal signs, and semicolumns in for loops, have a space on each side.
* There is always a space after a comma.
* Pointer is on the right: A *a; const A *a;
* Pointer on return type (and other constness) can be on the right: A* get(); const A* const;
Case
====
* Type names, template parameters and enum constants use TypeCase
* (static const) (member) variable, and (member) function names use camelCase, not under_scores.
Order
=====
* Public typedefs
* Public enums
* Public methods
* Public static const POD variables. const public variables sometimes OK.
* Protected methods
* Protected variables
* Private methods
* Private variables
* Constness
* Be (const) correct for arguments.
* Be (const) correct for member functions.
* Use either const A* const or A const* const.
Headers
=======
* They are named *.hpp for C++, *.h for C.
* Always put guards in header files: #pragma once
* Include what you use, and only what you use.
* Only forward declare if you only need a reference or pointer to type.
Lines
=====
* No line length limit. Split as few as possible. Try to be consistent.
* Avoid trailing whitespace.
* One statement per line.
* Unix '\n' ending on the repository.
Namespaces
==========
* Always use fully qualified name for standard library (e.g.using namespace std; is not allowed anywhere).
* No indent in namespaces.
Templates
=========
* If possible, put the code in a cpp file, and instantiate them explicitely.
Portability
===========
* Assume little endian.
* Don't assume type sizes (except char and unsigned char). Use for portable fixed size types.
* The code should be free of platform dependancy, except at the top of each file.
* Wrap up platform-dependant code into common APIs.
Performance
===========
* This applies mostly to the input/output/core code.
* Avoid virtual calls in perf-critical parts of the code. In this case you may use static virtual inheritance (template powa).
* Always pass objects per (const) reference (an exception for very lightweight objects such as iterators, but you should document that).
* Allocate small objects on the stack.
* Recycle objects that are on the heap. You should see new mostly in constructors and delete mostly in destructors.
* Do not return large objects. Take a reference/pointer as output.
Misc
====
* Use assert for impossible conditions. Do not put statements with side effects in asserts.
* Do not use exit() except on tests.
* Use TODO and FIXME in comments.
* Variable names should be descriptive, make them long rather than short.
* Experimental code goes between #if 0 #endif. Dead code should be removed (we'll get it back from git if necessary).
* Avoid global variables.
* Inner classes are OK. Don't abuse them.
* Use functors, not function pointers.
* Do not use the comma operator, it's mostly confusing.
* Do not overload operators too much.
* Undefined behaviour is never OK, even if its works (for the time being).
* Generally avoid gotos.
* Be warning free (in -Wall mode).