Peter Alexander:
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 ...;
}
v.s.
real log(real x)
{
assert(x > 0);
return ...;
}
As far as I'm aware there is no difference except when
inheritance is involved, so it's an easy choice for me.
If you have to create some temporary values (or to perform some
computation) for your asserts you probably want to put this code
inside a {} to avoid such names to spill inside the body of the
function.
There are also post-conditions, that catch all the returns you
have in your function, unlike free asserts.
There are also loop invariants, missing in D contract programming:
http://d.puremagic.com/issues/show_bug.cgi?id=9300
Also, a well designed language enforces pre and post conditions
to not mutate values (this is an ongoing discussion in D:
http://d.puremagic.com/issues/show_bug.cgi?id=9408 ), and offers
you the "old" (prestate) feature only inside contracts (missing
still in D contract programming). Contracts also make life a
little simpler for static analysis tools (that aren't yet
available in D).
So the better contract programming is implemented in a language
(and its tools), the more useful it is compared to using free
asserts.
Bye,
bearophile