Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2006

Tip: Looking for answers? Try searching our database.

Can I simplify this code?

Thread view: 
James Yong - 06 Dec 2006 08:07 GMT
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.]



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.