[jdom-interest] getMandatory*

Andre Van Delft avandelft at epo.org
Tue Jul 18 02:43:06 PDT 2000

Jason Hunter wrote:

> Being that we had an overwhelming set of +1 votes for "null" including
> the binding votes of Brett and myself (so glad we agree), I've now made
> the change in the tree.  I changed getChild(), getAttribute(),
> getRootElement(), and getProcessingInstruction() to return null if no
> such item was found.  I also removed NoSuch* classes from the tree.  I'm
> -1 on adding hasChild() because a null check is sufficient.  No reason
> to bloat the API.

Reluctantly I start the discussion again.

I find the change a significant improvement, because of the better performance.
However, I now have changed my code and I ended up with wrapper functions,
again. This time, I made a wrapper for getChild, named getMandatoryChild,
that throws a kind of NoSuchChildException if no child is found.
I want the getMandatoryChild functions etc. available in the JDom API;
otherwise aplication developers will create these theirselves time and again.

The reason is in the way I want to structure my code:

   optionalChild = elt.getChild("OPTIONAL");
    if (optionalChild != null)
        ...do something with it
   mandatoryChild = elt.getMandatoryChild("MANDATORY");
   ...do something with it...
catch (NoSuchItemException e)

Here NoSuchItemException is an abstract superclass for
NoSuchChildException, NoSuchAttributeException and
NoSuchProcessingInstructionException (in case a
ProcessingInstruction could be mandatory, which I don't know).
The single exception handing section at the end keeps the code clear
and concise.
I manage to have this layout using a wrapper function
getMandatoryChild in my class. Also I have defined 3
NoSuch*Exception classes myself.

It would not be a good idea to add an extra test after
the assignment to mandatoryChild, as in:

   mandatoryChild = elt.getMandatoryChild("MANDATORY");
   if (mandatoryChild != null)
       throw new NoSuchChildException("MANDATORY");

or with some error handling code and a return.
These solutions would clutter the function too much.

So I propose getMandatory* functions. Luckily there is
no hurry, since backward compatibility is OK this time.

More information about the jdom-interest mailing list