[jdom-interest] insertChild in Element

trebor.a.rude at lmco.com trebor.a.rude at lmco.com
Wed Aug 30 07:28:08 PDT 2000

(Sorry this isn't a proper reply, but I haven't yet figured out how to get
Outlook to quote properly, and I've got better things to do than fight with

I disagree that Element is exposing it's internal implementation. The
PartialList that is returned when getChildren is called could easily be
modified if the internal implementation of Element changes, and there would
be no effect to the interface. As for duplicating code, the only thing that
has to be duplicated is the call to getChildren:

Element insertThis = new Element("foo");
Element container = new Element("container");

container.getChildren().add(0, insertThis);
container.addContent(0, insertThis); // Is this really better?

The other thing that concerns me is that if we add this function, where does
it end? Will we have to add "removeContent(int index, Element ele)", etc.,
for consistency, or because people say "you addeded inserts, why not
removes, and..."? That would be an awful lot of clutter for minimal gain, in
my opinion.

On the other hand, getChildren() does do a bit of work filtering out all the
non-Element children (why is that, anyway?), so adding this to the Element
interface would make it more efficient, but I'm not sure it would be enough
of an improvment to justify the change.

Trebor A. Rude
trebor.a.rude at lmco.com

> -----Original Message-----
> From:	James W. Howe [SMTP:jwh at allencreek.com]
> Sent:	Wednesday, August 30, 2000 7:10 AM
> To:	Brett McLaughlin
> Cc:	jdom-interest at jdom.org
> Subject:	Re: [jdom-interest] insertChild in Element
> 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 
> 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 
> same.
> 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			
> _______________________________________________
> To control your jdom-interest membership:
> http://lists.denveronline.net/mailman/options/jdom-interest/youraddr@yourh
> ost.com

More information about the jdom-interest mailing list