Go to the first, previous, next, last section, table of contents.

Clean Use of C Constructs

Please explicitly declare all arguments to functions. Don't omit them just because they are ints.

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 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.


Go to the first, previous, next, last section, table of contents.