Debugging
Debugging
There are two major phases of debugging activities:
- Preventing bugs
- Detecting/locating bugs
Well-established fact: It is much more expensive (in terms
of time and effort) to detect/locate existing bugs, than to prevent
them in the first place.
Thought Experiment
- Everyone, form small groups of 3-4 people each.
- Discuss the following question:
What are some
things you can do to prevent bugs from occurring in
your own programs?
- Goal: Come up with at least three or more different
answers in your group.
Bug Prevention
There are four cornerstones to preventing program bugs:
- Design
- Good writing
- Self-checking code
- Test scaffolding
Preventing Bugs Via Design
- Good modularization / firewalls
- Strong encapsulation / information-hiding
- Clear, simple pre- and postconditions
- Requirements of operations should be testable
Preventing Bugs Via Good Writing
- Clarity of writing and style
- Use simple expressions, not complex
- Use meaningful names
- Clarity of purpose for:
- Functions
- Loops
- Nested constructs
- Functions should only do a single thing
- Comments, comments, comments!!!!
- Small size of functions
- Use the smallest acceptable scope for:
- Variable names
- Static/local functions
- Use named intermediate values
- Use specifications (pre- and postconditions, invariants)
Preventing Bugs Via Self-Checking Code
- Assertions
- Checking assumptions
- "
assert" macro
- Custom "
assert" macros
- Assertions about intermediate values
- Loop invariants
- Module-level invariants
- Preconditions
- Postconditions
Preventing Bugs Via Test Scaffolding
- Test drivers
- Unit testing
- Integration testing
- Test suites
- Expected outputs
- Regression testing
Stephen Edwards <edwards@cs.vt.edu>
Last modified: Sun Mar 9 21:47:56 EST 1997