C Coding Standards Summary

Do's:

Don'ts Warnings:

Naming Conventions

  • Names should be meaningful in the application domain, not the implementation domain. Note that well-structured code is layered internally, so your implementation domain is also the application domain for lower levels.
  • Names should be chosen to make sense when your program is read. Thus, all names should be parts of speech which will make sense when used with the language's syntactic keywords. Variables should be noun clauses.
  • Boolean variables should be named for the meaning of their "true" value. Procedures (functions called for their side-effects) should be named for what they do, not how they do it.
  • Function names should reflect what they return, and boolean-valued functions of an object should be named for the property their true value implies about the object. Functions are used in expressions, often in things like if's, so they need to read appropriately. For instance,
    if (checksize(s))
    
    is unhelpful because we can't deduce whether checksize returns true on error or non-error; instead
    if (validsize(s))
    
    makes the point clear and makes a future mistake in using the routine less likely.
  • Module globals should have a short prefix in names identifying the module, e.g. "wgl_" to avoid name clashes when combining independently developed modules.
  • Use some consistent scheme for naming related variables. If the top of memory is called physlim, should the bottom be membase? Consider the suffix -max to denote an inclusive limit, and -lim to denote an exclusive limit.

    The Standard Library

    Some notes on using particular functions:
  • Never use gets(). Use fgets instead so that you can be sure that you don't overflow your buffer.
  • Beware of library functions that have state and are called multiple times, especially in a loop. These functions include but are not limited to: strtok() and getchar() (use fgetc() rather than getchar()).