@@ -252,10 +252,10 @@ $(P
252252 Phobos and other official D source code, there are additional requirements:
253253 )
254254
255- $(UL
256- $(LI Braces should be on their own line. There are a few exceptions to this
257- (such as when declaring lambda functions), but with any normal function
258- block or type definition, the braces should be on their own line.)
255+ $(H4 Brackets)
256+ $(P Braces should be on their own line. There are a few exceptions to this
257+ (such as when declaring lambda functions), but with any normal function
258+ block or type definition, the braces should be on their own line.)
259259
260260-------------------------------
261261void func(int param)
@@ -270,12 +270,17 @@ void func(int param)
270270 }
271271}
272272-------------------------------
273-
274- $(LI Lines have a soft limit of 80 characters and a hard limit of 120
273+ $(P Avoid unnecessary parentheses:)
274+ -------------------------------
275+ (a == b) ? "foo" : "bar"; // NO
276+ a == b ? "foo" : "bar"; // OK
277+ -------------------------------
278+ $(H4 Line length)
279+ $(P Lines have a soft limit of 80 characters and a hard limit of 120
275280 characters. This means that most lines of code should be no longer than
276281 80 characters long but that they $(I can) exceed 80 characters when
277282 appropriate. However, they can $(I never) exceed 120 characters.)
278-
283+ $(LISTSECTION Whitespace,
279284 $(LI Put a space after `for`, `foreach`, `if`, and `while`: )
280285-------------------------------
281286for (…) { … }
@@ -286,7 +291,7 @@ while (…) { … }
286291do { … } while (…);
287292-------------------------------
288293 $(LI Chains containing `else if (…)` or `else static if (…)` should set the
289- keywords on the same line:)
294+ keywords on the same line:)
290295-------------------------------
291296if (…)
292297{
@@ -297,38 +302,65 @@ else if (…)
297302 …
298303}
299304-------------------------------
305+ $(LI Put a space between binary operators, assignments, `cast`, and lambdas:)
306+ -------------------------------
307+ a + b
308+ a / b
309+ a == b
310+ a && b
311+ arr[1 .. 2]
312+ int a = 100;
313+ b += 1;
314+ short c = cast(short) a;
315+ filter!(a => a == 42);
316+ -------------------------------
317+ $(LI Put no space between unary operators, after `assert`, function calls:)
318+ -------------------------------
319+ a = !a && !(2 == -1);
320+ bool b = ~a;
321+ auto d = &c;
322+ e++;
323+ assert(*d == 42);
324+ callMyFancyFunction("hello world");
325+ -------------------------------
326+ )
327+ $(LISTSECTION Imports,
328+ $(LI Local, selective imports should be preferred over global imports)
329+ $(LI Selective imports should have a space before and after the colon (`:`) like
330+ `import std.range : zip`)
331+ $(LI Imports should be sorted lexiographically.)
332+ )
333+ $(LISTSECTION Return type,
334+ $(LI The return type should be stated $(I explicitly) wherever possible,
335+ as it makes the documentation and source code easier to read.)
336+ $(LI $(LINK2 https://dlang.org/spec/struct.html#nested, Function-nested) structs
337+ (aka $(LINK2 https://wiki.dlang.org/Voldemort_types, Voldemort types))
338+ should be preferred over public `struct`s.)
339+ )
340+ $(LISTSECTION Attributes,
341+ $(LI $(I Non-templated) functions should be annotated with
342+ matching attributes (`@nogc`, `@safe`, `pure`, `nothrow`).)
343+ $(LI $(I Templated) functions should $(B not) be annotated with attributes
344+ as the compiler can infer them.)
345+ $(LI $(B Every) $(I unittest) should be annotated
346+ (e.g. `pure nothrow @nogc @safe unittest { ... }`)
347+ to ensure the existence of attributes on the templated function.)
348+ )
349+ $(LISTSECTION Templates,
300350 $(LI `unittest` blocks should be avoided in templates. They will generate
301351 a new `unittest` for each instance, hence tests should be put
302352 outside of the template.)
303- )
304- $(SUBLIST Imports,
305- $(LI Local, selective imports should be preferred over global imports)
306- $(LI Selective imports should have a space before and after the colon (`:`) like
307- `import std.range : zip`)
308- )
309- $(SUBLIST Return type,
310- $(LI The return type should be stated $(I explicitly) wherever possible,
311- as it makes the documentation and source code easier to read.)
312- $(LI $(LINK2 https://dlang.org/spec/struct.html#nested, Function-nested) structs
313- (aka $(LINK2 https://wiki.dlang.org/Voldemort_types, Voldemort types))
314- should be preferred over public `struct`s.)
315- )
316- $(SUBLIST Attributes,
317- $(LI $(I Non-templated) functions should be annotated with
318- matching attributes (`@nogc`, `@safe`, `pure`, `nothrow`).)
319- $(LI $(I Templated) functions should $(B not) be annotated with attributes
320- as the compiler can infer them.)
321- $(LI $(B Every) $(I unittest) should be annotated
322- (e.g. `pure nothrow @nogc @safe unittest { ... }`)
323- to ensure the existence of attributes on the templated function.)
324- )
353+ )
354+ $(LISTSECTION Declarations,
325355 $(LI Constraints on declarations should have the same indentation level as
326356 their declaration:)
327357-------------------------------
328- void foo(R)
358+ void foo(R)(R r)
329359if (R == 1)
330360-------------------------------
361+ )
331362
363+ $(BR)
332364$(P
333365 We are not necessarily recommending that all code follow these rules.
334366 They're likely to be controversial in any discussion on coding standards.
339371
340372Macros:
341373 TITLE=The D Style
342- SUBLIST =$(H4 $1) $(UL $+)
374+ LISTSECTION =$(H4 $1) $(UL $+)
343375
0 commit comments