On 04/14/2011 02:56 AM, bearophile wrote:
Andrei:
I disagree. I find examples that use assert() much clearer than examples
that print something to the console and then explain in a comment what
you ought to be seeing.
I don't understand why you say that. Isn't learning and understanding based on feedback?
If all the examples do is to show the same "assert passed" message, then what's
the point in running the examples? Just reading a static page gives the same insight. You
are able to modify them, of course, and see the modified asserts fail, and you may add a
writeln statement yourself, but... having to modify each example to see some feedback is
not a good default. I'd like to know if other people agree with you on this.
I more or less agree with both of you. My problem with using asserts in code
samples is:
* It's not a feature all languages have, thus readers may not know it (well).
* Asserts in sample is clever "highjacking" of a language feature for a clever
side-effect.
The point of assert in true code is to catch a potential bug. The point of
assert in sample code is to bring information to the reader, like if saying
"this predicate holds here":
assert(3/2 == 1);
Maybe an alias of assert for such use would help? (but I cannot find one) Or a
simple comment would do the job better:
// 3/2 == 1
I long for a different assert that would take one expression and a result, and
writse out when 'assertMode' is 'verbose':
assert(3/2, &);
// outputs "3/2 : 1"
Then, we get both silent and verbose asserts in one go; the former for
regression testing, the latter mode for diagnose or examples.
I currently do it that way:
auto n = 3/2;
writefln("3/2: %s, n); // commented out for regression test
assert(n == 1);
Denis
--
_________________
vita es estrany
spir.wikidot.com