This post originated from an RSS feed registered with Java Buzz
by Carlos Perez.
Original Post: Non-Static Method Calls Considered Harmful?
Feed Title: .:Manageability:.
Feed URL: http://www.manageability.org/blog/stuff/rssDisabled?portal_status_message=Syndication+is+Disabled
Feed Description: Random thoughts on the manageability of complex software.
Kasia and Simon are both recommending that .toString() never be called. Rather, they recommend String.valueOf() to avoid the dreaded NullPointerException. Well it's a pretty good tip especially within catch blocks. However, extrapolating, should non-static method calls ever be used at all?
That's an extremely radical proposal and I don't think its going to fit well with many people. However, here's a rule of thumb, wrappers of primitive types, strings or even value types should never be null. They should always be their default value.
Now you have a choice, either enforce this stringent requirement, or never call a method of a value type, always use an intermediary that checks for null. So two choices, pick your poison!
This incidentally has been discussed in the Tiger release in the context of autoboxing. The original choice was not to throw an exception when a primitive wrapper was null, rather than convert it to its default. However, that would have masked too many errors and is semantically awkward, so the choice was to throw the NPE.