forked from dlang/dlang.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dstyle.dd
315 lines (251 loc) · 8.54 KB
/
dstyle.dd
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
Ddoc
$(D_S The D Style,
$(P
$(I The D Style) is a set of style conventions for writing
D programs. The D Style is not enforced by the compiler. It is
purely cosmetic and a matter of choice. Adhering to the D Style,
however, will make it easier for others to work with your
code and easier for you to work with others' code.
The D Style can form the starting point for a project
style guide customized for your project team.
)
$(P
Submissions to Phobos and other official D source code will
follow these guidelines.
)
$(H3 Whitespace)
$(UL
$(LI One statement per line.)
$(LI Use spaces instead of hardware tabs.)
$(LI Each indentation level will be four columns.)
)
$(H3 Comments)
$(UL
$(LI Use // comments to document a single line:
-------------------------------
statement; // comment
statement; // comment
-------------------------------
)
$(LI Use block comments to document a multiple line block of
statements:
-------------------------------
/* comment
* comment
*/
statement;
statement;
-------------------------------
)
$(LI Use $(CODE version (none)) to $(SINGLEQUOTE comment out) a piece of trial code
that is syntactically valid:
-------------------------------
version (none)
{
/* comment
* comment
*/
statement;
statement;
}
-------------------------------
It can be turned on with $(CODE version(all)):
-------------------------------
version (all)
{
/* comment
* comment
*/
statement;
statement;
}
-------------------------------
)
$(LI Use nesting comments to $(SINGLEQUOTE comment out) a piece of syntactically invalid code:
-------------------------------
/+++++
/* comment
* comment
*/
{ statement;
statement;
+++++/
-------------------------------
)
)
$(H3 Naming Conventions)
$(DL
$(DT General)
$(DD Unless listed otherwise below, names should be camelCased (this
includes $(I all) variables). So, names formed by joining multiple
words have each word other than the first word capitalized. Also, names
do not begin with an underscore $(SINGLEQUOTE _) unless they are
private.
-------------------------------
int myFunc();
string myLocalVar;
-------------------------------
)
$(DT Modules)
$(DD Module and package names should be all lowercase, and only contain
the characters [a..z][0..9][_]. This avoids problems when dealing with
case-insensitive file systems.
-------------------------------
import std.algorithm;
-------------------------------
)
$(DT Classes, Structs, Unions, Enums, Templates)
$(DD The names of user-defined types should be PascalCased, which is the
same as camelCased except that the first letter is uppercase.
-------------------------------
class Foo;
struct FooAndBar;
-------------------------------
)
$(DD An exception is that eponymous templates that return a value instead of
a type should not be capitalized, as they are conceptually more similar
to functions.
-------------------------
template GetSomeType(T) { alias T GetSomeType; }
template isSomeType(T) { enum isSomeType = is(T == SomeType); }
-------------------------
)
$(DT Functions)
$(DD Function names should be camelCased, so their first letter is lowercase.
This includes properties and member functions.
-------------------------------
int done();
int doneProcessing();
-------------------------------
)
$(DT Constants)
$(DD The names of constants should be camelCased just like normal variables.
-------------------------------
enum secondsPerMinute = 60;
immutable hexDigits = "0123456789ABCDEF";
-------------------------------
)
$(DT Enum members)
$(DD The members of enums should be camelCased, so their first letter is
lowercase.
-------------------------------
enum Direction { bwd, fwd, both }
enum OpenRight { no, yes }
-------------------------------
)
$(DT Keywords)
$(DD If a name would conflict with a keyword, and it is desirable to use the
keyword rather than pick a different name, a single underscore
$(SINGLEQUOTE _) should be appended to it. Names should not be
capitalized differently in order to avoid conflicting with keywords.
-------------------------------
enum Attribute { nothrow_, pure_, safe }
-------------------------------
)
$(DT Acronyms)
$(DD When acronyms are used in symbol names, all letters in the acronym
should have the same case. So, if the first letter in the acronym
is lowercase, then all of the letters in the acronym are lowercase, and
if the first letter in the acronym is uppercase, then all of the
letters in the acronym are uppercase.
-------------------------------
class UTFException;
ubyte asciiChar;
-------------------------------
)
)
$(H3 Meaningless Type Aliases)
$(P Things like:)
-------------------------------
alias void VOID;
alias int INT;
alias int* pint;
-------------------------------
$(P should be avoided.)
$(H3 Declaration Style)
$(P Since the declarations are left-associative, left justify them:)
-------------------------------
int[] x, y; // makes it clear that x and y are the same type
int** p, q; // makes it clear that p and q are the same type
-------------------------------
$(P to emphasize their relationship. Do not use the C style:)
-------------------------------
int []x, y; // confusing since y is also an int[]
int **p, q; // confusing since q is also an int**
-------------------------------
$(H3 Operator Overloading)
$(P Operator overloading is a powerful tool to extend the basic
types supported by the language. But being powerful, it has
great potential for creating obfuscated code. In particular,
the existing D operators have conventional meanings, such
as $(SINGLEQUOTE +) means $(SINGLEQUOTE add) and $(SINGLEQUOTE <<)
means $(SINGLEQUOTE shift left).
Overloading operator $(SINGLEQUOTE +) with a meaning different from $(SINGLEQUOTE add)
is arbitrarily confusing and should be avoided.
)
$(H3 Hungarian Notation)
$(P Using hungarian notation to denote the type of a variable
is a bad idea.
However, using notation to denote the purpose of a variable
(that cannot be expressed by its type) is often a good
practice.)
$(H3 Properties)
$(P Functions should be property functions whenever appropriate. In
particular, getters and setters should generally be avoided in favor of
property functions. And in general, whereas functions should be verbs,
properties should be nouns, just like if they were member variables.
Getter properties should $(I not) alter state.)
$(H3 Documentation)
$(P
All public declarations will be documented in
$(LINK2 ddoc.html, Ddoc) format.
)
$(H3 Unit Tests)
$(P
As much as practical, all functions will be exercised
by unit tests using unittest blocks immediately following
the function to be tested.
Every path of code should be executed at least once,
verified by the $(LINK2 code_coverage.html, code coverage analyzer).
)
$(H2 Additional Requirements for Phobos)
$(P
In general, this guide does not try to recommend or require that code
conform to any particular formatting guidelines. The small section on
whitespace at the top contains its only formatting guidelines. However, for
Phobos and other official D source code, there are two additional
requirements:
)
$(UL
$(LI Braces should be on their own line. There are a few exceptions to this
(such as when declaring lambda functions), but with any normal function
block or type definition, the braces should be on their own line.)
-------------------------------
void func(int param)
{
if(param < 0)
{
...
}
else
{
...
}
}
-------------------------------
$(LI Lines have a soft limit of 80 characters and a hard limit of 120
characters. This means that most lines of code should be no longer than
80 characters long but that they $(I can) exceed 80 characters when
appropriate. However, they can $(I never) exceed 120 characters.)
)
$(P
We are not necessarily recommending that all code follow these two rules.
They're both likely to be controversial in any discussion on coding
standards. However, they are required in submissions to Phobos and other
official D source code.
)
)
Macros:
TITLE=The D Style
WIKI=DStyle
CATEGORY_APPENDICES=$0