Debugging by Thinking: A Multidisciplinary Approach (HP Technologies)
9.11 Assert assumptions
9.11.1 Basic tactic
Insert assertions into the program source.
9.11.2 Purpose
Make the assumptions of the code explicit.
9.11.3 Questions answered
The questions answered depend on the assertion used. Common assertions answer these questions:
-
Which procedures are getting unexpected input values?
-
Which procedures are returning unexpected output values?
-
Which local variables are getting assigned unexpected input values?
9.11.4 Potential problems
-
Some assertions can be costly to test. Others that are made in frequently executed functions may be costly in the aggregate.
-
Some programmers don’t like having users see assertions print if a problem occurs. You must assess which of two options will cause more trouble for the user: Your program can continue execution in a known error state, or it can terminate the application and print a message that won’t be of much help to the user.
-
You must be careful not to include any code that has side effects inside of the assertion expression. If you have such code and you turn assertion checking off, you will change the behavior of the program.
9.11.5 Refined tactics
Make assertions preprocessor macros instead of procedures so they can be eliminated by redefining the macro.
9.11.6 Related tactics
Distinguish between fatal and nonfatal assumptions that are being violated. Report the nonfatal ones, but continue processing, possibly after remedying the problem.
9.11.7 Choosing tactics
Do a complete test run, with all assertions active, at least once before the program is put into production.
Категории