[jdom-interest] getText() and getAttributeValue()
Bradley S. Huffman
hip at a.cs.okstate.edu
Thu Feb 14 16:21:28 PST 2002
"Trimmer, Todd" writes:
> If I want to safety check...
> I would write:
> Attribute attr = element.getAttribute("foo");
> String value = "";
> if (attr != null)
> value = attr.getValue();
> But if I want to safety check...
> I would write:
> String value = element.getAttributeValue("foo");
> if (value == null)
> value = "";
> Gee. Not a lot of savings here beyond the four characters I save in writing
> getAttributeValue("foo") vs. getAttribute("foo").getValue(). Suddenly, this
> *convenience* method doesn't seem very convenient, does it? It doesn't save
> me any work at all.
But it does clearly show that getAttributeValue can return null and gives
a clear indication how your application handles it. In cases like this, I
think most C/Java programmers will associate a null as a return value with
not finding what they where looking for.
> 3) Having getAttributeValue() return null forces it to such a trivial
> advancement beyond getAttribute("foo").getValue(), that it is reduced to
So we should get rid of getAttributeValue?
> p.s. -- I like the element.getAttributeValue(name, defaultValue) idea, too.
I don't because it's behavior isn't intuitive without looking at the
docs. Example getAttributeValue("drawf","Sleepy"), does this method
look for a attribute "drawf", then a attribute "Sleepy"?. To me
String value = element.getAttributeValue("drawf");
if (value == null)
value = "Sleepy";
is preferable because it reads well and clearly shows what the code does.
More information about the jdom-interest