[392] | 1 |
|
---|
[605] | 2 | Coding Style Guidelines
|
---|
| 3 | ========================
|
---|
[392] | 4 |
|
---|
[459] | 5 | Line lengths
|
---|
| 6 |
|
---|
| 7 | Try to keep lines less than 80 characters.
|
---|
| 8 |
|
---|
[605] | 9 | Break lines after logical separators.
|
---|
[459] | 10 |
|
---|
[605] | 11 | Vertically align like constructs.
|
---|
[459] | 12 |
|
---|
[605] | 13 | func(arg1,
|
---|
| 14 | arg2);
|
---|
| 15 |
|
---|
[392] | 16 | Indents
|
---|
| 17 |
|
---|
[459] | 18 | Place function definition { in column 1
|
---|
[392] | 19 |
|
---|
[459] | 20 | Place function definition } in column 1
|
---|
[392] | 21 |
|
---|
[605] | 22 | Indent nested constructs by 2 characters
|
---|
[392] | 23 |
|
---|
[459] | 24 | void func()
|
---|
| 25 | {
|
---|
[605] | 26 | statement;
|
---|
[459] | 27 | if (expr)
|
---|
[605] | 28 | statement2;
|
---|
[459] | 29 | }
|
---|
[392] | 30 |
|
---|
[459] | 31 | Tabs
|
---|
[392] | 32 |
|
---|
[605] | 33 | Use only 8 column tabs.
|
---|
[392] | 34 |
|
---|
[459] | 35 | Layouts
|
---|
| 36 |
|
---|
| 37 | if (expr)
|
---|
[605] | 38 | statement;
|
---|
[459] | 39 |
|
---|
[605] | 40 | // If expr and statement are brief single line is OK.
|
---|
| 41 | if (expr) statement;
|
---|
[459] | 42 |
|
---|
[1776] | 43 | // If statement is long, split logically and use braces.
|
---|
[605] | 44 | if (expr) {
|
---|
| 45 | statement_part1...
|
---|
| 46 | restoflongstatement;
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | if (expr)
|
---|
| 50 | statement;
|
---|
| 51 | else
|
---|
| 52 | statement2;
|
---|
| 53 |
|
---|
[459] | 54 | if (expr1) {
|
---|
[605] | 55 | statement1;
|
---|
| 56 | statement2;
|
---|
[459] | 57 | }
|
---|
[605] | 58 | else if (expr2) {
|
---|
| 59 | }
|
---|
[459] | 60 | else {
|
---|
| 61 | }
|
---|
| 62 |
|
---|
[605] | 63 | // If expression wrapped, align like constructs vertically.
|
---|
[459] | 64 | if (expr1 &&
|
---|
| 65 | expr2))
|
---|
| 66 | {
|
---|
| 67 | }
|
---|
| 68 |
|
---|
[605] | 69 | // Prefer expression sense that places shorter statement list first.
|
---|
| 70 | if (!expr)
|
---|
| 71 | ReportError();
|
---|
| 72 | else {
|
---|
| 73 | statement1;
|
---|
| 74 | ...;
|
---|
| 75 | statementn;
|
---|
| 76 | }
|
---|
| 77 |
|
---|
[459] | 78 | while (expr) {
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | for (expr; expr; expr) {
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | typedef struct {
|
---|
| 85 |
|
---|
| 86 | } TYPENAME;
|
---|
| 87 |
|
---|
| 88 | Variable definitions
|
---|
| 89 |
|
---|
[605] | 90 | Define one variable per line. They are easier to find and understand.
|
---|
[459] | 91 |
|
---|
| 92 | INT a;
|
---|
[605] | 93 | INT *pb;
|
---|
[459] | 94 |
|
---|
| 95 | not
|
---|
| 96 |
|
---|
[605] | 97 | INT a,*b;
|
---|
[459] | 98 |
|
---|
[605] | 99 | Variable naming
|
---|
| 100 |
|
---|
| 101 | Prefer Hungarian notation and CamelCaps for global variables.
|
---|
| 102 |
|
---|
| 103 | BOOL fAGlobalFlag;
|
---|
| 104 |
|
---|
| 105 | Prefer lower case and underscores for local variables.
|
---|
| 106 |
|
---|
| 107 | BOOL is_ok;
|
---|
| 108 |
|
---|
[1776] | 109 | Local variables can be short as long as meaning is clear.
|
---|
| 110 |
|
---|
[605] | 111 | BOOL ok;
|
---|
| 112 |
|
---|
| 113 | Underscores can be omitted if name remains readable.
|
---|
| 114 |
|
---|
| 115 | USHORT maxcnt;
|
---|
[1776] | 116 |
|
---|
[459] | 117 | Spaces
|
---|
| 118 |
|
---|
[605] | 119 | Separate keywords from leading paren with 1 space.
|
---|
[459] | 120 |
|
---|
[605] | 121 | if (expr) statement;
|
---|
[459] | 122 |
|
---|
[605] | 123 | No spaces between function name and leading paren.
|
---|
| 124 | No spaces between last arg and trailing paren.
|
---|
[459] | 125 |
|
---|
| 126 | func()
|
---|
| 127 |
|
---|
[605] | 128 | Follow separating commas and semicolons with a space.
|
---|
[459] | 129 |
|
---|
| 130 | x = func(a, c)
|
---|
| 131 |
|
---|
[605] | 132 | Surround binary operators with a leading and trailing spaces.
|
---|
[459] | 133 |
|
---|
| 134 | x = a + b
|
---|
| 135 |
|
---|
[605] | 136 | Try to avoid spurious internal and trailing whitespace.
|
---|
[459] | 137 |
|
---|
[605] | 138 | Expressions
|
---|
| 139 |
|
---|
| 140 | Do not use superfluous parens.
|
---|
| 141 |
|
---|
| 142 | Prefer
|
---|
| 143 |
|
---|
| 144 | return 0;
|
---|
| 145 |
|
---|
| 146 | to
|
---|
| 147 |
|
---|
| 148 | return (0);
|
---|
| 149 |
|
---|
[1776] | 150 | Do not use superfluous parens when operator precedence will do the
|
---|
[605] | 151 | right thing.
|
---|
| 152 |
|
---|
| 153 | Prefer
|
---|
| 154 |
|
---|
| 155 | if (a & mask && c)
|
---|
| 156 |
|
---|
| 157 | to
|
---|
| 158 |
|
---|
| 159 | if ((a & mask) && c)
|
---|
| 160 |
|
---|
| 161 | Avoid nested ternary conditionals (i.e. ? :). They are hard to read and
|
---|
| 162 | rarely generate better code than switch or if statement.
|
---|
| 163 |
|
---|
[1779] | 164 | Commenting code
|
---|
| 165 |
|
---|
| 166 | Prefer javadoc style comments for function headers.
|
---|
| 167 |
|
---|
| 168 | When usings // comments, // should be followed by a space.
|
---|
| 169 |
|
---|
| 170 | Avoid using /* */ comments to disable code. They can be hard to see.
|
---|
| 171 |
|
---|
[1776] | 172 | Disabling code
|
---|
| 173 |
|
---|
| 174 | When disabling multiple lines of code, prefer
|
---|
| 175 |
|
---|
| 176 | #if 0
|
---|
| 177 | #endif
|
---|
| 178 |
|
---|
| 179 | to
|
---|
| 180 |
|
---|
| 181 | /*
|
---|
| 182 | */
|
---|
| 183 |
|
---|
| 184 | comments or // comments.
|
---|
| 185 |
|
---|
| 186 | When disabling multiple lines of code, it is acceptable to use // comments
|
---|
| 187 | to disable a single line. The // should be followed by a trailing space
|
---|
| 188 |
|
---|
| 189 | // DbgMsg("here");
|
---|
| 190 |
|
---|
[392] | 191 | Memory
|
---|
| 192 |
|
---|
| 193 | - Check all malloc/realloc/strdup/DosAllocMem calls
|
---|
| 194 | Use xmalloc in non-time critical code
|
---|
| 195 | Use xrealloc in non-time critical code
|
---|
| 196 | Use xstrdup in non-time critical code
|
---|
| 197 |
|
---|
| 198 | - Use xfree rather than free
|
---|
| 199 |
|
---|
[619] | 200 | - If buffer overflow possible, check for it.
|
---|
[605] | 201 |
|
---|
[392] | 202 | Windows
|
---|
| 203 |
|
---|
[459] | 204 | - Check all WinCreateWindow calls
|
---|
[392] | 205 | - Check QWL_USER pointer data
|
---|
| 206 |
|
---|
[459] | 207 | Error notifications
|
---|
[392] | 208 |
|
---|
[459] | 209 | DosBeep(50,100) Alert user to operator error
|
---|
| 210 | Use only when failure reason is obvious
|
---|
[392] | 211 |
|
---|
[459] | 212 | saymsg Alert user to operator error with popup
|
---|
[392] | 213 |
|
---|
[459] | 214 | Runtime_Error Report "should not occur" errors
|
---|
| 215 | Dos_Error
|
---|
| 216 | Win_Error
|
---|
| 217 |
|
---|
[392] | 218 | $Id: CodingStyle.txt 1779 2014-06-26 22:32:51Z stevenhl $
|
---|