Re: Assertions in principle
On Mar 4, 1:33 pm, Kai-Uwe Bux <jkherci...@gmx.net> wrote:
Roland Pibinger wrote:
On Mon, 05 Mar 2007 07:44:00 +1300, Ian Collins wrote:
Roland Pibinger wrote:
asserts may check some aspects of
contracts in non-release settings but are not appropriate to 'enforce'
them in release mode.
Why are they not appropriate to 'enforce' them in release mode? If
something you trust breaks that trust, would you rather experience a
random, possibly damaging, failure or a controlled one?
assert is a means of finding bugs in your code. What you check at
runtime in your released program is something different (though
necessary and useful).
I feel this is going around in circles. As for a concrete example, I find
that g++ sometimes crashes upon me. Usually, it dies with a friendly
invitation to send in a bug report. It even gives some file and line number
info. I am pretty certain that the developers left some sanity check
assertions in g++. I highly appreciate that for the following reasons:
a) I prefer g++ crashing over generating faulty code. If I had no indication
that there was a problem with the compiler and the generated program
behaves not as expected, I would start searching for a bug in my code. That
could be a tremendous waste of time.
The only problem apparent in this situation is that gcc has crashed.
Had the compiler not crashed, the compiler would have gone to produce
either a correct or a faulty binary. Since the vast majority of the
time, gcc produces the former, it is much more likely that this crash
is preventing gcc from producing a correct build than it is somehow
avoiding an incorrect build.
b) I can use the file and line number info to check the bugzilla database
and see whether the bug has already been reported.
In other words, you can spend time debugging your C++ compiler instead
of your own C++ programs.
Do you think, the compiler would be a better program with those assertions
turned off?
I think that a better compiler would have the asserts turned off. The
point here is that a more thoroughly-tested compiler would see no
benefit from shipping with its asserts enabled. Shipping a program
with asserts enabled can only mean that the software has not been
adequately tested. So in this case, anyone who uses g++ is
effectively participates in the product's QA.
And while such an arrangement may be reasonable for a free product
like g++, it probably won't fly for those who program with a $500 C++
compiler. Customers who pay money for software do so with the
expectation that the software will be tested before it is shipped and
that it will run reliably after it has shipped. Leaving asserts
enabled in a shipping program has the completely opposite effect - the
asserts make the shipping program less reliable (that is, it is more
prone to fail), and all but eliminates the chances that the developers
will have tested the software before it shipped - and not just rely
on those who use the software to make up the difference.
Greg