the corner office

a blog, by Colin Pretorius

« Got m'monitor | Main | Swapped m'monitor »

# Deep Java Thought of the day

Assume a method is given an object as a parameter. You want to check whether this object is null before using it, because that's what good defensive programmers do. So you start thinking real deep thoughts about what sort of Error or Exception to throw. The main point being that if the method gets given a null reference, there's no graceful recovery required. Just skreem and die.

In a flash of brilliance, you realise that if you were to throw something called a NullPointerException, you'd get the message across quite succinctly.

Then you realise that this is what the JVM will do, anyway. So is there any worth to explicitly checking whether the object is instantiated or not, as good defensive programmers are wont to do?

And if you decide not to do an explicit check, then how do you make sure that the next person coming along doesn't just think you were sloppy? Do you start the method off with something like:

// I haven't checked if the object is null since the JVM would do a better job
...

'Cause that could get really messy.

File under: java : {2004.09.22 23:23}

Comments:

1. Ben Poole (2004.09.24 - 10:59) #

Dont check it. Dont comment, and dont apologise. Thats what I do ;o)

Seriously, it is: if null gets passed in, then the issue is with the code invoking the method  the checks should happen *there* in my view, and so your method should just stop dead. Im all for defensive coding, and in a previous life I would have tested the passed-in parameter, but now I dont  too much spaghetti code.

Crash early!

2. Senkwe (2004.09.24 - 19:44) #

I think it depends on what the method being invoked is doing. If it's supposed to use the object passed in to do some work, you have to revisit the biz rules and figure out if null is an acceptable input. If it's clearly not, I say throw an exception and leave it up to the code invoking the method to handle it :-) You've done your bit. Not sure about just stopping dead. I believe an app should just quit as soon as an exception is thrown because there's no such thing as an "expected exception" in my view. Clearly the proper flow of execution is buggered, so die app, die gracefully, but still die.

3. Colin (2004.09.24 - 22:15) #

Dying and dying early and loudly is definitely the right answer - I don't believe in polishing turds ;), I guess the how can be tricky. I think there are 3 factors:

1. Will things break if an invalid argument is only found to be so after changes have been made? Will it break the atomicity of a method or operation? If it will, make 100% sure everything is dandy up-front.

2. Who will use the code? If the method is part of a more public API then the "professionalism" of it needs to be higher - and you need to idiot-proof your code more. Users need to trust you more and you need to trust users less.

3. My own preference is to differentiate in whichever way between exceptions which indicate problems I've anticipated and thrown m'self, and those which indicate a bug or problem in my code that I obviously haven't anticipated. It allows one to identify the source of a problem more quickly and easily.

The fun part is reconciling those 3 though :)

4. Brendon Upson (2004.09.25 - 01:55) #

Generally I like the C style of return values.

something like:
boolean bSuccess = dodgyMethod(myNullObject);

Then test for success. I don't like how you put multiple statements in a try/catch and if any one of them fails you dump out. Also if you try to individually catch each statment the code is very messy and hard to follow due to the proliferation of try/catch blocks.

try/catch is sometimes useful though and I like that Java allows you the choice to pick ;-)

There's a bit of a discussion here: http://www.joelonsoftware.com/items/2003/10/13.html

Add a comment

Your name (mandatory):

Your email:
Your email address is not displayed
Your home page:

Comment (sorry, no HTML):

Remember details?
Yes No

meta

-home-
about
contact
disclaimer
articles
code
tech blog

style: [?]
[plain.dark.blue]

Categories

java
linux
music
notes/domino
personal
politiek
studies
techie
thee_blog
world

RSS Feeds

rssfeed all posts
rssfeed all cmts
rssfeed tech posts
rssfeed tech cmts

Archives

2012.02
2012.01
2011.12
2011.11
2011.10
2011.09
2011.08
2011.07
2011.06
2011.05
2011.04
2011.03
2011.02
2011.01
2010.12
2010.11
2010.10
2010.09
2010.08
2010.07
2010.06
2010.05
2010.04
2010.03
2010.02
2010.01
2009.12
2009.11
2009.10
2009.09
2009.08
2009.07
2009.06
2009.05
2009.04
2009.03
2009.02
2009.01
2008.12
2008.11
2008.10
2008.09
2008.08
2008.07
2008.06
2008.05
2008.04
2008.03
2008.02
2008.01
2007.12
2007.11
2007.10
2007.09
2007.08
2007.07
2007.06
2007.05
2007.04
2007.03
2007.02
2007.01
2006.12
2006.11
2006.10
2006.09
2006.08
2006.07
2006.06
2006.05
2006.04
2006.03
2006.02
2006.01
2005.12
2005.11
2005.10
2005.09
2005.08
2005.07
2005.06
2005.05
2005.04
2005.03
2005.02
2005.01
2004.12
2004.11
2004.10
2004.09
2004.08
2004.07
2004.06
2004.05
2004.04
2004.03
2004.02
2004.01
2003.12
2003.11
2003.10
2003.09
2003.08
2003.07
2003.06

© Colin Pretorius