Clever if/else statement



  • Hey all, got another neat code snippet from this post in the Flash forum, the initial question is quite a common one but it's their second post that has this little gem:

    if(condition){
      result = _root.eff * 10;
    }
    else if (condition){
      result = _root.eff * 2;
    }
    else{
    result = _root.eff * 1;
    }

    trace(result);

    I should point out that _root.eff hasn't actually been set to anything yet so it's still traced as undefined before the if statement even starts.  It's pretty clear that condition has been left in from a copy n paste job but I have this sneaking suspicion that instead of change it's name in the if/else statement they just changed their original var name.  Its hard to tell since I think I scared them off after pointing out the problems in their post...



  • Good God Almighty...

    At first glance I noticed it was a true/false test, so I assumed the WTF would be the else { } block, test for true, then false, and then maybe, I dunno, FileNotFound?

    But my god, can you at least decide what to do on true?

    Good WTF there.



  • [quote user="Tann San"]

    Hey all, got another neat code snippet from this post in the Flash forum, the initial question is quite a common one but it's their second post that has this little gem:

    if(condition){
      result = _root.eff * 10;
    }
    else if (condition){
      result = _root.eff * 2;
    }
    else{
    result = _root.eff * 1;
    }

    trace(result);

    I should point out that _root.eff hasn't actually been set to anything yet so it's still traced as undefined before the if statement even starts.  It's pretty clear that condition has been left in from a copy n paste job but I have this sneaking suspicion that instead of change it's name in the if/else statement they just changed their original var name.  Its hard to tell since I think I scared them off after pointing out the problems in their post...

    [/quote]

     

    Well, obviously condition is volatile. </sarcasm>

     



  • [quote user="Disiance"]

    At first glance I noticed it was a true/false test, so I assumed the WTF would be the else { } block, test for true, then false, and then maybe, I dunno, FileNotFound?

    [/quote]

    Well I don't know... since it's

    if (condition){

      ...

    } else if(condition) { ....

    Which means it's checking for condtion to be true twice in a row - right?

    Maybe it's Flash with threads, and they're checking if condtion changes in those few processor ticks... 🙂

     



  • [quote user="R.Flowers"][quote user="Disiance"]

    At first glance I noticed it was a true/false test, so I assumed the WTF would be the else { } block, test for true, then false, and then maybe, I dunno, FileNotFound?

    [/quote]

    Well I don't know... since it's

    if (condition){
      ...
    } else if(condition) { ....

    Which means it's checking for condtion to be true twice in a row - right?

    Maybe it's Flash with threads, and they're checking if condtion changes in those few processor ticks... :)[/quote]

    Yep, that's what it's doing, but from my first glance I expected it to check true and then false and then else, which would be a WTF, but it's just checking true twice, with a different result in each, which is even a bigger WTF.



  • This must be some definition of "clever" of which I was previously unaware.

     



  • Maybe the process of checking the condition sets it so that the second evaluation of the condition results to true? 

    That would be dastardly clever. 



  • Not possible, as soon as the first if() statement fires, the other two won't get a chance to be parsed. So this is exactly (more of less) what the thread starter said it was, bad copy & pasting job by someone who doesn't know what they're doing.



  • no,  If the first condition returns false but changes a variable so that the next time condition is run it will return true then the second statement wil in fact run.  However it would still suck because of a little thing we call side effects.



  • Perhaps they overloaded the multiplication operator to change the value of condition.  That would make them an u83r l33t k0d3r 🙂


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.