diff options
| author | markw <markw@69ca8d6d-28ef-0310-b511-8ec308f3f277> | 2000-12-20 22:35:12 +0000 |
|---|---|---|
| committer | markw <markw@69ca8d6d-28ef-0310-b511-8ec308f3f277> | 2000-12-20 22:35:12 +0000 |
| commit | 7f918bc591c5d6edca09766fdcb294e55a9d1ca6 (patch) | |
| tree | 101b1b397ce882d5dc00470564cddcf89504e2e4 | |
| parent | b70b9b132b2b19d227c91aa4b036d747878c2bc3 (diff) | |
| download | busybox-w32-7f918bc591c5d6edca09766fdcb294e55a9d1ca6.tar.gz busybox-w32-7f918bc591c5d6edca09766fdcb294e55a9d1ca6.tar.bz2 busybox-w32-7f918bc591c5d6edca09766fdcb294e55a9d1ca6.zip | |
Some corrections, some additions, some embellishments.
git-svn-id: svn://busybox.net/trunk/busybox@1480 69ca8d6d-28ef-0310-b511-8ec308f3f277
| -rw-r--r-- | docs/style-guide.txt | 89 |
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 | |||
| 178 | Variable and Function Names | 186 | Variable and Function Names |
| 179 | --------------------------- | 187 | --------------------------- |
| 180 | 188 | ||
| @@ -183,16 +191,32 @@ used to separate words (e.g., "variable_name" and "numchars" are both | |||
| 183 | acceptable). Using underscores makes variable and function names more readable | 191 | acceptable). Using underscores makes variable and function names more readable |
| 184 | because it looks like whitespace; using lower-case is easy on the eyes. | 192 | because 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 | |||
| 206 | The exception to this rule are enums, macros, and constant variables which | ||
| 207 | should all be in upper-case, with words optionally seperatedy by underscores | ||
| 208 | (i.e. FIFOTYPE, ISBLKDEV()). | ||
| 209 | |||
| 186 | Note: The Busybox codebase is very much a mixture of code gathered from a | 210 | Note: The Busybox codebase is very much a mixture of code gathered from a |
| 187 | variety of sources. This explains why the current codebase contains such a | 211 | variety of sources. This explains why the current codebase contains such a |
| 188 | hodge-podge of different naming styles (Java, Pascal, K&R, just-plain-weird, | 212 | hodge-podge of different naming styles (Java, Pascal, K&R, just-plain-weird, |
| 189 | etc.). The K&R guideline explained above should therefore be used on new files | 213 | etc.). The K&R guideline explained above should therefore be used on new files |
| 190 | that are added to the repository. Furthermore, the maintainer of an existing | 214 | that are added to the repository. Furthermore, the maintainer of an existing |
| 191 | file that uses alternate naming conventions should -- at his own convenience -- | 215 | file that uses alternate naming conventions should -- at his own convenience |
| 192 | convert 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 |
| 193 | priority task. Perhaps in the future we will include some magical Perl script | 217 | low priority task. Perhaps in the future we will include some magical Perl |
| 194 | that can go through and convert files -- left as an exercise to the reader for | 218 | script that can go through and convert variable names, left as an exercise for |
| 195 | now. | 219 | the 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 | ||
| 403 | The "bracketless" approach is error prone because someday you might add a line | 427 | The "bracketless" approach is error prone because someday you might add a line |
| 404 | like this: | 428 | like 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 | ||
| 412 | And the resulting behavior of your program would totally bewilder you. (Don't | 436 | And the resulting behavior of your program would totally bewilder you. (Don't |
| 413 | laugh, it happens to us all.) Remember folks, this is C, not Python. | 437 | laugh, 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 | |||
| 438 | to more modern compilers and the old declaration syntax is neither necessary | 462 | to more modern compilers and the old declaration syntax is neither necessary |
| 439 | nor desired. | 463 | nor desired. |
| 440 | 464 | ||
| 465 | |||
| 466 | Emphasizing Logical Blocks | ||
| 467 | ~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 468 | |||
| 469 | Organization and readability are improved by putting extra newlines around | ||
| 470 | blocks of code that perform a single task. These are typically blocks that | ||
| 471 | begin with a C keyword, but not always. | ||
| 472 | |||
| 473 | Furthermore, you should put a single comment (not necessarily one line, just | ||
| 474 | one comment) before the block, rather than commenting each and every line. | ||
| 475 | There is an optimal ammount of commenting that a program can have; you can | ||
| 476 | comment too much as well as too little. | ||
| 477 | |||
| 478 | A picture is really worth a thousand words here, so here is an example that | ||
| 479 | illustrates 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 | } | ||
