~vcs-imports/busybox/trunk

« back to all changes in this revision

Viewing changes to docs/style-guide.txt

  • Committer: Mark Whitley
  • Date: 2000-07-24 22:36:06 UTC
  • Revision ID: git-v1:40bfc763855145694f43f5eee5088a2361da56ed
First revision of the Busybox Style Guide and an accompanying .indent.pro
file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
Declaration Order
 
19
-----------------
 
20
 
 
21
Here 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
 
 
31
Whitespace
 
32
----------
 
33
 
 
34
Tabs vs Spaces in Line Indentation: The preference in Busybox is to indent
 
35
lines with tabs. Do not indent lines with spaces and do not indents lines
 
36
using a mixture of tabs and spaces. (The indentation style in the Apache and
 
37
Postfix source does this sort of thing: \s\s\s\sif (expr) {\n\tstmt; --ick.)
 
38
The only exception to this rule is multi-line comments that use an asterisk at
 
39
the 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
 
 
49
Furthermore, The preference is that tabs be set to display at four spaces
 
50
wide, but the beauty of using only tabs (and not spaces) at the beginning of
 
51
lines is that you can set your editor to display tabs at *watever* number of
 
52
spaces is desired and the code will still look fine.
 
53
 
 
54
 
 
55
Operator 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
 
 
77
Bracket Spacing: If an opening bracket starts a function, it should be on the
 
78
next line with no spacing before it. However, if a bracet follows an opening
 
79
control block, it should be on the same line with a single space (not a tab)
 
80
between 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
 
 
92
Also, please "cuddle" your else statments by putting the else keyword on the
 
93
same 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
 
 
113
Paren Spacing: Put a space between C keywords and left parens, but not between
 
114
function names and the left paren that starts it's parameter list (whether it
 
115
is 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
 
 
133
Variable and Function Names
 
134
---------------------------
 
135
 
 
136
Use the K&R style with names in all lower-case and underscores occasionally
 
137
used to seperate words (e.g. "variable_name" and "numchars" are both
 
138
acceptable). Using underscores makes variable and function names more readable
 
139
because it looks like whitespace; using lower-case is easy on the eyes.
 
140
 
 
141
Note: The Busybox codebase is very much a mixture of code gathered from a
 
142
variety of locations. This explains why the current codebase contains such a
 
143
plethora of different naming styles (Java, Pascal, K&R, just-plain-weird,
 
144
etc.). The K&R guideline explained above should therefore be used on new files
 
145
that are added to the repository. Furthermore, the maintainer of an existing
 
146
file 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
 
148
low priority task. Perhaps in the future we will include some magical Perl
 
149
script that can go through and convert files--left as an exersize to the
 
150
reader.
 
151
 
 
152
 
 
153
Tip and Pointers
 
154
----------------
 
155
 
 
156
The 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.