Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder dswimboy's Avatar
    Join Date
    Nov 2003
    Location
    mostly in Ann Arbor
    Posts
    458
    Thanks
    0
    Thanked 0 Times in 0 Posts

    which is better?

    Code:
    if ( (test1) && (test2) ) {
        code
    } else {
        code
    }
    or?
    Code:
    if (test1) {
         if (test2) {
              code
         }
    } else {
         code
    }
    "There is more than one way to do it."

  • #2
    Regular Coder dswimboy's Avatar
    Join Date
    Nov 2003
    Location
    mostly in Ann Arbor
    Posts
    458
    Thanks
    0
    Thanked 0 Times in 0 Posts
    responding to myself.

    i understand there are specific benifits to both. if one test2 has some odd null values based on test1 or whatever. but in general.

    i program in perl and php, currently, but i'd be interesting in hearing opinions from other languages as well.
    "There is more than one way to do it."

  • #3
    Regular Coder
    Join Date
    Feb 2003
    Location
    California
    Posts
    925
    Thanks
    0
    Thanked 0 Times in 0 Posts
    there might be a difference in cpu time, though Im sure your not to worried about that as it would be very small with the different checks that would have to go on. Also the readability of the code for others. Some might prefer it one way or the other. Otherwise they are the same to me...


    Jason

  • #4
    Regular Coder
    Join Date
    Feb 2004
    Location
    WA
    Posts
    213
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i understand there are specific benifits to both. if one test2 has some odd null values based on test1 or whatever. but in general.
    This is not true. All the languages I know short circuit the second expression depending on the result of the first expression.

    For example if test2 dereferences a null pointer and test1 checks for it, then the code runs fine because as soon as test1 fails, test2 is not even computed.

    I am not sure about the performance concerns. I'd like to hear some opinions on that too!
    Nobody is Perfect. I am Nobody.

  • #5
    Regular Coder
    Join Date
    Dec 2003
    Posts
    367
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The difference in performance would depend on the language. In a language like C or Java there would be no difference since if test1 is false then test2 won't be evaluated. In a language like VB (I know the code isn't VB, but this is just a for instance) both expressions in the if statement will be evaluated regardless.

    But those examples aren't semantically the same: in the second example the else code won't be executed if test1 is true and test2 is false, and in the first example it will.

    shmoove

  • #6
    New to the CF scene
    Join Date
    Oct 2003
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This is just my opinion based on "easy to understand"

    if(condition1 && condition2)
    {
    code
    }
    I apply this style when condition1 and condition2 are parallel, that's mean they are independent.

    if(condition1)
    {
    if(condition2)
    {
    code
    }
    }
    I apply this style when codition2 depend on condition1.

    It is just based on 1 criterion, I have no idea about speed of execution.

  • #7
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    The nested ifs will be marginally faster, notable only when executing the else clause.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards


  •  

    Posting Permissions

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