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
- Use descriptive names for globals, short names for locals (3).
- Be consistent (4).
- Use active names for functions (4).
- Be accurate (4).
Style: Expressions and Statements
- Indent to show structure (6).
- Use the natural form for expressions (6).
- Parenthesize to resolve ambiguity (6).
- Break up complex expressions (7).
- Be clear (7).
- Be careful with side effects (8).
Style: Consistency and Idioms
- Use a consistent indentation and brace style (10).
- Use idioms for consistency (11).
- Use else-ifs for multi-way decisions (14).
Style: Function Macros
- Avoid function macros (17).
- Parenthesize the macro body and arguments (18).
Style: Magic Numbers
- Give names to magic numbers (19).
- Define numbers as constants, not macros (20).
- Use character constants, not integers (21).
- Use the language to calculate the size of an object (22).
Style: Comments
- Don't belabor the obvious (23).
- Comment functions and global data (24).
- Don't comment bad code, rewrite it (25).
- Don't contradict the code (25).
- Clarify, don't confuse (26).