Contracts vs debug

Simen Kjærås simen.kjaras at gmail.com
Sat Feb 4 11:43:12 PST 2012


On Sat, 04 Feb 2012 18:18:22 +0100, F i L <witte2008 at gmail.com> wrote:

> Why/where should I use contracts vs debug statements? Is it completely  
> arbitrary? If so, I wonder if contracts syntax is even needed:
>
>     int foo(int bar)
>     in
>     {
>         assert(bar != 0);
>     }
>     body
>     {
>         return bar + 1;
>     }
>
> The thing I like more about debug statements, is that I can put them  
> anywhere in my code, testing parameters and locals in the same way. If  
> "for documentation" is the only argument for contracts, I find that a  
> bit weak.
>
>     int foo(int bar)
>     {
>         debug assert(bar != 0);
>
>         return bar + 1;
>     }
>
> That is much cleaner syntax and just as easy to understand from a  
> assertion-failure/documentation standpoint IMO.

The idea is also that contracts will be inherited. A subclass may
relax the 'in' contracts and strengthen the 'out' contracts. I am
not sure if this currently works, but that is the idea.


More information about the Digitalmars-d-learn mailing list