Icy Labs Style Guide
D Syntax
Types and Scoping
Use of 'auto'
Only use auto to define variables initialised with a literal, new class instance or struct OpCall:
auto myFoo = new Foo(); // new class instance auto myBarString = "bar"; // literal auto myBaz = Baz(); // struct OpCall
Do not use auto to define variables whose values are returned from methods
// Do not: auto mySomething = getThing();
Do not use auto when the type is not immediately obvious
Nested Classes
- Use module scope in preference to nested classes
Only use a nested class when it is only used by the enclosing class
- Do not make nested classes public unless part of the interface (e.g. a container for a set of options)
Local Variables
- Place a method's variables in the narrowest scope possible
- Initialise on declaration as close as possible to first usage
Classes
Doing Work in Constructors
Default Constructors
Explicit Constructors
Structs vs Classes
Inheritance
Interfaces
Operator Overloading
Access Control
Declaration Order
Length of Methods
Naming
General Naming Rules
Filenames
Type Names
Variable Names
Constant Names
Method Names
Template Names
Interface Names
Module Names
Comments
Comment Style
File Comments
Class Comments
Method Comments
Variable Comments
int foo; // this represents the amount of fooage
Implementation Comments
- Introduction before code block
Description of a condition or argument as first line inside code block
// searches for a red needle
// amongst a collection of needles
foreach ( needle; otherNeedles ) {
// looks at each needle in the collection of needles 'otherNeedles'
if ( needle.isRed ) {
// a red needle was found
writefln( "Red needle Found!" );
} else {
// a red needles wasn't found, keep looking
writefln( "Still looking!" );
}
}
Punctuation, Spelling and Grammar
TODO comments
Formatting and Statements
Parenthesis and Horizontal Whitespace
- Pad parentheses used for statement structure (conditionals, loops, method calls, method definitions) with 1 space
- Do not pad grouping parentheses
void someMethod( int argument ) {
}
callSomeMethod( 42 );
int foo = ((6 * 8) - 5);
if ( condition ) {
}- Function/Method parentheses have no preceding space and 1 space before opening {
- Conditional/Loop parentheses have 1 preceding space and 1 space before opening {
Line Length
Non-ASCII Characters
Spaces vs Tabs
- Indentation with Tabs
Method Declarations and Definitions
Calls to Methods
Conditionals
Space after if keyword (to differentiate from method calls)
- Pad interior with 1 space
- Trailing space before opening parenthesis
if ( condition ) {
}
Loops
Space after while or for keywords
- Pad interior with 1 space
- Trailing space before opening parenthesis
while ( condition ) {
}
for ( init; condition; update ) {
}For loops to be used when the same single variable is defined in the initialisation then referenced in condition and update (prevent counter variables from escaping scope)
- Preference towards forward rolling for-loops of the form using != and post-incremental ++:
for ( int i = 0; i != length; i++ ) {
}if <= or !=..+1 is used in for-loop condition, variable compared against must contain "maximum", "max" or semantic equivalent (to explicitly state inclusion)
- ++i increment standard for microcontroller programming
Switch
- always use final switch if all cases are to be visited
- code blocks around multi-statement cases (break can like outside?)
Boolean Expressions
Return Values
Variable Initialisation
Class Format
Vertical Whitespace
![icy [labs]](/moin_static171/common/wikilogo.png)