GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing dlang.org — The D Style - D Programming Language Report a bug
If you spot a problem with this page, click here to create a GitHub issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page.
Requires a signed-in GitHub account. This works well for small changes.
If you'd like to make larger changes you may want to consider using
a local clone.
The D Style
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. Whitespace
Naming Conventions
Type AliasesThe D programming languages offers two functionally equivalent syntaxes for type aliases, but ... alias size_t = uint; ... is preferred over ... alias uint size_t; ... because ...
alias important = someTemplateDetail!(withParameters, andValues); alias Callback = ReturnType function(Arg1, Arg2) pure nothrow; vs. alias someTemplateDetail!(withParameters, andValues) important; alias ReturnType function(Arg1, Arg2) pure nothrow Callback; Meaningless type aliases like ... alias VOID = void; alias INT = int; alias pint = int*; ... should be avoided. Declaration StyleSince 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 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 OverloadingOperator 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. Hungarian NotationUsing 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. PropertiesFunctions 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 not alter state. Property syntaxDo not use UFCS or Optional Parentheses outside of their intended use cases. Omitting parentheses is useful in generic code that does not care whether a member is a field or a function, for example the range primitive front. 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"; } DocumentationAll public declarations will be documented in Ddoc format and should have at least Params and Returns sections. Unit TestsAs 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 code coverage analyzer. Additional Requirements for PhobosIn 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: BracketsBraces 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"; // NO a == b ? "foo" : "bar"; // OK Line lengthLines 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 can exceed 80 characters when appropriate. However, they can never exceed 120 characters. Whitespace
for (…) { … } foreach (…) { … } static foreach (…) { … } if (x) { … } static if (x) { … } while (…) { … } do { … } while (…); version (…) { … } if (…) { … } else if (…) { … } a + b a / b a == b a && b arr[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"); Imports
Return type
Attributes
Templates
Declarations
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/Struct Field Declarations
class MyClass { // bad int a; double b; // good int x; double y; } Documentation
/// A public symbol enum myFancyConstant; /** Checks whether a number is positive. `0` isn't considered as positive number. Params: number = number to be checked Returns: `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. Copyright © 1999-2026 by the D Language Foundation | Page generated by
Ddoc on Mon Nov 9 05:59:35 2026
|
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |