Thursday, May 22, 2008

Important to read, essential to act

I just recently finished reading From Naked Ape to Superspecies which a friend of mine recommended on her blog, but let her speak for herself: LittleBigGirl's adventures away from home: A profoundly important book.

I just want to join her appeal and recommend - even urge- everyone to get hold of a copy of this excellent book. It is no secret that we bit the hand that feeds us, but to which extend it happens and how systematically we destroy planet earth is incomprehensible. It is time we rethink our value systems and start acting responsible. If everyone takes a small step it will be a giant leap for planet earth ;-)

Tuesday, May 20, 2008

Explicit NullPointerException

I am reading a book on JavaServer Faces (The Complete Reference.....) since I am supposed to teach the subject next week at a course on Java and web programming. One code example got me started on one of my favorite subjects - the abundance of null checks in source code. This is the example code I saw inside a custom converter example class:

public String getAsString(FacesContext context,
    UIComponent component,
    Object value)
{
    if( context == null || component == null )
    {
        throw new NullPointerException();
    }
    // and obviously some more code here....
}

This doesn't make sense at all since if the params are null there will eventuelly be a NullPointerException raised anyway. This method is supposed to be called by the JSF framework so it makes even less sense. Why would a framework call a custom converter class with null values? I saw similar code yesterday in the huge code pile I am working with now for a customer. The difference was that a checked exception was thrown instead of an unchecked. That is even worse since every caller of the method need to deal with that exception and - in this case - the source fault (null value of a param) was not mentioned at all. Just a generic "this was not good" message. I suggest that null values can take care of themselves. If someone passes in null for a param that never should be null a NullPointerException is a brilliant outcome with the stack trace showing us exactly where the null value was discovered.