Roland Pibinger wrote:
> On 2 Mar 2007 08:08:55 -0800, "jeffc226@...com" wrote:
> 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.
Sure
>> 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.
Sure
>> 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?
Sure, if you put your foot down and tell your manager that "I don't care
if the deadline is tomorrow, it ain't ready!"
Library interface changes, if done incorrectly could cause a whole whack
of problems. Input interfaces may only be narrowed, never widened.
Output interfaces may be widened, never narrowed. You tell that to some
programmers and they will stare at you with a blank look, or worse argue
with you.
>> 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?
So, you keep the programme from corrupting persistent data causing a
cascade of other problems afterwards.
>> Even most test departments use release code, not debug code.
>
> Of course, they test the program that is shipped, not a debug version.
Of course.
>> 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.
Definitely.
>> 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.
Given the set of valid inputs, your system (since you defined the valid
inputs) should work always.
>> 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.
Yeah, well that goes to design and the human factor. If the computer
did everything for us, what would we do?
The debugger, though resistant to automation, is still required, even
when you use asserts since that is the only way to get access to the
system state.
I just wrote the other day a parser with many asserts in it, but trying
to find why the assert failed wouldn't have been possible without the
debugger (and a bunch of debugging classes and functions that I wrote).
Did I tell you I *hate* pointers?
> 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.
If the programme or library displays the failed asserts in
"production/release mode", I would be concerned. If they could be
redirected to a file which I could look at postmortem, I would be happy
as I would be able to talk to that company and resolve the issue in a
more timely fashion.
Asserts are good, bad and ugly part of programming. I can't wait till a
theorem prover can be properly build.
Man, this thread spans 4 days. Its interesting, but I'm reading the
rest of it later.
Adrian