I read an interesting blog post today by Andy Palmer. I thought it was a good point and a principle I am trying to encourage on the software projects I work on.
I agree that a method should not return null when it doesn’t have a return value. There will be a reason why a return did not happen. I agree that throwing exceptions is a good way to indicate a valid return object was not possible because of an error condition, but I also have plenty of cases in my current project where something requests some data and there can be a good reason for it not being available. In these cases I am encouraging people to build return classes to pass back the value returned or a reason why one couldn’t be. I feel while this obviously results in more code, there is nothing more irritating then a NullReferenceException being thrown when you know the reason the value is null because of an event that has already happened. I feel if I never purposely make a null occur in my software, then a) I never have to check for them, and b) when one occurs, I immediately know there is something unexpectedly wrong.
The post is here: http://andypalmer.com/2008/08/returning-null-considered-dishonest/