Results 1 to 2 of 2
  1. #1
    paul is offline Member
    Join Date
    Jul 2007
    Posts
    26
    Rep Power
    0

    Default 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:

    Java 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:

    Java 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

  2. #2
    coco is offline Member
    Join Date
    Jul 2007
    Posts
    39
    Rep Power
    0

    Default

    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.

Similar Threads

  1. Queue data structure
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 08:35 PM
  2. Java Array of Structure
    By PAffiliates in forum New To Java
    Replies: 1
    Last Post: 01-28-2008, 06:08 AM
  3. data structure code
    By vgvt in forum New To Java
    Replies: 1
    Last Post: 01-17-2008, 02:49 PM
  4. Control Structure Question
    By ibanez270dx in forum New To Java
    Replies: 1
    Last Post: 11-12-2007, 10:10 PM
  5. Help with Structure
    By Albert in forum New To Java
    Replies: 1
    Last Post: 07-13-2007, 03:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •