Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: What is best loop sequence?

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default What is best loop sequence?

    What is the most efficient code?


    Writen like this:


     if(vehicle.Branch.UWCompanyCode==typekey.UWCompanyCode.TC_CGIC){
     
        if( vehicle.CollisionAPRateGroup_CG==-1  and (vehicle.CostNew_CG==null or vehicle.CurrentValue_CG==null)){
     
          throwError(displaykey.Web.PolicyLine.Vehicle.NoVRGMessage)
        }
      }
     
      if(vehicle.Branch.UWCompanyCode==typekey.UWCompanyCode.TC_FA){
        if( vehicle.CollisionAPRateGroup_CG==-1  and vehicle.CurrentValue_CG==null){
     
          throwError(displaykey.Web.PolicyLine.Vehicle.NoVRGMessageFA)
        }
      }

    or like this one?

    if( vehicle.CollisionAPRateGroup_CG==-1){  
        if(vehicle.Branch.UWCompanyCode==typekey.UWCompanyCode.TC_CGIC and ( vehicle.CostNew_CG==null or vehicle.CurrentValue_CG==null)){ 
            throwError(displaykey.Web.PolicyLine.Vehicle.NoVRGMessage)
         }
        if(vehicle.Branch.UWCompanyCode==typekey.UWCompanyCode.TC_FA  and vehicle.CurrentValue_CG==null){  
              throwError(displaykey.Web.PolicyLine.Vehicle.NoVRGMessageFA)
        }
      }

    I think that is the same thing


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: What is best loop sequence?

    What happened when you wrote an example program that measured the efficiency of each? What do you even mean by efficiency in this case?

    If you want help, you should provide a simpler example, without all those convoluted variable names.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: What is best loop sequence?

    Have you ever learned how to make a truth table?
    If you create a truth table, you should be able to find the most simplified logic.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  4. #4
    Forum VIP
    Join Date
    Jun 2011
    Posts
    317
    My Mood
    Bored
    Thanks
    47
    Thanked 89 Times in 74 Posts
    Blog Entries
    4

    Default Re: What is best loop sequence?

    It doesn't matter which is more efficient. For something as simple as conditional logic you would need to be executing it thousands of times per seconds *and* have the performance issues *and* perform extensive profiling *and* identify this logic as problematic to throughput before you even consider optimization.

    A more practical concern is correctness, readability and maintainability so the 'better' code is the one which is easier to understand so personally I think neither example hit's the mark. At a glance I can't easily tell which conditions which throw an exception and it even appears to have syntax errors.

  5. The Following 2 Users Say Thank You to ChristopherLowe For This Useful Post:

    aussiemcgr (January 15th, 2014), KevinWorkman (January 15th, 2014)

  6. #5
    Junior Member
    Join Date
    Jan 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: What is best loop sequence?

    I'll try to simplify:

     
    if(vehicle.Company==G){
     
        if( vehicle.Collisions==-1  and (vehicle.Cost==null or vehicle.CurrentValue==null)){
     
          throwError(GMessage)
        }
      }
     
      if(vehicle.Company==F){
        if( vehicle.Collisions==-1  and vehicle.CurrentValue==null){
     
          throwError(FMessage)
        }
      }

    or
     
    if( vehicle.Collisions==-1){  
        if(vehicle.Company==G and ( vehicle.Cost==null or vehicle.CurrentValue==null)){ 
            throwError(GMessage)
         }
        if(vehicle.Company==F  and vehicle.CurrentValue==null){  
              throwError(FMessage)
        }
      }

    In the first one I have two if related to the vehicle company then nested in each I verify at the same time the 2 conditions to send an error: collissions -1 and if there are specific null fields.

    In the second one
    I verify in only one if first if collisions is -1 if yes I verify the conditions for the 2 companies G and F

    --- Update ---

    For each vehicle company we have diferent conditions to throw error and each company have their own error message:
    For G there are 2 fields that can not be null
    For F there are only one of them

    But for both collisions must be -1 to throw error

  7. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: What is best loop sequence?

    The "efficiency" of either case is entirely dependent on the context of your problem. Neither one is inherently better or worse.

    However, if your code is run in an environment where vehicle.Company is almost always G but vehicle.Collisions is almost never -1, then the first example will have to evaluate two boolean expressions much more often than your second if statement. The order of your if statements will also affect this type of "efficiency".

    Again, this is entirely dependent on your context, so it's going to be pretty impossible to give you a straight answer.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. #7
    Junior Member
    Join Date
    Jan 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: What is best loop sequence?

    Thanks, I coded the first one and I was asked to change to the second by lead programmer (according to him that shoul simplify my code?!) I coded like that because as you said vehicle.Company is almost always G but vehicle.Collisions is almost never -1. And I wanted to be sure I was right thinking in the environment.

  9. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: What is best loop sequence?

    Quote Originally Posted by javaDruide View Post
    Thanks, I coded the first one and I was asked to change to the second by lead programmer (according to him that shoul simplify my code?!) I coded like that because as you said vehicle.Company is almost always G but vehicle.Collisions is almost never -1. And I wanted to be sure I was right thinking in the environment.
    You should have specified that in your original post then. I recommend asking the lead developer exactly why it simplifies the code, and explain to him your reasoning. It might just be a personal preference, or there might be a context you haven't explained (or aren't aware of yourself).
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  10. #9
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: What is best loop sequence?

    Presumably the vehicle.Company can't be both G and F? It makes more sense to use an if-elseif type control flow, or even just an if-else if there can't be any other values for Company. This makes more sense intuitively.

    Consider logically what each type of code is stating:

    Version 1 says if the Company is G, check for an error some way. Else, if the Company is F, check for an error some other way.

    Version 2 says if collision is -1, we probably have an error. Try to resolve if we do have an error by going into the details on what the Actual Company is.

    Both versions seem to be perfectly reasonable logic flows to me if you us an if/elseif, or even just an if/else. Otherwise I would would pick 2 as a clear winner for readability.

    Performance between the two is negligible if you're expecting an error to occur often, otherwise the second version theoretically is faster. As others have pointed out, though, unless you're throughput limited by this section of code, performance differences won't be noticeable.

  11. #10
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: What is best loop sequence?

    The only "fully-true" outcome REQUIRES "vehicle.Collisions==-1" to be true in all scenarios. I agree with checking vehicle.Collisions==-1 first, since if that is false, evaluating vehicle.Company==G and vehicle.Company==F is unnecessary computations (it's not even noticeable computations in reality, but we are talking about simplification here).
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

Similar Threads

  1. [SOLVED] 1, 4, 9, 16 sequence
    By mattisvan in forum Loops & Control Statements
    Replies: 4
    Last Post: March 15th, 2013, 07:18 AM
  2. Fibonacci sequence
    By ssohpkc in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 8th, 2013, 09:29 PM
  3. Sequence Classification
    By SilentNite17 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: June 1st, 2012, 08:25 AM
  4. Sequence
    By r_james14 in forum Java Theory & Questions
    Replies: 2
    Last Post: April 8th, 2012, 10:05 PM
  5. Data sequence
    By street_missile in forum Java ME (Mobile Edition)
    Replies: 10
    Last Post: August 26th, 2011, 11:54 AM