| | 1 | == Indenting == |
| | 2 | |
| | 3 | * Use spaces to indent. Tabs should not appear in code files (with the exception of files that require tabs, e.g. Makefiles). We have a Subversion pre-commit script that enforces this rule for most source files, preventing commits of files that don't follow this rule. |
| | 4 | * The indent size is 4 spaces. |
| | 5 | * Code editors should be configured to expand tabs that you type to 4 spaces. |
| | 6 | |
| | 7 | === Braces === |
| | 8 | |
| | 9 | * Function definitions — open and close braces should be on lines by themselves. Do not put the open brace on the same line as the function signature. |
| | 10 | |
| | 11 | Right: |
| | 12 | {{{ |
| | 13 | #!cpp |
| | 14 | void foo() |
| | 15 | { |
| | 16 | // do stuff |
| | 17 | } |
| | 18 | }}} |
| | 19 | |
| | 20 | Wrong: |
| | 21 | {{{ |
| | 22 | #!cpp |
| | 23 | void foo() { |
| | 24 | // do stuff |
| | 25 | } |
| | 26 | }}} |
| | 27 | |
| | 28 | * Other braces, including for, while, do, switch statements and class definitions — the open brace should go on the same line as the as the control structure. |
| | 29 | |
| | 30 | Right: |
| | 31 | {{{ |
| | 32 | #!cpp |
| | 33 | for (int i = 0; i < 10; i++) { |
| | 34 | // do stuff |
| | 35 | } |
| | 36 | }}} |
| | 37 | |
| | 38 | Wrong: |
| | 39 | {{{ |
| | 40 | #!cpp |
| | 41 | for (int i = 0; i < 10; i++) |
| | 42 | { |
| | 43 | // do stuff |
| | 44 | } |
| | 45 | }}} |
| | 46 | |
| | 47 | |
| | 48 | * If/else statements — as above, but if there is an else clause, the close brace should go on the same line as the else. Also, one-line if or else clauses should not get braces. |
| | 49 | |
| | 50 | Right: |
| | 51 | {{{ |
| | 52 | #!cpp |
| | 53 | if (timeToGetCoffee) { |
| | 54 | buyCoffee(&coffee); |
| | 55 | chugIt(coffee); |
| | 56 | } else if (timeToGoHome) |
| | 57 | // comment on else case |
| | 58 | outtaHere = true; |
| | 59 | }}} |
| | 60 | |
| | 61 | Wrong: |
| | 62 | {{{ |
| | 63 | #!cpp |
| | 64 | if (timeToGetCoffee) |
| | 65 | { |
| | 66 | buyCoffee(&coffee); |
| | 67 | chugIt(coffee); |
| | 68 | // comment on else case |
| | 69 | } else if (timeToGoHome) |
| | 70 | { |
| | 71 | outtaHere = true; |
| | 72 | } |
| | 73 | |
| | 74 | if (timeToGetCoffee) { |
| | 75 | } |
| | 76 | else |
| | 77 | |
| | 78 | // comment on else case |
| | 79 | |
| | 80 | if (timeToGoHome) |
| | 81 | outtaHere = true; |
| | 82 | }}} |