|
From: <ak...@sp...> - 2004-02-15 06:59:12
|
Reading the sources trying to see if I can extract Spring's JDBC function=
ality away from its dependence on DataSource I noticed that quite a few m=
ethods that take optional List parameters substitute an empty LinkedList =
(via new LinkedList()) to methods on which they depend.
Which left me wondering: are those LinkedList instances ever written to -=
- they seem to not need to be, but I couldn't answer definitively without=
significantly greater intimacy with Spring's code.
So, if those are meant to be read-only, I'd encourage using java.util.Col=
lections.EMPTY_LIST instead (and similarly EMPTY_MAP and EMPTY_SET as nee=
ded). Why? 3 reasons:
1. Better self-documenting code. If a default parameter value is re=
ad-only, use a read-only (immutable) instance. You still get the List se=
mantics and don't need null checks in the called methods.
2. It is faster. Object creation ion the heap is orders of magnitud=
e slower than putting an object reference on the stack.
3. Uses less memory. Small though they be, new objects still consum=
e resources. And have to be garbage collected later -- which goes back t=
o point #2.
Seems like a win all-around.
=3D=3D Ee
|