in reply to Pre and Post Condition

Yes.

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
    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)) ...
    Use a constant:
      
    use constant DEBUG => 0; ... sub foo { sanity_check(...) if DEBUG; }
    This has the nice effect of optimizing away the entire sanity check statement in production code, without employing a preprocessor/text-filter.
       MeowChow                                   
                   s aamecha.s a..a\u$&owag.print
Re: Re: Pre and Post Condition
by princepawn (Parson) on Aug 23, 2001 at 03:15 UTC
    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.
    That's why I like Carp::Datum. It comes with a pre-condition stripper that you can use once you are read to go into production.