Chapter 1: Deffensive programming
- Assume nothing. Unwritten assumptions continually cause faults, particularly as code grows.
- Employ a Good Coding Style and Sound Design
- Don’t Code in a Hurry
- Trust no one. Absolutely anyone—including yourself—can introduce flaws into your program logic. Treat all inputs and all results with suspicion until you can prove that they are valid.
- Write Code for Clarity, Not Brevity
- Keep it simple (KISS)
- If it can’t be maintained, your code is not safe.
- Simplicity is a virtue. Never make code more complex than necessary.
- Compile with All Warnings Switched On: Compiler warnings catch many silly coding errors. Always enable them. Make sure your code compiles silently.
- Use Static Analysis Tools
- Use Safe Data Structures
- Check Every Return Value
- Handle Memory (and Other Precious Resources) Carefully
- Treat all scarce resources with respect. Manage their acquisition and release carefully.
- Initialize All Variables at Their Points of Declaration
- Declare Variables as Late as Possible: and do not use the same variable for sematically different way.
- Use a Good Diagnostic Logging Facility
- Providing default behavior, especially if it is erroneouse.
- Checking numeric limits
- Make everything as const as you possibly can (final, in java)
- Make Preconditions, postconditions, invariants, assertions
Jun 10, 2010
comments powered by Disqus