Contracts vs debug

F i L witte2008 at gmail.com
Sat Feb 4 09:18:22 PST 2012


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.


More information about the Digitalmars-d-learn mailing list