[jdom-interest] insertChild in Element

Brett McLaughlin brett.mclaughlin at lutris.com
Wed Aug 30 07:20:06 PDT 2000

"James W. Howe" wrote:
> At 09:46 PM 8/29/2000 -0500, Brett McLaughlin wrote:
> >Chris Hamersley wrote:
> > >
> > > Hi All,
> > >
> > > I could really use an insertChild(int aIndex, Element aChild) method in the
> > > Element class.  I think I'm going to go ahead and add this but was
> > wondering
> > > if it could become incorporated so I don't have to add this feature to
> > > future releases.
> >
> >What's wrong with using the List operations from getChildren()?
> >
> >-Brett
> There are a couple of things that bother me about the getChildren
> approach.  First, it violates the Law of Demeter.   You shouldn't have to

What? ;-) The "Law of Demeter"? 

> grab the guts of an Element in order to manipulate the Element.  You are

You don't have to grab the "guts of an Element." You are grabbing a Java
structure that is universal (a List) and operating upon it. There's
nothing exposed about this.

> basically exposing the internal implementation of the Element object to the
> outside world.  The second problem I have is that it violates the notion of

You're inserting an Element - by definition, you want to affect the
internal implementation. I'm not sure I buy this...

> doing something Once And Only Once.  The odds are pretty good that there

Actually, adding a method to Element to do this would violate the notion
of providing one way to do a single task. I don't like an API that
provides 13 ways to do the same basic thing...

> will be more than one place where you might want to insert an element and
> duplicating code is generally not a good idea.  Granted, the amount of code
> you are duplicating is pretty small, but it is duplicated effort just the
> same.

What is wrong with

element.getChildren().add(myElement, 3);

Nothing complex or duplicated about it...

> It seems to me that the ability to insert a child into an Element at a
> particular location is behavior which belongs to the Element itself.  I
> would advocate adding this capability to Element.

Seeing as we haven't heard this complaint from anyone else in 6 months,
I'm leary to add it. I think the line above is pretty simple to put in,
and makes a lot of sense. It's most of the reason we made the list of
children live, anyway.


> James W. Howe                           mailto:jwh at allencreek.com
> Allen Creek Software, Inc.              pgpkey: http://ic.net/~jwh/pgpkey.html
> Ann Arbor, MI  48103

Brett McLaughlin, Enhydra Strategist
Lutris Technologies, Inc. 
1200 Pacific Avenue, Suite 300 
Santa Cruz, CA 95060 USA 

More information about the jdom-interest mailing list