
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.
Submissions to Phobos and other official D source code will follow these guidelines.
int myFunc();string myLocalVar;import std.algorithm;class Foo;struct FooAndBar;
template GetSomeType(T) {alias GetSomeType = T; }template isSomeType(T) {enum isSomeType =is(T == SomeType); }template MyType(T) {struct MyType { ... } }template map(fun...) {auto map(Range r) { ... } }
int done();int doneProcessing();
enum secondsPerMinute = 60;immutable hexDigits ="0123456789ABCDEF";
enum Direction { bwd, fwd, both }enum OpenRight { no, yes }
enum Attribute { nothrow_, pure_, safe }class UTFException;ubyte asciiChar;
struct Foo {}// this struct follows the regular naming conventions// this struct is only intended to be used as an UDA and therefore overrides the// regular naming conventions for structsstruct name { string value; }@name("bar") Foo foo;
The D programming languages offers two functionally equivalent syntaxes for type aliases, but ...
alias size_t =uint;
... is preferred over ...
aliasuint size_t;
... because ...
alias important = someTemplateDetail!(withParameters, andValues);alias Callback = ReturnTypefunction(Arg1, Arg2)purenothrow;
vs.
alias someTemplateDetail!(withParameters, andValues) important;alias ReturnTypefunction(Arg1, Arg2)purenothrow Callback;
Meaningless type aliases like ...
alias VOID =void;alias INT =int;alias pint =int*;
... should be avoided.
Since the declarations are left-associative, left justify them:
int[] x, y;// makes it clear that x and y are the same typeint** p, q;// makes it clear that p and q are the same type
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**
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 ‘+’ means ‘add’ and ‘<<’ means ‘shift left’. Overloading operator ‘+’ with a meaning different from ‘add’ is arbitrarily confusing and should be avoided.
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.
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 shouldnot alter state.
Do not useUFCS orOptional Parentheses outside of their intended use cases.Omitting parentheses is useful in generic code that does not care whether a member is a field ora function, for example the range primitivefront.It can also make a chain of range functions more compact.However, when a simple call is made to a function with side effects, prefer 'regular' function call syntax.
import std.range, std.stdio;void main(){// good writeln(); writeln("hello"); iota(0, 10).dropOne.array.front.writeln;// bad writeln;"hello".writeln; writeln ="hello";}
All public declarations will be documented inDdoc format and should have at leastParams andReturns sections.
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 thecode coverage analyzer.
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 additional requirements:
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 { ... }}
Avoid unnecessary parentheses:
(a == b) ?"foo" :"bar";// NOa == b ?"foo" :"bar";// OK
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 theycan exceed 80 characters when appropriate. However, they cannever exceed 120 characters.
for (…) { … }foreach (…) { … }staticforeach (…) { … }if (x) { … }staticif (x) { … }while (…) { … }do { … }while (…);version (…) { … }
if (…){ …}elseif (…){ …}
a + ba / ba == ba && barr[1 .. 2]int a = 100;b += 1;short c =cast(short) a;filter!(a => a == 42);
a = !a && !(2 == -1);bool b = ~a;auto d = &c;e++;assert(*d == 42);callMyFancyFunction("hello world");
void foo(R)(R r)if (R == 1)
// Prefer:T transmogrify(T)(T value)if (isIntegral!T)in (value % 7 == 0)out (result; result % 11 == 0){// ...}// over this:T transmogrify(T)(T value)if (isIntegral!T)in {assert(value % 7 == 0); }out (result) {assert(result % 11 == 0); }do{// ...}
struct S{int x;invariant (x > 0);}
class MyClass{// badint a;double b;// goodint x;double y;}
/// A public symbolenum myFancyConstant;
/**Checks whether a number is positive.`0` isn't considered as positive number.Params: number = number to be checkedReturns: `true` if the number is positive, `0` otherwise.See_Also: $(LREF isNegative)*/bool isPositive(int number){return number > 0;}
We are not necessarily recommending that all code follow these rules. They're likely to be controversial in any discussion on coding standards. However, they are required in submissions to Phobos and other official D source code.