in reply to Pre and Post Condition
Years ago, I formalized a way to include preconditions in my C++ class documentation in a simple and useful way.
I specify which conditions are assumed vs validated. An assumed precondition means that you the caller better get it right, or the function will not behave as advertised. A validated precondition means that the code will catch this misuse (whether by throwing an exception, returning an error code, or whatever).
The concept of preconditions is very valuable for documentation, not just for making code super-robust. It lets you note the allowed range or restrictions on a function without cluttering up the prose description.
Checking some kinds of preconditions are done in the normal way: if n is greater than m, throw an error; etc. For ones that are not normally validated but you want in debug mode, in C++ the assert macro is often used. Without using a text preprocessor or waiting for Perl 6's lazy evaluation, I don't see an easy way to turn off checking by throwing a switch. The switch would be present in each statement: if ($debug && expensive_sanity_check(x)) ...
—John
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
(MeowChow) Re2: Pre and Post Condition
by MeowChow (Vicar) on Aug 23, 2001 at 10:14 UTC | |
|
Re: Re: Pre and Post Condition
by princepawn (Parson) on Aug 23, 2001 at 03:15 UTC |