directory-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Tony Blanchard <>
Subject Re: [OT] How best to respond to null arguments?
Date Wed, 31 Aug 2005 19:08:54 GMT
For me,

passing null and wanting to pass null (not returning null) to some API 
is like wanting var args...
So if  varargs exists in the language and the API in question is not a 
var args one, there is a problem from my point of view => Option 2( if 
you are writing the used API and not the user of the API)
If var args does not exist in language, there is no need to throw NPE 
depending of the real need of the null argument (ie passing null to a 
method like getIconBytes((Icon)null ) is obviously a mistake => option 
3) Passing null to a method wich can work unless it is a var args 
synonym... Should pass...

Another step is speaking of  pre-conditions and post-conditions : the 
state of the object before and after the operation. If the null argument 
disturbs these pre/post condition => Option 3 or option 4  (the state of 
the object "x" is not valid)
else, ignore it...

I think the best is to do always the same thing for similar cases.

hope this is the best practice... Not sure.
also hope this helps.

Alex Karasulu a écrit :

> Hi all,
> I was playing again with my very many split personalities when I 
> realized one likes to throw NullPointerExceptions after checking if an 
> argument is null while another likes to throw 
> IllegalArgumentExceptions since null is value the argument can take.   
> Yet another personality does not care and figures a NPE will result 
> anyway when the null argument is referenced so why bother.
> Question: How best to respond to null arguments?
> Option #1: Ignore it and let a NPE result on its own
> Option #2: Test to see if arg is null early and throw a descriptive NPE
> Option #3: Test to see if arg is null early and throw a descriptive IAE
> Which sounds best?
> Alex

View raw message