Handling Null Value in Your Code

2010-05-23

Is null value should be handled in your code? I found a good article on this topic.

In short here is the answer:

No, if you are wrting the code make sure that objects are always initialized and thus avoid creating null refernces to the objects.

Yes, there are times when you need to check for null. If you are writing a library that external developers will use, then you will probably want to check for null in all of your methods that are exposed as part of the API. (On your internal methods, you don’t need to since you have already cleansed the input.)

Also, if your code is being used as a callback to some API and you don’t know if it can ever pass null into your code, you probably should check for null.

0 comments: