Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-16-2007, 07:18 PM
Member
 
Join Date: Jul 2007
Posts: 26
paul is on a distinguished road
Use if then else structure, help
Hi, When developing some logic to see if something exists and if it doesn't, then create it, he thinks the following solution is the preferred one:

Code:
OurObject obj; // create a reference to the object if it exists, if it doesn't exist, the method just returns null obj = someotherobject.somemethodwhichreturnsthe-existingobject(argument) try { // next call will throw exception if object was not found in before call obj.somemethod(); } catch (Exception e) { // apparently the object did not exist, so let's create it obj = someobject.createobject(arguments); }
i think its more correct to do it this way:

Code:
OurObject obj; // create a reference to the object if it exists, if it doesn't exist, the method just returns null obj = someotherobject.somemethodwhichreturnsthe-existingobject(argument) if (object == null) { // apparently the object did not exist, so let's create it obj = someobject.createobject(arguments); }
what do you think is better, faster, whatever.
Thanks
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-07-2007, 07:00 AM
Member
 
Join Date: Jul 2007
Posts: 39
coco is on a distinguished road
I think it depends on the context of the overall program and what you're doing. For example whether you think it's better for a method to throw an exception for breaking reasons or if you need it to continue.

Personally just looking at that code I'd say the second one is faster assembly wise as well as more logical looking. But the first one has various uses which could extend to several different applications.

Time wise? You'll probably not notice any real significance unless you're building a very poorly written program, to which it'll spread out.

You could always test it by using timers to see how long execution of a particular statement is.

Greetings.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Queue data structure Java Tip java.lang 0 04-14-2008 10:35 PM
Java Array of Structure PAffiliates New To Java 1 01-28-2008 08:08 AM
data structure code vgvt New To Java 1 01-17-2008 04:49 PM
Control Structure Question ibanez270dx New To Java 1 11-13-2007 12:10 AM
Help with Structure Albert New To Java 1 07-13-2007 05:27 PM


All times are GMT +3. The time now is 11:02 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org