[jdom-interest] insertChild in Element

James W. Howe jwh at allencreek.com
Wed Aug 30 06:09:44 PDT 2000

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()?

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 
grab the guts of an Element in order to manipulate the Element.  You are 
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 
doing something Once And Only Once.  The odds are pretty good that there 
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 

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.

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

More information about the jdom-interest mailing list