Another rant in the I'm Gonna Yak series....
Listen closely: NullPointerException doesn't tell me dick about where to look in your code!
If you recall the Optimistic Programming Model post, I'm talking specifically to you people! It is precisely your laissez-faire attitude towards checking for null that screws everyone over, by adding wasted time to the diagnosis of your crap.
"Oh, it will always be configured correctly" is a fucking pipedream, my friend. The first other person to come along and use it will certainly forget to set something, and now they are left with tracing through your crap in the IDE; not really being productive on their task anymore. Or worse yet, giving up (no access to source code) and looking elsewhere or building it. That's not really how I want to discover I needed to set something. Documentation? Yea, like you made any!
Please reconsider your foul ways! Switch over to the attitude that says: Make The System Tell Me What's Wrong! I know, this requires you to be pro-active about helping others, but it pays everyone back in a hurry! I want the You Forgot To Set The 'Important' Property exception!
Look! See how easy it is!
if(this.Important == null)
throw new ArgumentException("The 'Important' property was not set");
this.SomeThingImportant(this.Important, otherStuffThatCanBeNull); It only took you two lines of code to enhance your system, so now instead of the vague NullPointerException, you now know exactly what it was looking for as well!
In the general case, this technique consists of a series of guard clauses, before any real work is done. This is nice from an organizational standpoint; it puts all the validation in one place, and the "rest" of the method is clean-looking, because you did all the messy validation already.
In the most-general case, we are talking about input validation. If you have ever used FxCop on your code (go do it now if you haven't) you know those messages. I went back and appeased every "public parameter blah not validated" message, and I have never looked back; I do it every time now.
The time to switch is now! You are screwing yourself over along with the rest of us....
No comments:
Post a Comment