On 25.03.2014, at 20:16, [email protected] wrote:

> private static final List<MetaDataAttr> EMPTY_ATTRIBUTE_LIST = new 
> ArrayList<MetaDataObject_impl.MetaDataAttr>(0);

Such mutable zero-sized "constants" are dangerous because client code may 
(accidentally) try to add something to the empty list. 

I have seen (and occasionally do write) code such as

object.getListOfSomething().add(yyy). 

Such code expects that the list returned by the object is owned by the object 
and that it is mutable.

Java provides several immutable zero-sized collections as part of the 
Collections class, as constants and as functions. The latter play better with 
generics as Java can do type inference, e.g.

List<MetaDataAttr> EMPTY_ATTRIBUTE_LIST = Collections.emptyList();

Mind that such changes can break existing client code.

> private static final Attributes EMPTY_ATTRIBUTES = new AttributesImpl();

For this one, we'd probably need some immutable EmptyAttributesImpl class.

For the sake of design, I would consider immutable "empty" objects a good idea.

For the sake of not breaking existing code, we should probably stick with 
initializing fields with mutable empty instances, but not make these global 
constants.

Cheers,

-- Richard

Reply via email to