-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCODING
218 lines (146 loc) · 5.35 KB
/
CODING
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
Coding Standards for Marble
===========================
This file contains the coding standards for Marble. If you want to
add or change code in Marble, you must follow these standards.
Foundation
----------
The foundation for these standards is the kdelibs coding style. It is
described here: http://techbase.kde.org/Policies/Kdelibs_Coding_Style
I suggest that you start by reading that document, it's not long.
Recapitulation
--------------
Let's recapitulate a few key points from the kdelibs coding
style. This is not the full standard, but just the most important
points.
- 4 spaces indentation
- no tabs in the source code
- opening braces at end of line (except struct, class, functions and
namespaces)
- as little abbreviation in variable names as possible
- one variable declaration per line
- whitespace after control statements
- no space after pointer or reference ('*foo', not '* foo')
- no lines longer than 100 chars.
That's a very short recapitulation of the above mentioned document.
The full document contains lots of examples to show how it should be
done.
Class names and Variables
-------------------------
- Class names should have the ("namespace") prefix "Marble" if
* they are parts of the Marble library that get exported.
* they resemble widgets or similar visually exposed items
The remaining part of the name should reflect the purpose of the
class and should be camel cased with the first letter being upper
cased.
All other classes should not have the "Marble" prefix.
- All header files should contain an include guard which prevents from
including a header file more than once. The include guard name consists
of the Marble namespace prefix (if the class is part of the Marble
namespace), the name of the class and an H. The name is in full upper case
and separated with an underscore.
Correct:
#ifndef MARBLE_ROUTINGWIDGET_H
#define MARBLE_ROUTINGWIDGET_H
...
#endif // MARBLE_ROUTINGWIDGET_H
Wrong:
MARBLE_ROUTING_WIDGET_H
MARBLEROUTINGWIDGET_H
ROUTINGWIDGET_H
ROUTING_WIDGET_H
- camelCasing with the first letter being lower cased should be used
for methods and variables (e.g. myMethodName()). Full
upper cased names should be used for constants (e.g. RAD2DEG).
Extensions
----------
The style defined above is not complete, so we have added the
following item:
Broken Lines in Expressions
- - - - - - - - - - - - - -
If an expression is so long that the line has to be broken, the break
should occur *in front of* an operator, not *behind* it.
Example:
var = very_long_sub_expression
+ another_very_long_sub_expression;
If you use emacs, you can make it auto-indent by adding parenthesis to
the expression like this:
var = ( very_long_sub_expression
+ another_very_long_sub_expression );
Another common case for this is logical expressions for if statements:
if ( very_long_sub_expression
&& another_very_long_sub_expression )
doSomething();
See also below for how to handle braces in this case.
Exceptions
----------
The Marble coding style has a couple of exceptions to the foundation.
Spaces and Parenthesis
- - - - - - - - - - -
Due to historic reasons, the coding style of Marble has one
exception to the kdelibs style. That exception is that we use the Qt
way of using parenthesis, with a space inside both opening and closing
parenthesis. For example
Correct:
if ( width > 100 ) {
setHeight( 200 );
}
Wrong:
if (width > 100) {
setHeight(200);
}
Braces After Multi-line Expressions
- - - - - - - - - - - - - - - - - -
When an 'if' or 'while' statement has a long logical expression, the
following braces should be put at the beginning of the next line to
increase readability.
Correct:
if ( very_long_sub_expression
&& another_very_long_sub_expression )
{
doSomething();
doSomethingElse();
}
Wrong:
if ( very_long_sub_expression
&& another_very_long_sub_expression ) {
doSomething();
doSomethingElse();
}
As you can see, it is somewhat difficult to spot where the expression
ends and the action part begins if you just look at the left side.
Special considerations for Marble
---------------------------------
Some things are only applicable for Marble.
Abbreviations
- - - - - - -
Use the following abbreviations in variable names and comments:
lon longitude (not lng!)
lat latitude
As parameters (and preferably in other places as well)
longitude and latitude should appear in lon-lat order
(not lat-lon!).
Settings for Your Editor
------------------------
emacs
- - -
If you are an emacs user, you can put the following lines in your
.emacs file. Then these style guidelines will be followed almost
automatically:
;; Don't use tabs in indentation
(setq-default indent-tabs-mode nil)
;; Use 4 space indentation
(setq-default c-basic-offset 4)
kate
- -
Under "Settings -> Configure Kate" adjust following parameters:
- Check "Insert spaces instead of tabulators" on "Editing" page
- Set "Tab width" to 4 on "Editing" page
- Set "Encoding" to "Unicode ( utf-8 )" on "Open/Save" page
- Set "End of line" to "Unix" on "Open/Save" page
vim
- -
If you are a vim user, add the following to your ~/.vimrc file.
:set cindent
:set tabstop=4
:set shiftwidth=4
:set expandtab