[GD-General] Assertions, pre/post-conditions and call tracing
Brought to you by:
vexxed72
From: Brian H. <bri...@py...> - 2002-01-15 16:20:07
|
I've gotten into the habit of making tons of function pre-conditions and post-conditions that can optionally be compiled away. This is a Eiffel habit picked up second-hand from a former coworker of mine. In Eiffel, you can set up a bunch of safeguards to do basic sanity checking. The safeguards are pre-conditions (validate state and parameters before a method/"feature" is executed); post-conditions (validate state and parameters after a "feature" is executed); and class-invariants (check the integrity of the object after ANY "feature" is executed). Eiffel does this with "require" and "ensure" clauses that are analogous to assert() expressions in C++, but are a bit more readable. In addition, the "ensure" clause is guaranteed to be evaluated no matter how the function is exited (so you don't have to do a "goto ensure" all over your code to make sure you exit out the same place). I don't believe there's any clean way to do a class-invariant in C++, but I may be wrong on that count. The closest thing I can think of is making each class declare an InvarianceChecker class that is instanced at the beginning of each method and whose constructor/destructor pair do verification. Related to this is the ability to halt the debugger when a verification fails (which I assume can be done portably by * ( int * ) 0 = 0 ), and bring up a dialog box with A/R/F a la Microsoft. Finally, having a stack trace available so that I can see what function call list was causing the problem would be nice. I can do a lot of this with assert(), but I'm hoping there's a better way to do all this. I'm sure there might be some magic with exception handling that could be done but, as usual, I fear anything new in C++ that might not be portable. Anyway, I'm interested to hear what other types of infrastructure people have devised for this type of stuff. -Brian |