| 1 |
Ddoc |
|---|
| 2 |
|
|---|
| 3 |
$(D_S The D Style, |
|---|
| 4 |
|
|---|
| 5 |
$(P |
|---|
| 6 |
$(I The D Style) is a set of style conventions for writing |
|---|
| 7 |
D programs. The D Style is not enforced by the compiler, it is |
|---|
| 8 |
purely cosmetic and a matter of choice. Adhering to the D Style, |
|---|
| 9 |
however, will make it easier for others to work with your |
|---|
| 10 |
code and easier for you to work with others' code. |
|---|
| 11 |
The D Style can form the starting point for a project |
|---|
| 12 |
style guide customized for your project team. |
|---|
| 13 |
) |
|---|
| 14 |
|
|---|
| 15 |
$(P |
|---|
| 16 |
Submissions to Phobos and other official D source code will |
|---|
| 17 |
follow these guidelines. |
|---|
| 18 |
) |
|---|
| 19 |
|
|---|
| 20 |
<h3>White Space</h3> |
|---|
| 21 |
|
|---|
| 22 |
$(UL |
|---|
| 23 |
$(LI One statement per line.) |
|---|
| 24 |
|
|---|
| 25 |
$(LI Use spaces instead of hardware tabs.) |
|---|
| 26 |
|
|---|
| 27 |
$(LI Each indentation level will be four columns.) |
|---|
| 28 |
|
|---|
| 29 |
$(LI Operators are separated by single spaces from their operands.) |
|---|
| 30 |
|
|---|
| 31 |
$(LI Two blank lines separating function bodies.) |
|---|
| 32 |
|
|---|
| 33 |
$(LI One blank line separating variable declarations from statements |
|---|
| 34 |
in function bodies.) |
|---|
| 35 |
) |
|---|
| 36 |
|
|---|
| 37 |
<h3>Comments</h3> |
|---|
| 38 |
|
|---|
| 39 |
$(UL |
|---|
| 40 |
$(LI Use // comments to document a single line: |
|---|
| 41 |
------------------------------- |
|---|
| 42 |
statement; // comment |
|---|
| 43 |
statement; // comment |
|---|
| 44 |
------------------------------- |
|---|
| 45 |
) |
|---|
| 46 |
|
|---|
| 47 |
$(LI Use block comments to document a multiple line block of |
|---|
| 48 |
statements: |
|---|
| 49 |
------------------------------- |
|---|
| 50 |
/* comment |
|---|
| 51 |
* comment |
|---|
| 52 |
*/ |
|---|
| 53 |
statement; |
|---|
| 54 |
statement; |
|---|
| 55 |
------------------------------- |
|---|
| 56 |
) |
|---|
| 57 |
|
|---|
| 58 |
$(LI Use $(CODE version (none)) to $(SINGLEQUOTE comment out) a piece of trial code |
|---|
| 59 |
that is syntactically valid: |
|---|
| 60 |
------------------------------- |
|---|
| 61 |
version (none) |
|---|
| 62 |
{ |
|---|
| 63 |
/* comment |
|---|
| 64 |
* comment |
|---|
| 65 |
*/ |
|---|
| 66 |
statement; |
|---|
| 67 |
statement; |
|---|
| 68 |
} |
|---|
| 69 |
------------------------------- |
|---|
| 70 |
|
|---|
| 71 |
It can be turned on with $(CODE version(all)): |
|---|
| 72 |
|
|---|
| 73 |
------------------------------- |
|---|
| 74 |
version (all) |
|---|
| 75 |
{ |
|---|
| 76 |
/* comment |
|---|
| 77 |
* comment |
|---|
| 78 |
*/ |
|---|
| 79 |
statement; |
|---|
| 80 |
statement; |
|---|
| 81 |
} |
|---|
| 82 |
------------------------------- |
|---|
| 83 |
|
|---|
| 84 |
) |
|---|
| 85 |
|
|---|
| 86 |
$(LI Use nesting comments to $(SINGLEQUOTE comment out) a piece of syntactically invalid code: |
|---|
| 87 |
------------------------------- |
|---|
| 88 |
/+++++ |
|---|
| 89 |
/* comment |
|---|
| 90 |
* comment |
|---|
| 91 |
*/ |
|---|
| 92 |
{ statement; |
|---|
| 93 |
statement; |
|---|
| 94 |
+++++/ |
|---|
| 95 |
------------------------------- |
|---|
| 96 |
) |
|---|
| 97 |
) |
|---|
| 98 |
|
|---|
| 99 |
<h3>Naming Conventions</h3> |
|---|
| 100 |
|
|---|
| 101 |
$(DL |
|---|
| 102 |
$(DT General) |
|---|
| 103 |
<dd>Names formed by joining multiple words should have each word |
|---|
| 104 |
other than the first capitalized. |
|---|
| 105 |
Names shall not begin with an underscore $(SINGLEQUOTE _) unless they are private member variables. |
|---|
| 106 |
|
|---|
| 107 |
------------------------------- |
|---|
| 108 |
int myFunc(); |
|---|
| 109 |
------------------------------- |
|---|
| 110 |
|
|---|
| 111 |
$(DT Module) |
|---|
| 112 |
$(DD Module and package names are all lower case, and only contain |
|---|
| 113 |
the characters [a..z][0..9][_]. This avoids problems dealing |
|---|
| 114 |
with case insensitive file systems.) |
|---|
| 115 |
|
|---|
| 116 |
$(DT C Modules) |
|---|
| 117 |
$(DD Modules that are interfaces to C functions go into the "c" |
|---|
| 118 |
package, for example: |
|---|
| 119 |
------------------------------- |
|---|
| 120 |
import std.c.stdio; |
|---|
| 121 |
------------------------------- |
|---|
| 122 |
Module names should be all lower case. |
|---|
| 123 |
) |
|---|
| 124 |
|
|---|
| 125 |
$(DT Class, Struct, Union, Enum, Template names) |
|---|
| 126 |
$(DD are capitalized. |
|---|
| 127 |
|
|---|
| 128 |
------------------------------- |
|---|
| 129 |
class Foo; |
|---|
| 130 |
class FooAndBar; |
|---|
| 131 |
------------------------------- |
|---|
| 132 |
) |
|---|
| 133 |
$(DD An exception is that eponymous templates that return a value instead of a type should not be |
|---|
| 134 |
capitalized, as they are conceptually more similar to functions. |
|---|
| 135 |
|
|---|
| 136 |
------------------------- |
|---|
| 137 |
template GetSomeType(T) {} |
|---|
| 138 |
template isSomeType(T) {} |
|---|
| 139 |
------------------------- |
|---|
| 140 |
) |
|---|
| 141 |
|
|---|
| 142 |
$(DT Function names) |
|---|
| 143 |
$(DD Function names are not capitalized. |
|---|
| 144 |
|
|---|
| 145 |
------------------------------- |
|---|
| 146 |
int done(); |
|---|
| 147 |
int doneProcessing(); |
|---|
| 148 |
------------------------------- |
|---|
| 149 |
) |
|---|
| 150 |
|
|---|
| 151 |
$(V1 |
|---|
| 152 |
$(DT Const names) |
|---|
| 153 |
$(DD Are in all caps.) |
|---|
| 154 |
) |
|---|
| 155 |
$(DT Enum member names) |
|---|
| 156 |
$(DD Are in lowerCamelCase.) |
|---|
| 157 |
|
|---|
| 158 |
) |
|---|
| 159 |
|
|---|
| 160 |
<h3>Meaningless Type Aliases</h3> |
|---|
| 161 |
|
|---|
| 162 |
$(P Things like:) |
|---|
| 163 |
|
|---|
| 164 |
------------------------------- |
|---|
| 165 |
alias void VOID; |
|---|
| 166 |
alias int INT; |
|---|
| 167 |
alias int* pint; |
|---|
| 168 |
------------------------------- |
|---|
| 169 |
|
|---|
| 170 |
$(P should be avoided.) |
|---|
| 171 |
|
|---|
| 172 |
<h3>Declaration Style</h3> |
|---|
| 173 |
|
|---|
| 174 |
$(P Since the declarations are left-associative, left justify them:) |
|---|
| 175 |
|
|---|
| 176 |
------------------------------- |
|---|
| 177 |
int[] x, y; // makes it clear that x and y are the same type |
|---|
| 178 |
int** p, q; // makes it clear that p and q are the same type |
|---|
| 179 |
------------------------------- |
|---|
| 180 |
|
|---|
| 181 |
$(P to emphasize their relationship. Do not use the C style:) |
|---|
| 182 |
|
|---|
| 183 |
------------------------------- |
|---|
| 184 |
int []x, y; // confusing since y is also an int[] |
|---|
| 185 |
int **p, q; // confusing since q is also an int** |
|---|
| 186 |
------------------------------- |
|---|
| 187 |
|
|---|
| 188 |
<h3>Operator Overloading</h3> |
|---|
| 189 |
|
|---|
| 190 |
$(P Operator overloading is a powerful tool to extend the basic |
|---|
| 191 |
types supported by the language. But being powerful, it has |
|---|
| 192 |
great potential for creating obfuscated code. In particular, |
|---|
| 193 |
the existing D operators have conventional meanings, such |
|---|
| 194 |
as $(SINGLEQUOTE +) means $(SINGLEQUOTE add) and $(SINGLEQUOTE <<) |
|---|
| 195 |
means $(SINGLEQUOTE shift left). |
|---|
| 196 |
Overloading operator $(SINGLEQUOTE +) with a meaning different from $(SINGLEQUOTE add) |
|---|
| 197 |
is arbitrarily confusing and should be avoided. |
|---|
| 198 |
) |
|---|
| 199 |
|
|---|
| 200 |
<h3>Hungarian Notation</h3> |
|---|
| 201 |
|
|---|
| 202 |
$(P Using hungarian notation to denote the type of a variable |
|---|
| 203 |
is a bad idea. |
|---|
| 204 |
However, using notation to denote the purpose of a variable |
|---|
| 205 |
(that cannot be expressed by its type) is often a good |
|---|
| 206 |
practice.) |
|---|
| 207 |
|
|---|
| 208 |
<h3>Documentation</h3> |
|---|
| 209 |
|
|---|
| 210 |
$(P |
|---|
| 211 |
All public declarations will be documented in |
|---|
| 212 |
$(LINK2 ddoc.html, Ddoc) format. |
|---|
| 213 |
) |
|---|
| 214 |
|
|---|
| 215 |
<h3>Unit Tests</h3> |
|---|
| 216 |
|
|---|
| 217 |
$(P |
|---|
| 218 |
As much as practical, all functions will be exercised |
|---|
| 219 |
by unit tests using unittest blocks immediately following |
|---|
| 220 |
the function to be tested. |
|---|
| 221 |
Every path of code should be executed at least once, |
|---|
| 222 |
verified by the $(LINK2 code_coverage.html, code coverage analyzer). |
|---|
| 223 |
) |
|---|
| 224 |
|
|---|
| 225 |
) |
|---|
| 226 |
|
|---|
| 227 |
Macros: |
|---|
| 228 |
TITLE=The D Style |
|---|
| 229 |
WIKI=DStyle |
|---|