[jdom-interest] detach() [eg]

Joseph Bowbeer jozart at csi.com
Fri Apr 20 15:06:19 PDT 2001


edward.kenworthy at exchange.co.uk writes:

> The argument is that you're hiding a user error.
> Never, ever, a good idea.

Well, there's good and bad on all sides.  That's why they pay us.  (Oh,
that's right: We're volunteers...)

1. Second-guessing the user is not a good idea either.  For example, what
makes us think the user intended to setRootElement before they setDocType?

What if the user puts the wrong kind of Element in the Document, or
constructs invalid Elements?  These are all errors that JDOM has no clue
about, and are therefore the client's responsibility.  Compared to this,
checking for a null root is a cinch.

2. Adding artificial state to a class (the Document in this case) is not a
good idea either.  It makes the class harder to understand and less
flexible.

So as to fail fast, I suggest that we only consider throwing illegal-state
if someone tries to set a document property that depends on the root element
being set.  Are there any such properties?

3. Finally, looking at the XMLOutputter example, I don't see any benefit
from IllegalStateException.

Let's say the user detaches the rootElement and then passes the Document to
XMLOutputter.  Now, unless XMLOutputter checks its arguments, the Document
is going to throw an IllegalStateException when XMLOutputter tries to access
it.  I propose that XMLOutputter throw an IllegalArgument Exception instead.

--
Joe Bowbeer








More information about the jdom-interest mailing list