[jdom-interest] detach() [eg]

Kenworthy, Edward edward.kenworthy at exchange.co.uk
Mon Apr 30 23:51:37 PDT 2001

I think this argument is flawed.

subject.verb(object) sure I go along with that. As a rule of thumb which
obeys all the usual rules about rules.

However, how is parent.detach(child) superior to child.detach(parent) ?

In the first case detach is read as "detach this child" in the second it's
"detach from parent" how is the former superior to the latter ?

It isn't.

The latter has the advantage that it can be simplfied to child.detach()
because the child can only have one parent. This also eliminates the need
for any checking on the parameter because there isn't one.

In the case of parent.detach(child) what if the child isn't a child of
parent ? Got to check that and then what ? Throw an IAE ? What if the child
is null ? More checking. This is actually worse than having element.detach()
throw and exception if it is the root element.

Moving detach to the parent makes the api more complicated, sure it removes
the risk of having a null root but that's all and that isn't really such a
big deal beyond putting the document into an invalid state. But then we have
an exception for that ;-)

-----Original Message-----
From: Elliotte Rusty Harold [mailto:elharo at metalab.unc.edu]
Sent: 30 April 2001 15:07
To: jdom-interest at jdom.org
Subject: Re: [jdom-interest] detach() [eg]

At 6:00 PM -0700 4/29/01, Jason Hunter wrote:
>>  That's secondary to me at this point. I am
>>  saying, though, that I think detach() on an Element would make me think
>>  detaches an element from the object detach() is being invoked on.
>Luckily detach() takes no parameters so if that's your inclination (note
>it's not my inclination) then you'll have to realize it doesn't work
>that way.

I think Brett's correct here. The way I explain it to my students is that
the customary order in OOP is


element.detach() violates that common understanding, especially since 
detach is not a reflexive verb.

I really see no need for detach() anymore. I think that 
parent.removeContent(child) is all that's necessary; and detach() as 
currently written is just plain confusing. If we absolutely must have 
that bit of syntax sugar, then the name should be changed to a verb 
that does commonly operate on it's subject, like element.breakAway() 
or some such; but I really don't think we need it.

And of course removing detach() totally eliminates the problems of 
clients unintentionally detaching the root element of a document.

| Elliotte Rusty Harold | elharo at metalab.unc.edu | Writer/Programmer |
|                  The XML Bible (IDG Books, 1999)                   |
|              http://metalab.unc.edu/xml/books/bible/               |
|   http://www.amazon.com/exec/obidos/ISBN=0764532367/cafeaulaitA/   |
|  Read Cafe au Lait for Java News:  http://metalab.unc.edu/javafaq/ |
|  Read Cafe con Leche for XML News: http://metalab.unc.edu/xml/     |
To control your jdom-interest membership:

More information about the jdom-interest mailing list