False Is Not An Error

Stupid title, possibly stupid idea. I was trying to illustrate why can be a bad idea (harmful?) to use a boolean return type as an a success/failure flag, but after writing a page of nonsense I'm no closer to a decent explanation. Can anyone else do better? Something about return values being ignorable. Almost like a licence to:

 try {
     SomethingCrazy();
 }
 catch {}

I'll have another go tomorrow.


Not sure what point you are trying to make, but on the surface it seems like the ExceptionsVsErrorCodes? argument.

FWIW, I don't see use of boolean return codes being a smell, as long as all the caller cares about is whether it worked or it didn't work. If it doesn't matter whether it worked, then ignoring the return value is fine. I prefer exceptions, but many people still use programming languages that don't support them, or programming styles that are contraindicative.

Something that is a bit of a smell is the "return 0 on error; return a meaningful result on success" pattern, or the "return 0 on success; return a non-zero error code otherwise" pattern. If the entire API follows one of those patterns, then it is not too bad, but when different functions act in different ways, it is too easy to make mistakes. When you see code like this:

 if (!do_something())
 {
    do_something_else();
 }
it is not obvious whether do_something_else is executed on success or on failure of do_something. (Real programs would probably have more meaningful names than do_something and do_something_else, but it is still easy to have confusion.)

See also ErrorValue, CodeWithoutExceptions, AvoidExceptionsWheneverPossible, InBandSignal


EditText of this page (last edited July 12, 2004) or FindPage with title or text search