October 13, 2003
@ 11:23 PM

Joel Spolsky writes

The reasoning is that I consider exceptions to be no better than "goto's", considered harmful since the 1960s, in that they create an abrupt jump from one point of code to another. In fact they are significantly worse than goto's:

  1. They are invisible in the source code. Looking at a block of code, including functions which may or may not throw exceptions...
  2. They create too many possible exit points for a function. To write correct code, you really have to think about every possible code path through your function...

A better alternative is to have your functions return error values when things go wrong, and to deal with these explicitly

Whoa, this is probably the second post from Joel that I've completely disagreed with. Exceptions may suck (unchecked exceptions especially) but using return values with error codes sucks a lot more.

Tuesday, October 14, 2003 4:47:27 AM (GMT Daylight Time, UTC+01:00)

Tuesday, October 14, 2003 5:04:32 AM (GMT Daylight Time, UTC+01:00)
Personally, I'd rather never return to the days of wrapping practically every line of code in an "if" statement.
Scott Swigart
Tuesday, October 14, 2003 12:19:02 PM (GMT Daylight Time, UTC+01:00)
Joel is of course wrong on several counts.

The comparison to goto is a silly one; Exceptions only jump *up* the call stack. Goto jumps the IP anywhere in the code. Goto creates not just multiple exit points for a block (as, I must point out, does the 'return' statement, and as does the 'break' statement) but multiple *entry* points.

As to the second point, exceptions create no more exit points in the code than the same functionality implemented as return codes. What they do do is hide those exit points, as the possible exit-on-error is not explicitly implemented at the point of call of the function.

The implication that this means that correct code is harder to write ... well, again this just shows that Joel is not comparing like with like. If all the possible exit-on-error conditions are covered, you are dealing with the exact same level of complexity.

Using the RAII idiom managing with "all the return paths" is almost automatic, and is something you should be doing anyway, even if your writing code that uses error code-returns.

I wish high profile developers like Joel would actually think before firing off sound bites like that. It makes reeducating broken programmers that much harder than it is already.

Thursday, October 16, 2003 3:24:32 AM (GMT Daylight Time, UTC+01:00)
I like Joel's articles, but that whole exception thing was silly of him. But he's backpedaling on it now. :)

Friday, October 17, 2003 4:52:39 AM (GMT Daylight Time, UTC+01:00)
Well, it (having exceptions) does prevent you from having 100% statement coverage (if it is required). Unless there is some way to run test cases that allow exceptions? That is a small tradeoff when you consider all that could go wrong without them. C comes to mind...

- d-
Comments are closed.