[jdom-interest] detach() [eg]

Kenworthy, Edward edward.kenworthy at exchange.co.uk
Tue Apr 24 23:29:21 PDT 2001

Hi Joseph

You're over-complicating it, no doubt because making it appear complex
bolsters your argument.

There's an axiom which reads make things as simple as possible and then
stop. Being over-complex is bad, but so is being over-simplified.

If the document has a null root then it IS in an illegal state for many
methods. The standard way for an object in Java to show it is in an illegal
state is for it to throw an IllegalStateException. Sorry if you don't like
it but hey you're too late, you should have worked for Sun ;-).

The only real options are either prevent Document ever going into an illegal
state (as per Elliotte's suggestion) or having the Document behave as it
should and throw an IllegalStateException (my suggestion). If you want to
test for the presence of a root method then I think hasRoot() would work
fine, I could also live with getRootElement() returning null.

So what's the problem ?


-----Original Message-----
From: Joseph Bowbeer [mailto:jozart at csi.com]
Sent: 23 April 2001 17:25
To: jdom-interest at jdom.org
Subject: [jdom-interest] detach() [eg]

I'm for either:

(1) Allow the root element to be detached by allowing documents to have a
null root.  Assign the burden of checking for a null root to the client.
(Part of the client's precondition.)

(2) Prevent the root element from being detached.

I prefer #1.  As for #2, Elliotte's first proposal seems OK, and I'm still
digesting the second proposal.

I'm against both (3) Using an implicit placeholder for the detached root, or
(4) Adding IllegalStateException to Document.

No one likes placeholder (3), except perhaps as the lesser of evils.

As for IllegalStateException (4), I don't like it because of its time bomb
nature.  Compared to a null root, IllegalStateException makes it harder for
clients to determine the state of a given document (calling getRootElement
would throw an IllegalStateException, right?), and it gives the clients no
discretion in determining whether an empty document is in fact a legal
argument or not.

With the addition of IllegalStateException, client methods would need to
catch and rethrow in order to report the most appropriate exception:

    void process(Document doc) {
        Element root;
        try {
            root = doc.getRootElement();
        catch (IllegalStateException ex) {
            throw new IllegalArgumentException("no root: " + doc);
        // check remaining preconditions
        // ...

See also:


Joe Bowbeer

To control your jdom-interest membership:

More information about the jdom-interest mailing list