Code of the day

One of my friends just sent me a code sample and I thought it would be a good idea to share it. It’s taken from a real project and it was written by someone with the title of “Software architect”.

Code sample:

try{
    someObject.someMethodName();
}catch(NullPointerException e){
    throw new BusinessException("the object is null")
}

And no, it’s not a joke.

3 Responses to “Code of the day”

  1. mihai Says:

    I don’t get it. Is there something wrong with that code? I can’t see the problem. I only used php up to this point and I don’t know much of action script. Sorry for the silly question, but it’s been on my mind ever since I read this post.

  2. cornel Says:

    Hey, there are no silly questions :)
    The code is in java not actionscript but this is not important.
    There are 2 problems:
    a)is ugly as hell, and is throwing an so called businessexception instead of checking the validity of the parameter (it should have been something like that:
    if (someObject==null)
    throw new IllegalArgumenException(“the parameter is null”);
    someObject.someMethodName();
    b)Is wrong and dangerous. Imagine that someObject is not null but during the execution of someMethodName() another object is null and the code will throw NullPointerException. The code will catch it and it will throw the BusinessException with the wrong message and the poor developer will not able to understand where is the problem and it will have to run step by step the code.
    c)I do not like the title “software architect” but that’s a personal opinion :) A good article related to that is http://martinfowler.com/ieeeSoftware/whoNeedsArchitect.pdf

  3. mihai Says:

    Thanks. Now that you broke it down and explained it, it actualy makes a lot of sense. At first, I thought that the title was the real subject of the post but it didn’t make sense tu put the code sample there. Anyway, thanks for the reply.

Leave a Reply