Thursday, September 11, 2008

Null or Exception?

Find = Null

Load = Exception


Probably about 2-3 weeks ago, while reading the thoughtblogs, I saw a few blogs argued about what should be returned from a load method, should it be a null or an exception. The same question came to me previously and left me wondering too.


While trying to provide an answer to this question, I chance upon a very simple logic when I first started to make use of Nhibernate. In scenario, when I am trying to check if a record is unique in the persistence layer, I always get an exception thrown from the Nhibernate whenever the record is unique as no similar record is found. I do not want to catch such an exception and recover from it explicit, as this is not an exception in this scenario as I do except a null record instead of an exception.


Searching around for an answer and after reading the Nhibernate document, I realize that I actually use the wrong method for this scenario. I have been using the LOAD method which I should actually use the FIND method. The LOAD method will always throw an exception whenever no record is found. The LOAD method is design to get something that you except that it must be there. Therefore, an exception will be thrown when nothing is found.


On the other hand, the find method allows you to search for record that might not exist. In this case, expecting a null record return is a norm. So why thrown an exception? Imagine when you were building a search engine and an exception is thrown when no similar record is found. Then you need to try catch such exception. Isn’t this redundant?


So from then on, I have tried to stick to this simple logic. All load method will thrown an exception and find method will not when no record is found. Use these methods in the appropriate scenario although they may be very similar. Interestingly, the answer to the question I have is not very far away and does not need to take a genius to answer it. It probably just lies somewhere around what we have been working with.


Sometimes, is good to ponder why some of the framework is design in such a way. If they are good, reference your design and code to it. This will also give your design and code with more strength as it is a true example.

Wednesday, September 3, 2008

Hug a Developer

Excellent video from devshop. This is something I can really relate to... and have to agree with both hands down. Can't believe that there is even a video to express what a developer had to go through.

Maybe can show this video to anyone who are interested to know what software developer does... enduring the PAIN...


But well, I dun remember taking any course at that age...thank god!


Hug a developer