module style.style; // selective imports are aligned as far to the left as possible // no need to go all the way to the right of style.subcategory2.yetmorestuff below import standardlibrary.stuff : identifier; import standardlibrary.otherstuff : otherIdentifier; import style.stuff; import style.otherstuff : abcdefg; import style.subcategory.stuff; import style.subcategory.otherstuff; static import style.subcategory2.stuff; import style.subcategory2.otherstuff; public import style.subcategory2.yetmorestuff; // opening curly brackets always on the same line as what they open class Class : Interface, Interface2 { // the only case where I break nested indentation rules // because it's too much of a pain to have everything two tabs to the right except // a few publics and privates public: // indentation always with hard tabs, I have them set to 8 spaces /+ Block comments would look like this. I rarely use them, except for + "paragraph-like" text which is meant to look like text, as opposed to + just "comments". + + It's rare that code needs that kind of explanation, so most of my + comments are in the // style. +/ int i, j, k; // function names start with lowerCamelCase, class/struct names with UpperCamelCase // typedefs and enums with lowerCamelCase also // constants and enum contents ALL_UPPER_SNAKE_CASE private int foo(int l, int m, int n) { // short functions tend to be one-liners int nestedFn() { return m; } // but if statements don't usually go on the same line as their content // (see below for the case where they do) if (somthing) Do(somthing); // spaces come after language keywords like if/while/assert, not after functions // and only keywords which begin statements: not typeof, for instance // comments almost always on a line of their own, not with code if (that) { this.Foo(j,k,i); other(); } else that.Foo(k,i,j); // the only case where if statements and their contents are on the same line if (test1) { doThis(op1); doThat(op1); } if (test2) { doThis(op2); doThat(op2); } if (test3) { doThis(op3); doThat(op3); } if (test4) { doThis(op4); doThat(op4); } reallyLongFunctionCall( arg1, arg2, arg3, // empty lines here to make it easier to read // really hard to make a rule for where I put them... // where the code seems too crammed together for easy reading, I add an empty line // sometimes even very compact code doesn't need one functionCallInMiddle(i,j,k,l,m,n), callWithLongOrLotsOfParams( foothebar, adsfvanm, qwertyuiop, f(x) ), More, args, than, will, fit, nicely ); // closing bracket on its own line // align variables, always with spaces // combined with tab indentation, this means that no matter the // settings of whoever's reading the code, the indentation doesn't break int i1, i2; float f10, f20; // align initializers int a = 1; uint b = 2; long lgn = 3; // if the same type, do either: int x = 0, y = 1; // or (for no more than two types per line): int x = 0, y = 1; // or (for many types or long type/variable names): const LongTypeName longvariablename = 0, y = 1; switch (i) { case 1: caseOne(); case 2: caseOneOrTwo(); break; case 3, 4, 5: first(); while(k--) ++j; break; // short, only one statement before break, can go on one line case 9, 8: last(); break; default: throw new Error("an error"); } int delegate(int) dg = (int i) { dg = (int i) { dg = &nestedFn(); return 0; } return nestedFn(); } dg(); dg(); dg(); static if (b1) somthing(); else static if (b2) { do other(1+2+3+4+4); while (x > 2); } else static if (b3) last(); } void bar() { // prefer this if (situationBad) return error(); // to this if (situationBad) { error(); return; } } void baz() { // generally avoid starting a line with alignment // hence, align LHS differently from RHS auto oldZot = qux.zot; qux. zot = quux.zot; quux. zot = oldZot; // simple nested loops while (a) foreach (b; c) if (d) foo(); // more complex while (a) { foreach (b; c) if (d) foo(); bar(); } // similar code, easier to see like this if (x < 0) while (a--) foo(); else if (x > 0) while (a++) goo(); // than like this: if (x < 0) while (a--) foo(); else if (x > 0) while (a++) goo(); } void zot() { // complicated ifs // okay, this is going a bit overboard with the complexity, I'd probably use some well-named temporaries here // but if I had to write something like this, it'd look like this if ( // align the ||s and &&s (abc && (def & bitMask)) || foo() || ( // put the &&s far enough to the right that they can be seen easily (bar || qwerty) && ( qux - zort == 2 || zort == zork ) && boofar ) || boolean // brackets like this ) { doStuff(); doMoreStuff(); } else doOtherStuff(); } int longFunctionDefinition( int x, int y, // related parameters on one line bool b, typeof(this) c ) { scope (exit) if (b) c.foo(); return x + y; } // long strings private const char[] STRING = "this is some text which has to be placed like this to avoid getting spurious tabs in the beginning of every line" ; private const char[] ALT_STRING = "this is the other way of doing it" "if the string is 'line-oriented' I'll do this" "but if it's 'paragraph-oriented' I'll do the above" ; }