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