Villanova University
CSC 2400: Computer Systems I

Basic Rules of Programming Style


These rules of programming style are from The Practice of Programming (Kernighan and Pike). Italicized rules pertain to the process of programming; non-italicized rules pertain to the products of programming, that is, computer programs. The parenthesized number at the end of each rule indicates the page that describes the rule.

Style: Names

  1. Use descriptive names for globals, short names for locals (3).
  2. Be consistent (4).
  3. Use active names for functions (4).
  4. Be accurate (4).

Style: Expressions and Statements

  1. Indent to show structure (6).
  2. Use the natural form for expressions (6).
  3. Parenthesize to resolve ambiguity (6).
  4. Break up complex expressions (7).
  5. Be clear (7).
  6. Be careful with side effects (8).

Style: Consistency and Idioms

  1. Use a consistent indentation and brace style (10).
  2. Use idioms for consistency (11).
  3. Use else-ifs for multi-way decisions (14).

Style: Function Macros

  1. Avoid function macros (17).
  2. Parenthesize the macro body and arguments (18).

Style: Magic Numbers

  1. Give names to magic numbers (19).
  2. Define numbers as constants, not macros (20).
  3. Use character constants, not integers (21).
  4. Use the language to calculate the size of an object (22).

Style: Comments

  1. Don't belabor the obvious (23).
  2. Comment functions and global data (24).
  3. Don't comment bad code, rewrite it (25).
  4. Don't contradict the code (25).
  5. Clarify, don't confuse (26).