source: trunk/CodingStyle.txt@ 1889

Last change on this file since 1889 was 1889, checked in by Steven Levine, 7 years ago

Minor clarifications and typo corrections.

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