Numerous spelling / grammar corrections from Larry Doolittle
[oweals/busybox.git] / docs / style-guide.txt
1 Busybox Style Guide
2 ===================
3
4 This document describes the coding style conventions used in Busybox. If you
5 add a new file to Busybox or are editing an existing file, please format your
6 code according to this style. If you are the maintainer of a file that does
7 not follow these guidelines, please -- at your own convenience -- modify the
8 file(s) you maintain to bring them into conformance with this style guide.
9 Please note that this is a low priority task.
10
11 To help you format the whitespace of your programs, an ".indent.pro" file is
12 included in the main Busybox source directory that contains option flags to
13 format code as per this style guide. This way you can run GNU indent on your
14 files by typing 'indent myfile.c myfile.h' and it will magically apply all the
15 right formatting rules to your file. Please _do_not_ run this on all the files
16 in the directory, just your own.
17
18
19 Declaration Order
20 -----------------
21
22 Here is the order in which code should be laid out in a file:
23
24  - commented program name and one-line description
25  - commented author name and email address(es)
26  - commented GPL boilerplate
27  - commented longer description / notes for the program (if needed)
28  - #includes and #defines
29  - const and global variables
30  - function declarations (if necessary)
31  - function implementations
32
33
34 Whitespace
35 ----------
36
37 This is everybody's favorite flame topic so let's get it out of the way right
38 up front.
39
40
41 Tabs vs. Spaces in Line Indentation
42 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
43
44 The preference in Busybox is to indent lines with tabs. Do not indent lines
45 with spaces and do not indents lines using a mixture of tabs and spaces. (The
46 indentation style in the Apache and Postfix source does this sort of thing:
47 \s\s\s\sif (expr) {\n\tstmt; --ick.) The only exception to this rule is
48 multi-line comments that use an asterisk at the beginning of each line, i.e.:
49
50         /t/*
51         /t * This is a block comment.
52         /t * Note that it has multiple lines
53         /t * and that the beginning of each line has a tab plus a space
54         /t * except for the opening '/*' line where the slash
55         /t * is used instead of a space.
56         /t */
57
58 Furthermore, The preference is that tabs be set to display at four spaces
59 wide, but the beauty of using only tabs (and not spaces) at the beginning of
60 lines is that you can set your editor to display tabs at *whatever* number of
61 spaces is desired and the code will still look fine.
62
63
64 Operator Spacing
65 ~~~~~~~~~~~~~~~~
66
67 Put spaces between terms and operators. Example:
68
69         Don't do this:
70
71                 for(i=0;i<num_items;i++){
72
73         Do this instead:
74
75                 for (i = 0; i < num_items; i++) {
76
77         While it extends the line a bit longer, the spaced version is more
78         readable. An allowable exception to this rule is the situation where
79         excluding the spacing makes it more obvious that we are dealing with a
80         single term (even if it is a compound term) such as:
81
82                 if (str[idx] == '/' && str[idx-1] != '\\')
83
84         or
85
86                 if ((argc-1) - (optind+1) > 0)
87
88
89 Bracket Spacing
90 ~~~~~~~~~~~~~~~
91
92 If an opening bracket starts a function, it should be on the
93 next line with no spacing before it. However, if a bracket follows an opening
94 control block, it should be on the same line with a single space (not a tab)
95 between it and the opening control block statement. Examples:
96
97         Don't do this:
98
99                 while (!done)
100                 {
101
102                 do
103                 {
104
105         Don't do this either:
106
107                 while (!done){
108                 do{
109
110         Do this instead:
111
112                 while (!done) {
113                 do {
114
115
116 Paren Spacing
117 ~~~~~~~~~~~~~
118
119 Put a space between C keywords and left parens, but not between
120 function names and the left paren that starts it's parameter list (whether it
121 is being declared or called). Examples:
122
123         Don't do this:
124
125                 while(foo) {
126                 for(i = 0; i < n; i++) {
127
128         Do this instead:
129
130                 while (foo) {
131                 for (i = 0; i < n; i++) {
132
133         But do functions like this:
134
135                 static int my_func(int foo, char bar)
136                 ...
137                 baz = my_func(1, 2);
138
139
140 Cuddled Elses
141 ~~~~~~~~~~~~~
142
143 Also, please "cuddle" your else statements by putting the else keyword on the
144 same line after the right bracket that closes an 'if' statement.
145
146         Don't do this:
147
148         if (foo) {
149                 stmt;
150         }
151         else {
152                 stmt;
153         }
154
155         Do this instead:
156
157         if (foo) {
158                 stmt;
159         } else {
160                 stmt;
161         }
162
163 The exception to this rule is if you want to include a comment before the else
164 block. Example:
165
166         if (foo) {
167                 stmts...
168         }
169         /* otherwise, we're just kidding ourselves, so re-frob the input */
170         else {
171                 other_stmts...
172         }
173
174
175 Variable and Function Names
176 ---------------------------
177
178 Use the K&R style with names in all lower-case and underscores occasionally
179 used to separate words (e.g., "variable_name" and "numchars" are both
180 acceptable). Using underscores makes variable and function names more readable
181 because it looks like whitespace; using lower-case is easy on the eyes.
182
183 Note: The Busybox codebase is very much a mixture of code gathered from a
184 variety of sources. This explains why the current codebase contains such a
185 hodge-podge of different naming styles (Java, Pascal, K&R, just-plain-weird,
186 etc.). The K&R guideline explained above should therefore be used on new files
187 that are added to the repository. Furthermore, the maintainer of an existing
188 file that uses alternate naming conventions should -- at his own convenience --
189 convert those names over to K&R style; converting variable names is a very low
190 priority task. Perhaps in the future we will include some magical Perl script
191 that can go through and convert files -- left as an exercise to the reader for
192 now.
193
194
195 Tip and Pointers
196 ----------------
197
198 The following are simple coding guidelines that should be followed:
199
200  - When in doubt about the proper behavior of a Busybox program (output,
201    formatting, options, etc.), model it after the equivalent GNU program.
202    Doesn't matter how that program behaves on some other flavor of *NIX;
203    doesn't matter what the POSIX standard says or doesn't say, just model
204    Busybox programs after their GNU counterparts and nobody has to get hurt.
205
206  - Don't use a '#define var 80' when you can use 'static const int var 80'
207    instead. This makes the compiler do type checking for you (rather than
208    relying on the more error-prone preprocessor) and it makes debugging
209    programs much easier since the value of the variable can be easily
210    displayed.
211
212  - If a const variable is used in only one function, do not make it global to
213    the file. Instead, declare it inside the function body.
214
215  - Inside applet files, all functions should be declared static so as to keep
216    the global name space clean. The only exception to this rule is the
217    "applet_main" function which must be declared extern.
218
219  - If you write a function that performs a task that could be useful outside
220    the immediate file, turn it into a general-purpose function with no ties to
221    any applet and put it in the utility.c file instead.
222
223  - Put all help/usage messages in usage.c. Put other strings in messages.c.
224    Putting these strings into their own file is a calculated decision designed
225    to confine spelling errors to a single place and aid internationalization
226    efforts, if needed.  (Side Note: we might want to use a single file instead
227    of two, food for thought).
228
229  - There's a right way and a wrong way to test for sting equivalence with
230    strcmp:
231
232         The wrong way:
233
234         if (!strcmp(string, "foo")) {
235                 ...
236
237         The right way:
238
239         if (strcmp(string, "foo") == 0){
240                 ...
241
242         The use of the "equals" (==) operator in the latter example makes it much
243         more obvious that you are testing for equivalence. The former example with
244         the "not" (!) operator makes it look like you are testing for an error. In
245         a more perfect world, we would have a streq() function in the string
246         library, but that ain't the world we're living in.
247
248  - Do not use old-style function declarations that declare variable types
249    between the parameter list and opening bracket. Example:
250
251         Don't do this:
252
253                 int foo(parm1, parm2)
254                         char parm1;
255                         float parm2;
256                 {
257                         ....
258
259         Do this instead:
260
261                 int foo(char parm1, float parm2)
262                 {
263                         ....
264
265  - Please use brackets on all if and else statements, even if it is only one
266    line. Example:
267
268         Don't do this:
269
270                 if (foo)
271                         stmt;
272                 else
273                         stmt;
274
275         Do this instead:
276
277                 if (foo) {
278                         stmt;
279                 } else {
280                         stmt;
281                 }
282
283         The "bracketless" approach is error prone because someday you might add a
284         line like this:
285
286                 if (foo)
287                         stmt;
288                         new_line();
289                 else
290                         stmt;
291
292         And the resulting behavior of your program would totally bewilder you.
293         (Don't laugh, it happens to us all.) Remember folks, this is C, not
294         Python.