[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.3 Clean Use of C Constructs

Please explicitly declare the types of all objects. For example, you should explicitly declare all arguments to functions, and you should declare functions to return int rather than omitting the int.

Some programmers like to use the GCC `-Wall' option, and change the code whenever it issues a warning. If you want to do this, then do. Other programmers prefer not to use `-Wall', because it gives warnings for valid and legitimate code which they do not want to change. If you want to do this, then do. The compiler should be your servant, not your master.

Declarations of external functions and functions to appear later in the source file should all go in one place near the beginning of the file (somewhere before the first function definition in the file), or else should go in a header file. Don't put extern declarations inside functions.

It used to be common practice to use the same local variables (with names like tem) over and over for different values within one function. Instead of doing this, it is better to declare a separate local variable for each distinct purpose, and give it a name which is meaningful. This not only makes programs easier to understand, it also facilitates optimization by good compilers. You can also move the declaration of each local variable into the smallest scope that includes all its uses. This makes the program even cleaner.

Don't use local variables or parameters that shadow global identifiers.

Don't declare multiple variables in one declaration that spans lines. Start a new declaration on each line, instead. For example, instead of this:

 
int    foo,
       bar;

write either this:

 
int foo, bar;

or this:

 
int foo;
int bar;

(If they are global variables, each should have a comment preceding it anyway.)

When you have an if-else statement nested in another if statement, always put braces around the if-else. Thus, never write like this:

 
if (foo)
  if (bar)
    win ();
  else
    lose ();

always like this:

 
if (foo)
  {
    if (bar)
      win ();
    else
      lose ();
  }

If you have an if statement nested inside of an else statement, either write else if on one line, like this,

 
if (foo)
  ...
else if (bar)
  ...

with its then-part indented like the preceding then-part, or write the nested if within braces like this:

 
if (foo)
  ...
else
  {
    if (bar)
      ...
  }

Don't declare both a structure tag and variables or typedefs in the same declaration. Instead, declare the structure tag separately and then use it to declare the variables or typedefs.

Try to avoid assignments inside if-conditions. For example, don't write this:

 
if ((foo = (char *) malloc (sizeof *foo)) == 0)
  fatal ("virtual memory exhausted");

instead, write this:

 
foo = (char *) malloc (sizeof *foo);
if (foo == 0)
  fatal ("virtual memory exhausted");

Don't make the program ugly to placate lint. Please don't insert any casts to void. Zero without a cast is perfectly fine as a null pointer constant, except when calling a varargs function.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

This document was generated by anoncvs update for www on February, 21 2004 using texi2html