[jdom-interest] setText() to replace children?

Steven D. Keens skeens at planetfred.com
Thu Jul 12 05:54:28 PDT 2001

I've been part of a maintenance crew which has made me into a firm believer
in self documenting code - which setText() is not.  This whole dicussion
about setText() makes it non self document.  And as you know it's because it
contains an extra content.clear().

To make it self documenting there are a few things you can do:
1) Rename it to removeContentAndSetText() or something like that stating
extactly what it does.  I prefer this one.  Did anyone offer this
suggestion?  And getText() can be renamed to something like
getImmediateContatenatedText().  From my experience these probably won't be
very popular method names but they sure do make things clear.

2) Force the user to recognize in their code that setText() has some extra
behavior.  You can only do this with exceptions. I understand your dislike
for exceptions, particularly unchecked ones.  I want to avoid them as much
as possible as well.  That's why I prefer option 1.

3) Remove the method altogether.  Can't do this one because many people seem
to like setText()'s convenience.

As for validation, do you do any validation at all?  If you don't validate
the data, either in a DTD or schema, then I hope you have some validation
directly in your code.

Steven Keens                mailto:skeens at planetfred.com
Planetfred Inc.             http://www.planetfred.com
44 Byward Market, Suite 240, Ottawa, ON, K1N 7A2, Canada

The most exciting phrase in science, the one which heralds
new discoveries, is not "Eureka!", but "That's funny!"
                       -- Issac Asimov

>-----Original Message-----
>From: jdom-interest-admin at jdom.org
>[mailto:jdom-interest-admin at jdom.org]On Behalf Of Alex Chaffee
>Sent: Wednesday, July 11, 2001 14:34
>Cc: 'JDOM Interest List'
>Subject: Re: [jdom-interest] setText() to replace children?
>Steven D. Keens wrote:
>> I like your idea but wouldn't it be better if the exception is
>thrown when
>> there is mixed content.  Also, getText() could react the same way, by
>> throwing an exception if it contains mixed content.  Thus, we retain the
>> convenience of the two methods (isn't that why they exist, for
>> Plus we have a consistent and symetric API that behaves predictably.
>No exceptions!  Exceptions are bad!  I don't want to have to catch an
>exception every time I call getText, and I also don't want my
>thread to die
>with an unchecked exception if the program reads a data file with invalid
>mixed content.  getText should not throw an exception; it should return a
>String like it says it does.  I don't even really care what that string
>contains as long as it's unambiguous and documented.
>(Though I like Rusty's "recurse" proposal the more I think about it.)
>If anyone wants more complex behavior (wrt whitespace or whatever)
>they can
>walk the list themselves in 3 lines of code, or just use XMLOutputter.

More information about the jdom-interest mailing list