aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--docs/style-guide.txt89
1 files changed, 75 insertions, 14 deletions
diff --git a/docs/style-guide.txt b/docs/style-guide.txt
index 4d0c7ca17..374a822b2 100644
--- a/docs/style-guide.txt
+++ b/docs/style-guide.txt
@@ -109,6 +109,13 @@ between it and the opening control block statement. Examples:
109 while (!done){ 109 while (!done){
110 do{ 110 do{
111 111
112 And for heaven's sake, don't do this:
113
114 while (!done)
115 {
116 do
117 {
118
112 Do this instead: 119 Do this instead:
113 120
114 while (!done) { 121 while (!done) {
@@ -175,6 +182,7 @@ block. Example:
175 182
176 183
177 184
185
178Variable and Function Names 186Variable and Function Names
179--------------------------- 187---------------------------
180 188
@@ -183,16 +191,32 @@ used to separate words (e.g., "variable_name" and "numchars" are both
183acceptable). Using underscores makes variable and function names more readable 191acceptable). Using underscores makes variable and function names more readable
184because it looks like whitespace; using lower-case is easy on the eyes. 192because it looks like whitespace; using lower-case is easy on the eyes.
185 193
194 Frowned upon:
195
196 hitList
197 TotalChars
198 szFileName (blech)
199
200 Preferred:
201
202 hit_list
203 total_chars
204 file_name
205
206The exception to this rule are enums, macros, and constant variables which
207should all be in upper-case, with words optionally seperatedy by underscores
208(i.e. FIFOTYPE, ISBLKDEV()).
209
186Note: The Busybox codebase is very much a mixture of code gathered from a 210Note: The Busybox codebase is very much a mixture of code gathered from a
187variety of sources. This explains why the current codebase contains such a 211variety of sources. This explains why the current codebase contains such a
188hodge-podge of different naming styles (Java, Pascal, K&R, just-plain-weird, 212hodge-podge of different naming styles (Java, Pascal, K&R, just-plain-weird,
189etc.). The K&R guideline explained above should therefore be used on new files 213etc.). The K&R guideline explained above should therefore be used on new files
190that are added to the repository. Furthermore, the maintainer of an existing 214that are added to the repository. Furthermore, the maintainer of an existing
191file that uses alternate naming conventions should -- at his own convenience -- 215file that uses alternate naming conventions should -- at his own convenience
192convert those names over to K&R style; converting variable names is a very low 216-- convert those names over to K&R style; converting variable names is a very
193priority task. Perhaps in the future we will include some magical Perl script 217low priority task. Perhaps in the future we will include some magical Perl
194that can go through and convert files -- left as an exercise to the reader for 218script that can go through and convert variable names, left as an exercise for
195now. 219the reader for now.
196 220
197 221
198 222
@@ -388,26 +412,26 @@ line. Example:
388 Don't do this: 412 Don't do this:
389 413
390 if (foo) 414 if (foo)
391 stmt; 415 stmt1;
392 else 416 stmt2
393 stmt; 417 stmt3;
394 418
395 Do this instead: 419 Do this instead:
396 420
397 if (foo) { 421 if (foo) {
398 stmt; 422 stmt1;
399 } else {
400 stmt;
401 } 423 }
424 stmt2
425 stmt3;
402 426
403The "bracketless" approach is error prone because someday you might add a line 427The "bracketless" approach is error prone because someday you might add a line
404like this: 428like this:
405 429
406 if (foo) 430 if (foo)
407 stmt; 431 stmt1;
408 new_line(); 432 new_line();
409 else 433 stmt2
410 stmt; 434 stmt3;
411 435
412And the resulting behavior of your program would totally bewilder you. (Don't 436And the resulting behavior of your program would totally bewilder you. (Don't
413laugh, it happens to us all.) Remember folks, this is C, not Python. 437laugh, it happens to us all.) Remember folks, this is C, not Python.
@@ -438,3 +462,40 @@ support ancient, antediluvian compilers. To our good fortune, we have access
438to more modern compilers and the old declaration syntax is neither necessary 462to more modern compilers and the old declaration syntax is neither necessary
439nor desired. 463nor desired.
440 464
465
466Emphasizing Logical Blocks
467~~~~~~~~~~~~~~~~~~~~~~~~~~
468
469Organization and readability are improved by putting extra newlines around
470blocks of code that perform a single task. These are typically blocks that
471begin with a C keyword, but not always.
472
473Furthermore, you should put a single comment (not necessarily one line, just
474one comment) before the block, rather than commenting each and every line.
475There is an optimal ammount of commenting that a program can have; you can
476comment too much as well as too little.
477
478A picture is really worth a thousand words here, so here is an example that
479illustrates emphasizing logical blocks:
480
481 while (line = get_line_from_file(fp)) {
482
483 /* eat the newline, if any */
484 if (line[strlen(line)-1] == '\n') {
485 line[strlen(line)-1] = '\0';
486 }
487
488 /* ignore blank lines */
489 if (strlen(file_to_act_on) == 0) {
490 continue;
491 }
492
493 /* if the search string is in this line, print it,
494 * unless we were told to be quiet */
495 if (strstr(line, search) && !be_quiet) {
496 puts(line);
497 }
498
499 /* clean up */
500 free(line);
501 }