On 31 Mar 2004, at 11:52, Gianugo Rabellino wrote:
I've seen in the new API that in a number of places there are contracts that return null if something isn't found: since these are all perfect candidates for nasty NPEs, how about switching to NullObject pattern or throw Exceptions instead?
As far as I can see, "null"s are returned only when implementing the collections interfaces (Library, Parameters, Configuration) or when URLs are resolved (Resolver).
And even there (ex. Configuration and Parameters) when calling a method specifed in the interface, and not inherited from the Set/Map/Collection, it will throw specific exceptions (ex. ConfigurationException).
Where did you notice it?
Well... there are 10 occurrences, but this are the most prominent:
o.a.c.kernel.configuration.Parameters.java:
public Object get(Object name) {
if (name == null) return(null);o.a.c.kernel.archival.HashLibrary.java:
public Descriptor get(Identifier identifier) {
if (this.contains(identifier)) {
Iterator iterator = this.iterator();
while (iterator.hasNext()) {
Descriptor descriptor = (Descriptor) iterator.next();
if (descriptor.equals(identifier)) return(descriptor);
}
}
return(null);
}o.a.c.kernel.resolution.CompoundResolver.java:
public Resource resolve(String name) {
Resolver r[] = (Resolver[])list.toArray(new Resolver[list.size()]);
for (int x = 0; x < r.length; x ++) {
Resource s = r[x].resolve(name);
if (s != null) return(s);
}
return(null);
}I haven't been through the code to see what exactly these things do, but from a pure OOP POV (whatever that means :-)) there is some "code that smells" (http://c2.com/cgi/wiki?CodeSmell). It might make sense, but in most cases it doesn't.
Ciao,
-- Gianugo Rabellino Pro-netics s.r.l. - http://www.pro-netics.com Orixo, the XML business alliance - http://www.orixo.com (Blogging at: http://www.rabellino.it/blog/)
