Some corrections, some additions, some embellishments.

This commit is contained in:
Mark Whitley 2000-12-20 22:35:12 +00:00
parent 5e8c0ffb75
commit 3680c58084

View File

@ -109,6 +109,13 @@ between it and the opening control block statement. Examples:
while (!done){ while (!done){
do{ do{
And for heaven's sake, don't do this:
while (!done)
{
do
{
Do this instead: Do this instead:
while (!done) { while (!done) {
@ -175,6 +182,7 @@ block. Example:
Variable and Function Names Variable and Function Names
--------------------------- ---------------------------
@ -183,16 +191,32 @@ used to separate words (e.g., "variable_name" and "numchars" are both
acceptable). Using underscores makes variable and function names more readable acceptable). Using underscores makes variable and function names more readable
because it looks like whitespace; using lower-case is easy on the eyes. because it looks like whitespace; using lower-case is easy on the eyes.
Frowned upon:
hitList
TotalChars
szFileName (blech)
Preferred:
hit_list
total_chars
file_name
The exception to this rule are enums, macros, and constant variables which
should all be in upper-case, with words optionally seperatedy by underscores
(i.e. FIFOTYPE, ISBLKDEV()).
Note: The Busybox codebase is very much a mixture of code gathered from a Note: The Busybox codebase is very much a mixture of code gathered from a
variety of sources. This explains why the current codebase contains such a variety of sources. This explains why the current codebase contains such a
hodge-podge of different naming styles (Java, Pascal, K&R, just-plain-weird, hodge-podge of different naming styles (Java, Pascal, K&R, just-plain-weird,
etc.). The K&R guideline explained above should therefore be used on new files etc.). The K&R guideline explained above should therefore be used on new files
that are added to the repository. Furthermore, the maintainer of an existing that are added to the repository. Furthermore, the maintainer of an existing
file that uses alternate naming conventions should -- at his own convenience -- file that uses alternate naming conventions should -- at his own convenience
convert those names over to K&R style; converting variable names is a very low -- convert those names over to K&R style; converting variable names is a very
priority task. Perhaps in the future we will include some magical Perl script low priority task. Perhaps in the future we will include some magical Perl
that can go through and convert files -- left as an exercise to the reader for script that can go through and convert variable names, left as an exercise for
now. the reader for now.
@ -388,26 +412,26 @@ line. Example:
Don't do this: Don't do this:
if (foo) if (foo)
stmt; stmt1;
else stmt2
stmt; stmt3;
Do this instead: Do this instead:
if (foo) { if (foo) {
stmt; stmt1;
} else {
stmt;
} }
stmt2
stmt3;
The "bracketless" approach is error prone because someday you might add a line The "bracketless" approach is error prone because someday you might add a line
like this: like this:
if (foo) if (foo)
stmt; stmt1;
new_line(); new_line();
else stmt2
stmt; stmt3;
And the resulting behavior of your program would totally bewilder you. (Don't And the resulting behavior of your program would totally bewilder you. (Don't
laugh, it happens to us all.) Remember folks, this is C, not Python. 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
to more modern compilers and the old declaration syntax is neither necessary to more modern compilers and the old declaration syntax is neither necessary
nor desired. nor desired.
Emphasizing Logical Blocks
~~~~~~~~~~~~~~~~~~~~~~~~~~
Organization and readability are improved by putting extra newlines around
blocks of code that perform a single task. These are typically blocks that
begin with a C keyword, but not always.
Furthermore, you should put a single comment (not necessarily one line, just
one comment) before the block, rather than commenting each and every line.
There is an optimal ammount of commenting that a program can have; you can
comment too much as well as too little.
A picture is really worth a thousand words here, so here is an example that
illustrates emphasizing logical blocks:
while (line = get_line_from_file(fp)) {
/* eat the newline, if any */
if (line[strlen(line)-1] == '\n') {
line[strlen(line)-1] = '\0';
}
/* ignore blank lines */
if (strlen(file_to_act_on) == 0) {
continue;
}
/* if the search string is in this line, print it,
* unless we were told to be quiet */
if (strstr(line, search) && !be_quiet) {
puts(line);
}
/* clean up */
free(line);
}