Brilliant or brillant, this is the boolean



  • IsMoving and IsRotating always being defined (we use a subset of javascript
    where those two variables actually have the boolean type, so they can never have
    any other value than true or false, not even "undefined") and my collegue who
    wrote this insisted that it was "best practice" to use !!  So, hmm, ideas ?


    function UpdateIsActive() {
     var isAc = !!IsMoving || !!IsRotating;
     //...
    }


    PS: yes I said "javascript", not not kill me please...
    [:)]

     



  • This is just about as bad as [code]"The number is :" . ($number_in_a_string+0)[/code]
    (Converts a string to a number )
    While i dont completely disagree with these practices i dont like teaching it as "best practices".

    In fact you dont need this ( ugly ) hack so why even use it ?



  • This is almost as good as when my friend just learned about pointers.  He created a varriable, then derefrenced the address-of operator some 27 times.


    int i;
    ****(&(&(&(&i))))  = 343;
    cout << 343 << endl;

    Only, you can't do that because & needs an l-value, (visual studio, maybe GCC lets you?)  he split it up into multiple variables to hold those refrences, but I'm too lazy to do so.  That and I don't want to send it through my compilier.



  • If they were integers, and you were comparing them, you'd want a ! or !! to ensure they were strictly 0 or 1.



    But || is a boolean operator, promoting both sides to boolean anyway..



  • @WIldpeaks said:

    IsMoving and IsRotating always being defined (we use a subset of javascript
    where those two variables actually have the boolean type, so they can never have
    any other value than true or false, not even "undefined") and my collegue who
    wrote this insisted that it was "best practice" to use !!  So, hmm, ideas ?


    function UpdateIsActive() {
     var isAc = !!IsMoving || !!IsRotating;
     //...
    }

    This is completely stupid, any value or object gets promoted to boolean when used in a JS boolean operation anyway, if it exists it's positive, if it doesn't it's negative, specific values may cast differently ("", 0 and "0" all cast to false)

    So no, it's not "best practice" of anything, especially not if the variables are always defined, and even less if they're already booleans



  • @WIldpeaks said:


    PS: yes I said "javascript", not not kill me please...
    [:)]



    So... you desire someone to kill you, then?


  • Ha, someone noticed the pun [:P]



  • This is completely stupid, any value or object gets promoted to boolean when used in a JS boolean operation anyway, if it exists it's positive, if it doesn't it's negative, specific values may cast differently ("", 0 and "0" all cast to false)

    So no, it's not "best practice" of anything, especially not if the variables are always defined, and even less if they're already booleans

    Exactly my first reaction too, but he was looking so convinced of it that I had a doubt


Log in to reply