[prog] C programming - capturing information sent to stdout

Mary mary-linuxchix at puzzling.org
Thu Jul 14 12:33:17 EST 2005


On Thu, Jul 14, 2005, Sue Stones wrote:
> Yes but I am asking where the boundaries of that expectation are.  At
> one level you could say that if you know enough to put a assert
> statement in there then you are expecting an error, so assert
> shouldn't be used.  Using assert to determine if preconditions have
> been met, says that you expect that some programmers will not comply
> with the preconditions, so assert shouldn't be met.

Hmmm, let me put it a different way: asserts are for a programmer
audience. If your "users" are really other programmers who are
calling your library and want to use it correctly, then asserts are
often appropriate and friendly: far better to let them know nice and
early that what they're doing is wrong and needs to be fixed.

Contrast non-programmer end users, people using your code for some other
purpose than "to write more code". In many many many cases these users
want either a friendly message, some attempt to recover from the error,
error logging, alerts to on-call staff: almost always something more
complex than "imemdiately die".

So, I guess my position is really that asserts aren't all that useful in
complex programs for the case of "oh wow, something's gone wrong with
the way I was used" because users of complex programs are likely to want
behaviour more complex than "everything disappears with short error
message about an assertation failing."

The situation you want to use them in: to slightly simplify the code in
a situation where you are pretty much doing "print error, die
immediately" anyway sounds OK. But "print error, die immediately" is
*all* that assert does.

As far as I can see, situations where you want "print error, die
immediately" are:
 - during development when you want to find the error and eliminate it
 - in simple programs like scripts that read in a file and do something
   with it

However, there are plenty of cases where you want more:
 - any time you want to recover from an error, such as giving a user a
   second chance to open a file
 - any time you have complex error handling (like sending out a failure
   message to client programs, closing files and saving existing work on
   them, saving state in general)
 - any time you want to do error handling, do a recovery and have the
   program continue to run (like servers -- asserts are very nasty in
   long running servers)
 
Basically, what I'm trying to say is that asserts are not the magic
bullet of error handling. Their primary use case is debugging combined
with enforcing pre- and post-conditions. They do have a use for very
simple error handling like you're doing. But in my experience "print
error, die immediately" is actually a pretty rare and unusually simple
error response. In larger programs especially, error handling is a pain
and can comprise up to 75% of the code, and asserts are almost never
used in them except for debugging and automated testing.

-Mary


More information about the Programming mailing list