I have a method that accepts an enum as the parameter.
Should i apply defensive programming and check whether to throw an
exception if it is null?
Lasse Reichstein Nielsen - 12 Jun 2005 11:40 GMT
> I have a method that accepts an enum as the parameter.
> Should i apply defensive programming and check whether to throw an
> exception if it is null?
Sure, why not?
If your method does not accept null as an argument (as its documentation
will show), you might as well fail early.
Maybe your method tries to use the value, and will throw a NPE anyway,
but maybe it just stores the value for later and then the error will
be harder to track. And even if you use the value now, you might
change the method later, so it's safer to add the null check now.
/L

Signature
Lasse Reichstein Nielsen - lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Harald - 12 Jun 2005 11:52 GMT
> I have a method that accepts an enum as the parameter.
> Should i apply defensive programming and check whether to throw an
> exception if it is null?
I guess the JVM will do this for you as soon as you use the value
(haven't tried enums yet.)
Harald.

Signature
---------------------+---------------------------------------------
Harald Kirsch (@home)|
Java Text Crunching: http://www.ebi.ac.uk/Rebholz-srv/whatizit/software
Roland - 12 Jun 2005 12:35 GMT
> I have a method that accepts an enum as the parameter.
> Should i apply defensive programming and check whether to throw an
> exception if it is null?
An enum instance is like an instance of a class. When used as a
parameter, its value could be null, like any other reference type
parameter.
How to handle it, entirely depends on what your method should do: if
null isn't allowed as value of the method's parameter, you could throw
an exception. In other cases a method might handle it "gracefully".

Signature
Regards,
Roland de Ruiter
` ___ ___
`/__/ w_/ /__/
/ \ /_/ / \
Lucy - 13 Jun 2005 01:12 GMT
> > I have a method that accepts an enum as the parameter.
> > Should i apply defensive programming and check whether to throw an
[quoted text clipped - 6 lines]
> an exception. In other cases a method might handle it "gracefully".
> --
I recall reading somewhere that an assert should not be used to
check method parameters, so why would you bother? Or has my
memory already been garbage collected.
Daniel Dyer - 13 Jun 2005 10:39 GMT
> I recall reading somewhere that an assert should not be used to
> check method parameters, so why would you bother? Or has my
> memory already been garbage collected.
Assertions generally shouldn't be used for this because they can be
switched off at runtime (or more correctly, not switched on). Assertions
are not the only way to check parameters. A simple conditional that
throws an IllegalArgumentException is usually sufficient.
Dan.

Signature
Daniel Dyer
http://www.footballpredictions.net
iamfractal@hotmail.com - 13 Jun 2005 10:30 GMT
> I have a method that accepts an enum as the parameter.
> Should i apply defensive programming and check whether to throw an
> exception if it is null?
Hi!
"Effective Java," is a great book, and I did enjoy one Amazon
reviewer's comment on it; the comment seems applicable here (from
http://www.amazon.com/exec/obidos/tg/detail/-/0201310058/qid=1118654844/sr=8-1/r
ef=pd_csp_1/104-9588299-9954334?v=glance&s=books&n=507846):
The other caveat is that this book is written from the perspective of
writing public APIs. Bloch is the lead architect for the core JDK APIs
where the classes and APIs have special security considerations.
Several pieces of advice he gives apply absolutely to a public API
where you want to defend the API from malicious code. However, if the
code you are creating is intended for a less hostile and more trusting
environment, fully following all of his advice will result in
unnecessarily complex code. The one knock I give to the book is that
Bloch does not delineate the circumstances that would dictate
following his rather rigorous approach to bullet-proofing an API and
can leave the reader with the mistaken perception that the advice
applies equally to all categories of code.
.ed
www.EdmundKirwan.com - Home of The Fractal Class Composition.