Wednesday, January 07, 2009

Exception vs. Error Code - round 2

This post is a follow up of my previous post on the same topic Exception vs. Error Code - round 1. Here I would like to discuss what Uncle Bob and other Object Mentors' view on this topic which was explained in the their book Clean Code.

First of all their view is definitely pro exception, and the two major reasons given against returning error code were:

a. Returning error code is a subtle violation of Command-Query-Separation (CQS) principle. (page 46)

Take a look at the following code sniplet:


if(connection.close() == IO_ERROR){
handleIoError(connection);
}


The close() method is clearly a command but by returning the error code this method offers both query and command style invocation thus violates the CQS principle.

b. Returning error code clutter the caller code and since its easy to miss handling certain type of error code therefore it is also error prone. (page 104)

This point is consistent with Kent Beck's view which I have discussed in the previous post.

As Unble Bob mentioned programming is sort of like martial art. Different master will form their own style and some one can choose to practice certain style exclusively or a mix of styles, but when certain practice is criticized by more than one master then you better take it seriously and rethink your practice because there is probably something wrong with that.