On 2 Mar 2007 08:08:55 -0800, "jeffc226@...com" wrote:
>This might be less of a design issue than a C++ language issue per se,
>but I have a problem with assertions. I mean, they work, of course.
>But there's something I'm uncomfortable with that's never been
>explained to my satisfaction.
>
>I recently read this explanation. "There is an effective litmus test
>to differentiate the cases in which you need to use assert and when
>you need to use genuine error checking: you use error checking for
>things that could happen, even very improbably. You use assert only
>for things that you truly believe cannot possibly happen under any
>circumstances. An assertion that fails always signals a design or a
>programmer error - not a user error."
This is a very good explanation (source?). assert is for finding bugs
in a program. Input validation, error handling, ... is not done with
asserts. Therefore asserts should never be used in production
(release) code.
>OK I can buy that. But what keeps going unmentioned is that
>assertions are debug-mode only. Well, it's mentioned, but usually in
>the context of increased efficiency at runtime since they're compiled
>out.
Yes, that's their purpose. Actually, assert should be renamed to
debug_assert.
>I understand the idea that even the things you take for granted as
>being true might not be true. Software has bugs, and today almost
>every decent sized application uses third party supplements and
>interfaces. Interfaces change, code versions change, versions of code
>get mismatched. New permutations of hardware and software mixes are
>constantly occurring.
Well tested software has so few bugs that is can be smoothly used by
the client. And what have interface changes and mismatched code to do
with asserts?
>And where does all this manifest itself? At the developer's box?
>Sometimes. But the majority of time in a modern large application
>with many users, it's very likely for a bug to show itself in the
>field. And that is exactly where the assertion does not exist.
The bug crashes the program, assert calls abort which crashes the
program. So?
>Even most test departments use release code, not debug code.
Of course, they test the program that is shipped, not a debug version.
>What exactly
>is the point of checking for "impossible" error situations only at the
>developer's desk?
To find bugs in the program. Nothing more, nothing less. Together with
unit tests, functional tests, integration tests, ... it is one step to
produce a deployable program. Actually, assert can be seen as
automated test built into code.
>That just doesn't make sense to me. The code gets
>executed far too much outside of that environment, in ways the
>original developer might not even have imagined, for that to be good
>enough.
That just doesn't make sense to me. You write code that consists of
(public) interfaces to encapsulated code. You validate the input.
Since software is (should be) reusable it's no problem (even desired)
that it is used in ways 'the original developer might not even have
imagined'. You check the input as part of the contract between your
code and the caller. When the input is within the allowd range your
code must work.
>I would go so far as to say the original developer's box is precisely
>where assertions are NOT needed, because that's the only place where a
>debugger is available. (I see how they can come in handy, and force
>you to resist making assumptions.) But you really need assertions (or
>something) in environments where the debugger isn't available.
Using a debugger is the most inefficient activity in programming. It
is entirely resistant to automation. asserts help you avoid using the
debugger.
BTW, have you ever seen a program/library that prints/displays asserts
in production/release mode? I would be very reluctant to use that
program because the code quality most probably is very poor.
Best wishes,
Roland Pibinger
|