View Single Post
  #1 (permalink)  
Old 07-16-2007, 07:18 PM
paul paul is offline
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
Reply With Quote
Sponsored Links