[jdom-interest] NoSuch*Exceptions in JDOM

Patrick Dowler Patrick.Dowler at nrc.ca
Mon Jun 19 09:58:55 PDT 2000


On Sat, 17 Jun 2000, Jim Rudnicki wrote:
> Lurking here I notice everyone dividing into two camps. Let me offer a third
> direction.
> 
> Any function using the return value to return error codes I accept as bad
> design.  I thought we all learned this long ago.  A section from "Writing
> Solid Code" burned in my memory (but looked up, p91):
> 
>   "Don't use confusing dual purpose return values--each output should
> represent exactly one data type.  Make it hard to ignore important details
> by making them explicit in the design."

Sounds nice in theory...

> From this, the correct design choice is:
> 1. So getChild() should always return a child or throw an exception.  Null
> is not a child.  Exceptions are hard to ignore.  null is too easy to ignore.

Null is not an error code and it is not too easy to ignore. In fact, it is 
impossible to ignore since ignoring it will result in a NullPointerException :-)

Null is a  perfectly sensible assignment value for objects in Java. It means
that there is no object. As I pointed out in a previous post, the standard
java classes woprk exactly like this:

	java.util.Map.get(Object key)

returns null if the key is not in the map. We can argue about whether this
is a good design or not, but it is standard Java style. Someone else
pointed out a similar example with Hashtable (also a map).


> 2. If the program does not know if a child is present, it should, must, or
> ought to be testing with:
> boolean hasChild( String s );

"must" is a strong word. Doesn't getChild() a serve as both a check and a
get? With hasChild() you get into messy caching to avoid 2 x O(n)
lookup. What's the point, really... keep the API simple.

Generally, anyone using getChild hjas to be cognizant of the fact that the
child might not exist. The result can be null or an exception. My position 
remains that an absent child may be an error in XML but it isn't an error
per se in Java. Thus, XML encourages an exception (to signal an error)
but standard Java usage encourages a null return. Java is filled to the
brim with null return values...

-- 

Patrick Dowler
Canadian Astronomy Data Centre




More information about the jdom-interest mailing list