developer's mag
main page
article
part 1
part 2
part 3
part 4
part 5
part 6
part 7
part 8
|
4 - Using ASSERTING()
Now you have assertions - now what? With
this easy to use version, consider peppering
your code with it. At a minimum:
- Use it validate function inputs and outputs
- if a function is someday called with
bad
input, ASSERTING() will catch it.
- Use it to confirm invariants - items that
shouldn't change in a section of code.
Check
both on entering a section and exiting.
- Use it to inject special time-consuming code.
For instance, if you are coding a sort, you could
create a routine to check the sort,
and place the call in an ASSERTING().
Production code won't have the time-consuming
check, while your debug version will.
Obviously, these are only a few ideas. Key
to it all is that you are asserting a fact
about the code, an assertion that the failure
of which is going to cause you grief down
the line. By checking everything immediately,
you avoid that grief later on.
This column's program
includes all of the
necessary tool source, and is a simple tester,
allowing you to trigger a variety of nifty
errors and let you see the errors caught
by the tools. For assertions, you can see
what turning them off or breaking to the
emulator is like. As well, there are a variety
of errors generated for capture by our third
and final tool.
Previous Section
Next Section
|