1. Hello Guest! It's with a heavy heart that we must announce the removal of the Legacy GMC Archive. If you wish to save anything from it, now's the time! Please see this topic for more information.
    Dismiss Notice

GML Evaluation Order

Discussion in 'Programming' started by samspade, Sep 1, 2017.

  1. samspade

    samspade Member

    Joined:
    Feb 26, 2017
    Posts:
    2,107
    I have a question about the evaluation order of if statements. I have been using statements like:

    Code:
    
    if (instance_exists(target)) && (distance_to_object(target) < range) {
        /* do something */
    }
    
    
    in code rather than:

    Code:
    
    if (instance_exists(target)) {
        if (distance_to_object(target) < range) {
            /* do something */
        }
    }
    
    
    because the first form was easier for me to read and saved on indentation. I tested it and it wasn't giving me an error if the instance didn't exist so I assumed that it evaluated the if statement in order and (at least for an &&) therefore wouldn't check the second half of the if statement if the first half returned false.

    However, I was reading the help manual about evaluation order and it seems like this may not be true on all platforms. Should I always use the second ordering or are the two forms identical?
     
  2. Insanebrio

    Insanebrio Member

    Joined:
    Jun 30, 2016
    Posts:
    147
    Use the second: its more safe and also nested ifs uses less cpu than using operators (just a very little)
     
  3. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    7,119
    Well they made short circuit evalutations standard in Studio 2, so maybe it's okay now. I was having issues finding any documentation about it not working on certain platforms.
     
  4. CMAllen

    CMAllen Member

    Joined:
    Mar 2, 2017
    Posts:
    856
    This isn't entirely accurate. Depending on the operations, the CPU can evaluate a series of conditionals in a single go, whereas with multiple nested if statements, each conditional must be evaluated one at a time regardless. That being said, I know that GMS2 does, as TheouAegis mentions, short circuit evaluations, while in older version of GM, an entire if statement had to be evaluated regardless of if a conditional made the if() branch false. So this sort of a difficult topic to cover properly. It's more of an 'it depends' situation.

    Regardless, one thing IS for certain -- check your LEAST computationally complex conditionals first, leaving your most complex conditional checks for last.
     
    Perseus likes this.
  5. flyingsaucerinvasion

    flyingsaucerinvasion Member

    Joined:
    Jun 20, 2016
    Posts:
    2,210
    short-circuiting exists in gms1.4 as well. Meaning an evaluation will bail on the first term(?) that is false.
     
    ph101 and CMAllen like this.
  6. CMAllen

    CMAllen Member

    Joined:
    Mar 2, 2017
    Posts:
    856
    I knew GMS2 did, but I wasn't 100% on GMS1.4. Good to know.
     
  7. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    7,119
    it is optional in 1.4
     
  8. zendraw

    zendraw Member

    Joined:
    Jun 20, 2016
    Posts:
    1,376
    even if &&`ing is applied on all platforms, id still use nested ifs, its more readable and flexible, i aways find the need to modify an if the more i progress with a project, and to turn a chain of &&`s into a nest of ifs, is just a waste of time. for optimisation, you shuld nest the most light check first, or if you use &&`s you shuld put it infront, otherwise use the most compatible.
    for me, if somthing throws a small impact on performance, i go with whats most compatible, otherwise, whats most optimal, but i dont think you will get in a situation where you need to choose the most optimal for the sake of compatibility. atleast i havent. maybe with surfaces and dealing with files you will get in such situation.
     
  9. dannyjenn

    dannyjenn Member

    Joined:
    Jul 29, 2017
    Posts:
    569
    I'd just like to point out, the nested if statements are not always a good substitute for the &&.

    Suppose you were doing something more complicated, such as:
    Code:
    if(a&&b){
        // one
    }
    else{
        // two
    }
    If you want to re-write that as nested if statement then you need to do this:
    Code:
    if(a){
        if(b){
            // one
        }
        else{
            // two
        }
    }
    else{
        // two
    }
    which is a lot more difficult to follow than if you were to just use the &&.

    And that's just with a single else statement added. Suppose you were trying to do something even more complex, like this:
    Code:
    if(a&&b){
        // one
    }
    else if(c&&d){
        // two
    }
    else{
        // three
    }
    Try doing that with nested if statements and it quickly becomes nothing but an illegible and inflexible mess.
     
  10. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    7,119
    var i = a;
    if i { i += b; }
    if i == 2 { // one }
    else { // two }

    :p

    You can ignore the second conditional if a and b are not contingent.
     
    dannyjenn likes this.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice