• Sir Smackalot (unregistered) in reply to ChiefCrazyTalk

    Dan was refering to the sig in the post two above his, where Brillant is spelled Brilliant.

  • </Life> (unregistered) in reply to Coder
    Anonymous:
    Also if the variable is null then invert_bool does nothing, instead of returning null


    var_dump(false == NULL) // bool(true)... sh*t!
  • (cs) in reply to Satanicpuppy
    Satanicpuppy:
    lofwyr:
    Anonymous:
    The WTF is 73 (and counting) responses to a piece of code that does exactly what it is meant to do. 


    And could have been replaced by a simple "NOT" operator. May not be the worst WTF ever, but unless someone is paid by lines of code that mentioned piece of code could have been avoided. Only inexperienced or sloppy developers write more _code_ than necessary.

    l.



    Not true. The minimum possible code is often unreadable.


    Not true either, depending on the language.

    Satanicpuppy:

    Better to have a couple of extra lines to improve readability.


    Of course you could break apart calculations, etc. But I don't see how that would help  in this case.  Comments could be also useful, but that is not _code_.


    Satanicpuppy:

    Experienced coders, as opposed to prima donnas, know this.


    Let me know, when you've become one of those ... experienced coders I mean.

    l.
  • (cs) in reply to johnl
    johnl:
    There are good and not so good books all over the series.  I for one thought that the best books were Eric, Soul Music and Night Watch, though I've not read Going Postal


    Going Postal starts off serious, but the last third will have you in splits. Not as funny as the initial few books, but still good.
  • (cs) in reply to einstruzende
    einstruzende:
    kipthegreat:
    Anonymous:

    Ah, this is very good error handling. The function returns nothing when the caller accidentally passes a $val that is not a bool. That way it's easy to tell when you've passed it bad data. Clearly the exclamation point has none of that kind of power.

     



    If this is indeed PHP, then everything has a boolean value, so one of "== true" and "== false" will always return true.

    Now, if he had written the function like this, then a non-boolean would get passed through with nothing changed (again, assuming this is PHP):

    <FONT size=+0>function</FONT> invert_bool($val){
    <FONT size=+0>if</FONT>($val === <FONT size=+0>true</FONT>)
    <FONT size=+0>return false</FONT>;
    <FONT size=+0>if</FONT>($val === <FONT size=+0>false</FONT>)
    <FONT size=+0>return true</FONT>;
    }

    Of course, maybe that is the behavior he was trying to get.  You can tell from the javadoc-style comment that his primary language is Java, and he wasn't used to a weakly-typed language like PHP.  Maybe he was afraid that ! would return something weird for a non-boolean?  Hard to say, but I think he was probably a Java programmer who was learning a new language when he wrote this.



    Now WTF would that function return if the parameter wasn't boolean?  Nothing? File Not Found?  I'm thankful i'm in a land where such nonsense is a lot harder to pull off (C# & Java).

    yup. shoulda been:

    function invert_bool($val)

        if($val === true) { return false;}
        if($val === false){ return true; }
        return $val;
    }

  • (cs) in reply to masklinn
    masklinn:
    Uh, yeah, right, funny how Ruby manages string interpolation without prefixing the fucking variable names eh.

    What PHP has is not a feature, it's a damn retarded stupidity, because while it does have a meaning it perl it has none in PHP

    The $ prefix for variables is certainly not meaningless. I have no problem with people pointing out valid issue's they have with the language, it's by no means perfect, but at least have the courtesy of actually knowing what you're talking about before you label part of a language as stupid.


  • Mike (unregistered)

    <font>/**
    * @return true | false
    * @param $val bool The bool to invert
    * @desc Returns the
    */</font>
    <font>function</font> invert_bool($val){
    <font>if</font>($val == <font>true</font>)
    <font>return false</font>;
    <font>if</font>($val == <font>false</font>)
    <font>return true</font>;
    }

    If this guy was writing the same code for C/C++ he would get a compiler error "Not all control paths return a value" or something similar.

    Instead he could have written:

    function invert_bool( $val ){
        if( $val == true )
            return false;
        return true;
    }



    -Mike

  • Peter (unregistered) in reply to olddog
    olddog:
    Anonymous:
    Anonymous:
    olddog:

    Zero is a theoretical bone. There's always the other half.

    olddog, what does your sig means?

    mean, I mean

    Just a quantum pun about the number line. Do you believe in zero, or do you believe there's always half of a half...of a half?



    There are two zeroes in floating-point land. -0 and +0 are equal, but if you divide one by them, you get two different results.

    1.0/-0.0; -> -Infinity
    1.0/+0.0;  -> Infinity

    Well, this works as above in the JavaScript Console under Firefox. Some languages consider dividing by zero to actually be an error. Funny that.

    Anyway, this is good for amusing your mathematically inclined friends. Just ask them to find two floating-point numbers a and b that satisfy:

    (1) a == b
    (2) 1/a != 1/b

    It's a real WTF for people who think floats work like reals in math.
  • foo! foo! food\ (unregistered) in reply to Peter

    So you need experience to forget about boolean algebra.... that's interesting. I always knew there was something wrong with doing things the shortest way possible.

  • (cs) in reply to Satanicpuppy
    Satanicpuppy:

    And could have been replaced by a simple "NOT" operator. May not be the worst WTF ever, but unless someone is paid by lines of code that mentioned piece of code could have been avoided. Only inexperienced or sloppy developers write more _code_ than necessary.


    Not true. The minimum possible code is often unreadable. Better to have a couple of extra lines to improve readability. Experienced coders, as opposed to prima donnas, know this.


    You are correct.

    But not today.

    As you subtly implied, you see yourself as an experienced coder. You should know then, that as far as coding practices go, there are no absolutes. You can be right sometimes, rarely, or most of the time. But this case is extremely simple and doesn't qualify as a "coding practice".

    The function is a pure wrapper around the ! operator. It does nothing extra. Hence there are no situations where it is actually useful.
  • Rob (unregistered) in reply to My Name
    Anonymous:
    bugsRus:
    Hey give the coder a break.  Maybe the 1 key on thier keyboard was broken!!!!!!!


    Then he could have written it shorter (1 line instead of 4!):

    function invert_Bool($val) {
        return $val?false:true;
    }


    Then he should have used the Win32 character map util and copied each "!" :P
  • &#198;var (unregistered) in reply to wakeskate
    wakeskate:
    Kooch:
    function invert_bool($val) {
        return $val ? false : true;
    }


    Actually, I think that Joshua had it right but at the same time I want to use a tertiary operator :P

    function invert_bool($val) {
        return ($val == true) ? !$val : !$val;
    }


    Or even neater:

    function invert_bool($val) {
            return !(bool)$val;
    }
  • (cs) in reply to ammoQ

    Speaking of not liking exclamation points, I often wish that modern languages had the "until (condition)" statement, defined as "while (!condition)".

    eRet = oFile.readFirst ();
    until (eRet == retEOF)
       {
       // process the record

       eRet = oFile.readNext (sFilename);
       }

    instead of

    eRet = oFile.readFirst ();
    while (eRet != retEOF)
       {
       // process the record

       eRet = oFile.readNext (sFilename);
       }

    or

    bMustEnd = false;
    do
       {
       // process something
       }
    until (bMustEnd);

    instead of

    bMustEnd = false;
    do
       {
       // process something
       }
    while (!bMustEnd);

  • (cs) in reply to Jenny Simonds
    Jenny Simonds:

    Speaking of not liking exclamation points, I often wish that modern languages had the "until (condition)" statement, defined as "while (!condition)"

    Nice.. If I git this, you want:

    until($arg==false){}

    vs

    while($arg==true){}


    Since you've started a wish list, I'd like to see ( in crude syntax ):

    skip(these){}

    vs

    for(each){}

  • (cs) in reply to Jenny Simonds
    Jenny Simonds:

    Speaking of not liking exclamation points, I often wish that modern languages had the "until (condition)" statement, defined as "while (!condition)".

    Ruby does. It also has an "unless" statement which is (as you'd have guessed) the opposite of "if", and condition-inversion for one liners (taken from perl):

    1. doSomething if condition
    2. doSomething unless condition
    3. doSomething while condition
    4. doSomething until condition

    Which are respectively equivalent to

    • if condition
          doSomething
      end
          
    • unless condition
          doSomething
      end
          
    • while condition
          doSomething
      end
          
    • until condition
          doSomething
      end
          
  • (cs) in reply to masklinn

    Ruby is quirky. o_O

    I hope it doesn't go the same route as PHP what with all the 'added features' and 'handy tools'. PHP is virtually 1-dimensional and the string functions reference page is a couple screens long for just the functions.

    I mean, at some point, a language is complete, and you can build your features yourself.

    But I suppose, a to-die-for feature native to a language is probably better than what I can come up with. :)

  • (cs) in reply to dhromed
    dhromed:
    Ruby is quirky. o_O

    Not that quirky in fact (and much of the quirkiness I found in it seems to come from perl, I don't like the reverse condition for example and it comes straights from perl). One thing it pushes forward, though, is the sheer english pronunciation of the code: try reading the 2 forms out loud, you'll realize that post-condition is an exact translation of usual natural language constructs.

    dhromed:
    I hope it doesn't go the same route as PHP what with all the 'added features' and 'handy tools'. PHP is virtually 1-dimensional and the string functions reference page is a couple screens long for just the functions.

    I mean, at some point, a language is complete, and you can build your features yourself.

    But I suppose, a to-die-for feature native to a language is probably better than what I can come up with. :)

    Well, given the fact that Ruby has no function and hates built-in functions (i'm not sure there is any built-in functon in Ruby apart from basic IO things like puts, and even them are methods of a global "base" object) it won't.

    The ruby guys don't even use loops anyway (if/unless tests are another matter), the ruby-way is to use iterators and "blocks" (FP-inherited constructs functions as first-class objects with read/write closure over their context)

    I still prefer Python atm, but Ruby's cool and very straightforward (Ruby's mostly follows the PoLS, the Principle of Least Surprise, and it's syntax is geared towards this goal).

  • Alex (unregistered) in reply to Random Human
    Anonymous:
    And the corresponding "return_bool" function would be:

    function return_bool($val) {
        return !invert_bool($val);
    }

    omgplz! the return_bool function gotta look like this: function return_bool($val) { return invert_bool(invert_bool($val)); }

    everything else would be plain wrong

  • (cs) in reply to Peter
    Anonymous:
    olddog:
    Anonymous:
    Anonymous:
    olddog:

    Zero is a theoretical bone. There's always the other half.

    olddog, what does your sig means?

    mean, I mean

    Just a quantum pun about the number line. Do you believe in zero, or do you believe there's always half of a half...of a half?



    There are two zeroes in floating-point land. -0 and +0 are equal, but if you divide one by them, you get two different results.

    1.0/-0.0; -> -Infinity
    1.0/+0.0;  -> Infinity

    Well, this works as above in the JavaScript Console under Firefox. Some languages consider dividing by zero to actually be an error. Funny that.

    Anyway, this is good for amusing your mathematically inclined friends. Just ask them to find two floating-point numbers a and b that satisfy:

    (1) a == b
    (2) 1/a != 1/b

    It's a real WTF for people who think floats work like reals in math.

    As you say.  There are two zeroes in floating-point land.

    Thus if:

    a is -0
    b is +0

    and

    -0 and +0 are equal.

    and if

    1.0/a; -> -Infinity
    1.0/b;  -> Infinity

    then the challenge to satisfy both

    (1) a == b
    (2) 1/a != 1/b

    suggests that zero is not stable, but has tendencies. funny how that works.

  • Ken (unregistered)

    at least they could have used else if or at least just else. This is a good one though... lol.

  • Fireblaze (unregistered)

    Read the code to a friend over the phone and have him type it down.... If the types it right the first time its good code.

    "CAPTCHA Validation Incorrect".... :P

  • (cs) in reply to Dave
    Dave:
    Ah, this is very good error handling. The function returns nothing when the caller accidentally passes a $val that is not a bool. That way it's easy to tell when you've passed it bad data. Clearly the exclamation point has none of that kind of power.  

    Precisely. The real wtf is the post itself! Lol.. or maybe it was a "trick" post? ;-)

  • Adam (unregistered)

    Surely, someone should have simply settled for:

    function invert_bool($val) {
        return $val ^ true;
    }
    

Leave a comment on “Avoiding The Dreaded Exclamation Point”

Log In or post as a guest

Replying to comment #:

« Return to Article