Monday 13 October 2003 — This is 21 years old. Be careful.
OK, so usually Joel Spolsky is a pretty bright guy, and even when his ideas seem wacky or overstated, there’s something true and useful about them. But he’s dead wrong about exceptions. Joel’s opinion (that status returns are the only way to go and that exceptions are akin to the dreaded goto) runs so counter to modern thinking that the ensuing discussion is considering the possibility that he was being subtly ironic, or that his site had been hacked.
Of course, I’ve made my opinion clear on this already. See my article: Exceptions vs. status returns. Update: I’ve added a section to that article rebutting Joel’s points specifically.
Comments
I used to work with an old school C programmer who insisted on using K&R style C function declarations. He'd been writing C code since Bjarne Strostrup was in short pants. He wasn't about to change. His code was extremely well written and careful as hell but just try to get someone else to even touch it.
Error checking is vitally important, yet poor programmers traditionally don't do it. Exceptions make it easy for them to do error checking in a minimally acceptable way, which is a great improvement over what they would do otherwise.
However, exceptions make it more difficult for competent programmers to write truly excellent error checking code. Sure, they too can easily write decent error checking code with it, but to write excellent code, they have to treat the exceptions just like status returns - catch every possibility after every call.
Most programmers are not very good. So, all in all, exceptions are a good thing, overall. But don't pretend that they're superior to status returns when in the hands of the competent.
The one key distinction between exceptions and error return codes is that exceptions are less easily ignored. You either have to catch the exception and deal with it or some other code that calls you will. Ignoring a return value from a function is just too easy.
Also in languages such as Java and C#, being able to get access to a stack trace in an exception catch block is truly wonderful.
However, given that other languages don't necessarily provide that kind of info as easily, that alone is a good reason for carefully constructed exceptions.
Just as there are poor examples of OO design, there exist examples of poor exception handling (given that exceptions are objects). Blaming the language (or indeed making blanket statements about the "one true approach") rather than the shortcomings of an individual programmer seems a bit daft to me.
http://www.joelonsoftware.com/items/2003/10/15.html
Ball is in your court.
--bob
I didn't say that exceptions make it more difficult to write decent error checking code. In fact, I said the exact opposite - that exceptions make it easier to write decent error checking code.
The problem is that they make it more difficult to write better than decent error checking code.
In a world full of poor programmers, the latter detriment is more than offset by the former benefit - decent error checking code is much better than the error checking code that otherwise would be produced by the average programmer.
But they bring good programmers down to average. That's why I said that they're a "bring to mean" mechanism.
The hard part of writing code in the face of possible errors is to handle all of the cases and be prepared to deal with them. Detecting errors is easy, having a plan to deal with them in an appropriate way is much more work.
Add a comment: