Can I simplify this code?
for (Iterator iter = this.children.iterator(); iter.hasNext(); )
{
String obj = (String)iter.next();
if (obj.equals(child)) return true;
}
return false;
to
return this.children.contains(child);
Steve Brecher - 06 Dec 2006 10:02 GMT
> Can I simplify this code?
>
[quoted text clipped - 8 lines]
>
> return this.children.contains(child);
Looks OK to me, as long as there is in fact a "contains" method provided by
whatever class children is an instance of and it does the conventional
thing.

Signature
For mail, please use my surname where indicated:
steve@surname.reno.nv.us (Steve Brecher)
Hendrik Maryns - 06 Dec 2006 10:23 GMT
James Yong schreef:
> Can I simplify this code?
>
[quoted text clipped - 8 lines]
>
> return this.children.contains(child);
Depends on what children is. If it is a Collection, then yes.
H.
- --
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
Andreas Leitgeb - 06 Dec 2006 11:54 GMT
> Can I simplify this code?
> for (Iterator iter = this.children.iterator(); iter.hasNext(); )
[quoted text clipped - 7 lines]
>
> return this.children.contains(child);
I'd say: "yes, but ..."
The "but"-case is, when you have not only String objects
(or even null) in your collection, then the former code
might throw an exception, whereas the latter will rather
ignore irrelevant items. So it's not strictly equivalent.
Practically, the latter is not just shorter, it's also
most likely faster (even by orders of magnitude, depending
on the type of collection actually used) and more stable.
Thomas Schodt - 06 Dec 2006 14:50 GMT
> Can I simplify this code?
>
[quoted text clipped - 8 lines]
>
> return this.children.contains(child);
What is wrong with just trying it?
Stefan Ram - 06 Dec 2006 22:33 GMT
>> return this.children.contains(child);
>What is wrong with just trying it?
By trying one will only find errors, which can be detected by
the compiler and the runtime tests available. It might be a
mistake that only has effects under another Java implementation
or other runtime conditions.
Tris Orendorff - 15 Dec 2006 21:59 GMT
>>> return this.children.contains(child);
>>What is wrong with just trying it?
[quoted text clipped - 3 lines]
> mistake that only has effects under another Java implementation
> or other runtime conditions.
WTF! That is a risk is a bit far-fetched. Do you even use the collection
classes?

Signature
Tris Orendorff
[Q: What kind of modem did Jimi Hendrix use?
A: A purple Hayes.]