next up previous
Next: Types Up: Quality Issues in Routines Previous: Coupling

Defensive Coding

  There are many aspects to defensive coding. We mean defensive here in the same way defensive is used in defensive driving. You need to code to protect yourselves not from malicious people, but from errors others (you don't make any, do you?) make.

The premier defensive coding technique is the use of the assert-type statement. asserts should be use to check, at runtime, all assumptions used to generate the code. asserts check a condition and then exit if the conditions are not met.

Exception handling is a mechanism for dealing with known conditions that can be recovered from. The idea of the throw-catch metaphor is popular but not the only metaphor.

For large systems, the metaphor of a firewall is used to talk about damage containment. Concepts like information hiding and loose coupling help erect a firewall. The idea is to set up a safe area where all data is known to be correct. This allows private routines and data to be freely used without worrying about whether a particular data check has been made.

   table369
Table 15: Quality Checks in Routines



Steve Stevenson
Wed Feb 26 10:54:45 EST 1997