blob: 36974d7f5f6e5efd5687ae9ea11bec6cf724fc53 [file] [log] [blame]
Mark Whitley40bfc762000-07-24 22:36:06 +00001Busybox Style Guide
2===================
3
4This document describes the coding style conventions used in Busybox. If you
5add a new file to Busybox or are editing an existing file, please format your
6code according to this style. If you are the maintainer of a file that does
7not follow these guidelines, please -- at your own convenience -- modify the
8file(s) you maintain to bring them into conformance with this style guide.
9Please note that this is a low priority task.
10
11To help you format the whitespace of your programs, an ".indent.pro" file is
12included in the main Busybox source directory that contains option flags to
13format code as per this style guide. This way you can run GNU indent on your
14files by typing 'indent myfile.c myfile.h' and it will magically apply all the
15right formatting rules to your file. Please _do_not_ run this on all the files
16in the directory, just your own.
17
Mark Whitley2368a382000-08-22 00:20:21 +000018
Mark Whitley40bfc762000-07-24 22:36:06 +000019Declaration Order
20-----------------
21
22Here is the order in which code should be laid out in a file:
23
Mark Whitley9028e2c2000-11-17 21:28:39 +000024 - commented program name and one-line description
Mark Whitley40bfc762000-07-24 22:36:06 +000025 - commented author name and email address(es)
26 - commented GPL boilerplate
Mark Whitley9028e2c2000-11-17 21:28:39 +000027 - commented longer description / notes for the program (if needed)
Mark Whitley40bfc762000-07-24 22:36:06 +000028 - #includes and #defines
Mark Whitley9028e2c2000-11-17 21:28:39 +000029 - const and global variables
Mark Whitley40bfc762000-07-24 22:36:06 +000030 - function declarations (if necessary)
31 - function implementations
32
Mark Whitley2368a382000-08-22 00:20:21 +000033
Mark Whitley40bfc762000-07-24 22:36:06 +000034Whitespace
35----------
36
Mark Whitley2368a382000-08-22 00:20:21 +000037This is everybody's favorite flame topic so let's get it out of the way right
38up front.
39
40
Mark Whitley9028e2c2000-11-17 21:28:39 +000041Tabs vs. Spaces in Line Indentation
Mark Whitley2368a382000-08-22 00:20:21 +000042~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
43
44The preference in Busybox is to indent lines with tabs. Do not indent lines
45with spaces and do not indents lines using a mixture of tabs and spaces. (The
46indentation 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
48multi-line comments that use an asterisk at the beginning of each line, i.e.:
Mark Whitley40bfc762000-07-24 22:36:06 +000049
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
58Furthermore, The preference is that tabs be set to display at four spaces
59wide, but the beauty of using only tabs (and not spaces) at the beginning of
Mark Whitley9028e2c2000-11-17 21:28:39 +000060lines is that you can set your editor to display tabs at *whatever* number of
Mark Whitley40bfc762000-07-24 22:36:06 +000061spaces is desired and the code will still look fine.
62
63
Mark Whitley2368a382000-08-22 00:20:21 +000064Operator Spacing
65~~~~~~~~~~~~~~~~
66
67Put spaces between terms and operators. Example:
Mark Whitley40bfc762000-07-24 22:36:06 +000068
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
Mark Whitley9028e2c2000-11-17 21:28:39 +000080 single term (even if it is a compound term) such as:
Mark Whitley40bfc762000-07-24 22:36:06 +000081
82 if (str[idx] == '/' && str[idx-1] != '\\')
83
84 or
85
86 if ((argc-1) - (optind+1) > 0)
87
88
Mark Whitley2368a382000-08-22 00:20:21 +000089Bracket Spacing
90~~~~~~~~~~~~~~~
91
92If an opening bracket starts a function, it should be on the
Mark Whitley9028e2c2000-11-17 21:28:39 +000093next line with no spacing before it. However, if a bracket follows an opening
Mark Whitley40bfc762000-07-24 22:36:06 +000094control block, it should be on the same line with a single space (not a tab)
Mark Whitley9028e2c2000-11-17 21:28:39 +000095between it and the opening control block statement. Examples:
Mark Whitley40bfc762000-07-24 22:36:06 +000096
97 Don't do this:
98
Mark Whitley9028e2c2000-11-17 21:28:39 +000099 while (!done)
100 {
101
102 do
103 {
104
105 Don't do this either:
106
Mark Whitley40bfc762000-07-24 22:36:06 +0000107 while (!done){
108 do{
109
110 Do this instead:
111
112 while (!done) {
113 do {
114
Mark Whitley2368a382000-08-22 00:20:21 +0000115
116Paren Spacing
117~~~~~~~~~~~~~
118
119Put a space between C keywords and left parens, but not between
120function names and the left paren that starts it's parameter list (whether it
121is 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
Mark Whitley9028e2c2000-11-17 21:28:39 +0000133 But do functions like this:
Mark Whitley2368a382000-08-22 00:20:21 +0000134
135 static int my_func(int foo, char bar)
136 ...
137 baz = my_func(1, 2);
138
139
140Cuddled Elses
141~~~~~~~~~~~~~
142
Mark Whitley9028e2c2000-11-17 21:28:39 +0000143Also, please "cuddle" your else statements by putting the else keyword on the
144same line after the right bracket that closes an 'if' statement.
Mark Whitley40bfc762000-07-24 22:36:06 +0000145
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
Mark Whitley9028e2c2000-11-17 21:28:39 +0000163The exception to this rule is if you want to include a comment before the else
164block. 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
Mark Whitley40bfc762000-07-24 22:36:06 +0000174
Mark Whitley40bfc762000-07-24 22:36:06 +0000175Variable and Function Names
176---------------------------
177
178Use the K&R style with names in all lower-case and underscores occasionally
Mark Whitley9028e2c2000-11-17 21:28:39 +0000179used to separate words (e.g., "variable_name" and "numchars" are both
Mark Whitley40bfc762000-07-24 22:36:06 +0000180acceptable). Using underscores makes variable and function names more readable
181because it looks like whitespace; using lower-case is easy on the eyes.
182
183Note: The Busybox codebase is very much a mixture of code gathered from a
Mark Whitley9028e2c2000-11-17 21:28:39 +0000184variety of sources. This explains why the current codebase contains such a
185hodge-podge of different naming styles (Java, Pascal, K&R, just-plain-weird,
Mark Whitley40bfc762000-07-24 22:36:06 +0000186etc.). The K&R guideline explained above should therefore be used on new files
187that are added to the repository. Furthermore, the maintainer of an existing
Mark Whitley9028e2c2000-11-17 21:28:39 +0000188file that uses alternate naming conventions should -- at his own convenience --
189convert those names over to K&R style; converting variable names is a very low
190priority task. Perhaps in the future we will include some magical Perl script
191that can go through and convert files -- left as an exercise to the reader for
192now.
Mark Whitley40bfc762000-07-24 22:36:06 +0000193
194
195Tip and Pointers
196----------------
197
198The following are simple coding guidelines that should be followed:
199
Mark Whitley9028e2c2000-11-17 21:28:39 +0000200 - When in doubt about the proper behavior of a Busybox program (output,
Mark Whitley2368a382000-08-22 00:20:21 +0000201 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
Mark Whitley9028e2c2000-11-17 21:28:39 +0000204 Busybox programs after their GNU counterparts and nobody has to get hurt.
Mark Whitley2368a382000-08-22 00:20:21 +0000205
Mark Whitley40bfc762000-07-24 22:36:06 +0000206 - Don't use a '#define var 80' when you can use 'static const int var 80'
Mark Whitley9028e2c2000-11-17 21:28:39 +0000207 instead. This makes the compiler do type checking for you (rather than
Mark Whitley40bfc762000-07-24 22:36:06 +0000208 relying on the more error-prone preprocessor) and it makes debugging
Mark Whitley9028e2c2000-11-17 21:28:39 +0000209 programs much easier since the value of the variable can be easily
210 displayed.
Mark Whitley40bfc762000-07-24 22:36:06 +0000211
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
Mark Whitley9028e2c2000-11-17 21:28:39 +0000216 the global name space clean. The only exception to this rule is the
Mark Whitley40bfc762000-07-24 22:36:06 +0000217 "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
Mark Whitley9028e2c2000-11-17 21:28:39 +0000223 - 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).
Mark Whitley40bfc762000-07-24 22:36:06 +0000228
Mark Whitley52681b42000-07-25 20:30:00 +0000229 - 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
Mark Whitley9028e2c2000-11-17 21:28:39 +0000244 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.
Mark Whitley52681b42000-07-25 20:30:00 +0000247
Mark Whitley40bfc762000-07-24 22:36:06 +0000248 - 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.