blob: 72d3b9375f7e5384e16d90615c9e4716474e94c4 [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
24 - commented author name and email address(es)
25 - commented GPL boilerplate
26 - commented description of program
27 - #includes and #defines
28 - const and globals variables
29 - function declarations (if necessary)
30 - function implementations
31
Mark Whitley2368a382000-08-22 00:20:21 +000032
Mark Whitley40bfc762000-07-24 22:36:06 +000033Whitespace
34----------
35
Mark Whitley2368a382000-08-22 00:20:21 +000036This is everybody's favorite flame topic so let's get it out of the way right
37up front.
38
39
40Tabs vs Spaces in Line Indentation
41~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
42
43The preference in Busybox is to indent lines with tabs. Do not indent lines
44with spaces and do not indents lines using a mixture of tabs and spaces. (The
45indentation style in the Apache and Postfix source does this sort of thing:
46\s\s\s\sif (expr) {\n\tstmt; --ick.) The only exception to this rule is
47multi-line comments that use an asterisk at the beginning of each line, i.e.:
Mark Whitley40bfc762000-07-24 22:36:06 +000048
49 /t/*
50 /t * This is a block comment.
51 /t * Note that it has multiple lines
52 /t * and that the beginning of each line has a tab plus a space
53 /t * except for the opening '/*' line where the slash
54 /t * is used instead of a space.
55 /t */
56
57Furthermore, The preference is that tabs be set to display at four spaces
58wide, but the beauty of using only tabs (and not spaces) at the beginning of
59lines is that you can set your editor to display tabs at *watever* number of
60spaces is desired and the code will still look fine.
61
62
Mark Whitley2368a382000-08-22 00:20:21 +000063Operator Spacing
64~~~~~~~~~~~~~~~~
65
66Put spaces between terms and operators. Example:
Mark Whitley40bfc762000-07-24 22:36:06 +000067
68 Don't do this:
69
70 for(i=0;i<num_items;i++){
71
72 Do this instead:
73
74 for (i = 0; i < num_items; i++) {
75
76 While it extends the line a bit longer, the spaced version is more
77 readable. An allowable exception to this rule is the situation where
78 excluding the spacing makes it more obvious that we are dealing with a
79 single term (even if it is a compund term) such as:
80
81 if (str[idx] == '/' && str[idx-1] != '\\')
82
83 or
84
85 if ((argc-1) - (optind+1) > 0)
86
87
Mark Whitley2368a382000-08-22 00:20:21 +000088Bracket Spacing
89~~~~~~~~~~~~~~~
90
91If an opening bracket starts a function, it should be on the
Mark Whitley40bfc762000-07-24 22:36:06 +000092next line with no spacing before it. However, if a bracet follows an opening
93control block, it should be on the same line with a single space (not a tab)
94between it and the opening control block statment. Examples:
95
96 Don't do this:
97
98 while (!done){
99 do{
100
101 Do this instead:
102
103 while (!done) {
104 do {
105
Mark Whitley2368a382000-08-22 00:20:21 +0000106
107Paren Spacing
108~~~~~~~~~~~~~
109
110Put a space between C keywords and left parens, but not between
111function names and the left paren that starts it's parameter list (whether it
112is being declared or called). Examples:
113
114 Don't do this:
115
116 while(foo) {
117 for(i = 0; i < n; i++) {
118
119 Do this instead:
120
121 while (foo) {
122 for (i = 0; i < n; i++) {
123
124 Do functions like this:
125
126 static int my_func(int foo, char bar)
127 ...
128 baz = my_func(1, 2);
129
130
131Cuddled Elses
132~~~~~~~~~~~~~
133
Mark Whitley40bfc762000-07-24 22:36:06 +0000134Also, please "cuddle" your else statments by putting the else keyword on the
135same line after the right bracket that closes an 'if' statment.
136
137 Don't do this:
138
139 if (foo) {
140 stmt;
141 }
142 else {
143 stmt;
144 }
145
146 Do this instead:
147
148 if (foo) {
149 stmt;
150 } else {
151 stmt;
152 }
153
154
Mark Whitley40bfc762000-07-24 22:36:06 +0000155Variable and Function Names
156---------------------------
157
158Use the K&R style with names in all lower-case and underscores occasionally
159used to seperate words (e.g. "variable_name" and "numchars" are both
160acceptable). Using underscores makes variable and function names more readable
161because it looks like whitespace; using lower-case is easy on the eyes.
162
163Note: The Busybox codebase is very much a mixture of code gathered from a
164variety of locations. This explains why the current codebase contains such a
165plethora of different naming styles (Java, Pascal, K&R, just-plain-weird,
166etc.). The K&R guideline explained above should therefore be used on new files
167that are added to the repository. Furthermore, the maintainer of an existing
168file that uses alternate naming conventions should -- at his own convenience
169-- convert those names over to K&R style; converting variable names is a very
170low priority task. Perhaps in the future we will include some magical Perl
171script that can go through and convert files--left as an exersize to the
172reader.
173
174
175Tip and Pointers
176----------------
177
178The following are simple coding guidelines that should be followed:
179
Mark Whitley2368a382000-08-22 00:20:21 +0000180 - When in doubt about the propper behavior of a busybox program (output,
181 formatting, options, etc.), model it after the equivalent GNU program.
182 Doesn't matter how that program behaves on some other flavor of *NIX;
183 doesn't matter what the POSIX standard says or doesn't say, just model
184 busybox programs after their GNU counterparts and nobody has to get hurt.
185
Mark Whitley40bfc762000-07-24 22:36:06 +0000186 - Don't use a '#define var 80' when you can use 'static const int var 80'
187 instead. This makes the compiler do typechecking for you (rather than
188 relying on the more error-prone preprocessor) and it makes debugging
189 programs much easier since the value of the variable can be easily queried.
190
191 - If a const variable is used in only one function, do not make it global to
192 the file. Instead, declare it inside the function body.
193
194 - Inside applet files, all functions should be declared static so as to keep
195 the global namespace clean. The only exception to this rule is the
196 "applet_main" function which must be declared extern.
197
198 - If you write a function that performs a task that could be useful outside
199 the immediate file, turn it into a general-purpose function with no ties to
200 any applet and put it in the utility.c file instead.
201
202 - Put all help/usage messages in usage.c. Put other strings in messages.c
203 (Side Note: we might want to use a single file instead of two, food for
204 thought).
205
Mark Whitley52681b42000-07-25 20:30:00 +0000206 - There's a right way and a wrong way to test for sting equivalence with
207 strcmp:
208
209 The wrong way:
210
211 if (!strcmp(string, "foo")) {
212 ...
213
214 The right way:
215
216 if (strcmp(string, "foo") == 0){
217 ...
218
219 The use of the "equals" (==) operator in the latter example makes it much
220 more obvious that you are testing for equivalence. The former example with
221 the "not" (!) operator makes it look like you are testing for an error.
222
Mark Whitley40bfc762000-07-24 22:36:06 +0000223 - Do not use old-style function declarations that declare variable types
224 between the parameter list and opening bracket. Example:
225
226 Don't do this:
227
228 int foo(parm1, parm2)
229 char parm1;
230 float parm2;
231 {
232 ....
233
234 Do this instead:
235
236 int foo(char parm1, float parm2)
237 {
238 ....
239
240 - Please use brackets on all if and else statements, even if it is only one
241 line. Example:
242
243 Don't do this:
244
245 if (foo)
246 stmt;
247 else
248 stmt;
249
250 Do this instead:
251
252 if (foo) {
253 stmt;
254 } else {
255 stmt;
256 }
257
258 The "bracketless" approach is error prone because someday you might add a
259 line like this:
260
261 if (foo)
262 stmt;
263 new_line();
264 else
265 stmt;
266
267 And the resulting behavior of your program would totally bewilder you.
268 (Don't laugh, it happens to us all.) Remember folks, this is C, not
269 Python.