![[Dept of Engineering]](http://www.eng.cam.ac.uk/images/house_style/engban-s.gif)
Next: Common Difficulties and Mistakes
Up: More C++
Previous: Performance
``Code Complete'' by Steve McConnell (Microsoft Press, 1993) is in the CUED library. It has very
useful sections on testing and debugging, and quotes results from investigations of software quality
assurance. On p.567 it notes that the industry average for code production is 8-20 lines of correct
code per day. On p.610 it notes that industry average experience suggests that there are 15-50
errors per 1000 lines of delivered code. The recommendation is to design and code defensively - it
saves time in the end.
C++ is a strongly-typed language with many features to help write safe code,
but you still need to be cautious
- Let the compiler help you as much as possible! Many compilers generate warning messages
if you ask them to.
- Assume when you're writing your program that it won't be bug-free. Write it so that you
(and others) can easily debug it: make routines small and control flow simple.
- Concrete examples are easier to debug. You can make them into a template
once they're working.
- Write a test suite (for you and future users)
- Don't get stuck at one level - zoom in and pan out. Your code should
be understandable locally (parts should be as isolated from other
parts as possible) and at the top level.
- Write ``defensive'' code. Always assume the worst.
- It's not uncommon for beginners to prove to themselves on paper
that their broken code is correct and the compiler is wrong. Add cout
statements to print out intermediate values and test assertions.
- If you're using floating point arithmetic, check divisions to see if you're dividing by zero.
- Check that you're not going off the end of arrays.
- To comment-out large chunks of code bracket it with
#if 0
...
#endif
See the Debugging handout
for details of the
wdb debugger.
Next: Common Difficulties and Mistakes
Up: More C++
Previous: Performance
Tim Love
2001-07-05