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, 14 October 2003 04:47:27 (GMT Daylight Time, UTC+01:00)
Agreed.

http://www.jroller.com/page/cpurdy/20031013#poor_sod
Tuesday, 14 October 2003 05:04:32 (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, 14 October 2003 12:19:02 (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.

Thad
(codemonkey_uk)
Thursday, 16 October 2003 03:24:32 (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. :)

Rachael
R343L
Friday, 17 October 2003 04:52:39 (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...

didier
- d-
Comments are closed.