M
Michael Wojcik
assert should be used to find bugs in your program.
My point is precisely that no, it shouldn't, because it's a terrible
error-handling mechanism. It provides no recovery and poor reporting.
Example:
for (i = 0; i < n; ++i) a = 0;
assert (i >= n && (n < 0 || i == n));
After execution of the loop, i will be equal to n, except if n was
negative. There will be no assertion unless I made some mistake in this
tiny bit of code. If I made a mistake, then how could I possibly handle
it?
By logging the problem, using the program's logging mechanism (which
could be trivial, if it's a trivial program, or complex if it isn't),
and returning an error to the caller.
Incorrect inputs are not bugs in the program and therefore should never
be handled by an assert.
True, but too narrow. Nothing should ever be "handled" by assert,
because it's an astoundingly poor way of "handling" things.
And please note that in this thread Chuck and I were both discussing
incorrect input to low-level functions, which could very well be bugs
in the program (such as passing a null pointer to a function which
requires a non-null one).