On 02/03/2013 11:14 PM, Peter Alexander wrote:
On Sunday, 3 February 2013 at 22:00:05 UTC, bearophile wrote:
Nick Sabalausky:

Why is it silly? (Genuine question)

"Silly" wasn't the right word, sorry.

But generally if a language offers you a clean feature (D contract
programming is designed clean enough) it's better to use it, when you
clearly need it.

I don't use D contracts, even though I use asserts.

I find that adding contracts bloats my code quite a lot, making it less
readable.

real log(real x)
in
{
     assert(x > 0);
}
body
{
     return ...;
}


If the code is written consistently in this vertically-bloated style, it is 'less readable' anyway. Contracts will not make a huge difference. :o)


v.s.

real log(real x)
{
     assert(x > 0);
     return ...;
}


real log(real x) in{ assert(x > 0); }body{
    return ...;
}

real log(real x) in{
    assert(x > 0);
}body{
    return ...;
}

As far as I'm aware there is no difference except when inheritance is
involved, so it's an easy choice for me.

The difference is that the first version of log given above may be correct whereas the second is certainly buggy (pass in 0 and it fails). As correctness is not necessarily modular, the entire code base as a unit may still work correctly if it contains either version. That's why your approach is practicable in the absence of a tool/review process that checks assertions in a modular way.

Reply via email to