• Sean Connery (unregistered)

    whats wrong with that? This library of booleans manipulation should also have a not_false() function

  • Joshua (unregistered)

    But return !$val is so much less readable... :-)

    the real wtf is the ugly need for '$'!

  • (cs)

    Hey give the coder a break.  Maybe the 1 key on thier keyboard was broken!!!!!!!

  • Coder (unregistered)
    This is super-useful for all those times you want to detect a boolean value change! You can embed instructions into that method that log it, send email to an administrator, etc. Also if the variable is null then invert_bool does nothing, instead of returning null which could possibly eliminate tons of errors. Clearly that is mega useful.
  • diaphanein (unregistered) in reply to Coder

    While I won't knock the usefulness of this (think std::not1 in STL), but how this was done is asinine.

  • Colin (unregistered)

    What about invert_bool(5) ?

  • PS (unregistered) in reply to Colin

    <font size="2">Well, that's easy. It will obviously return File Not Found.</font>

  • (cs)

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

  • DavidK (unregistered) in reply to Colin

    Ah, the joys of PHP.

    You know if you had mentioned the lack of type checking, then he probably would end up with:

    <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($val == 0)
    return 1;
    if($val == 1)
    return 0;
    if(is_null($val))
    return "";
    if($val == "")
    return NULL;
    if($val == "FileNotFound")
    return "FileFound";
    }

    														Or something to that effect ;)<br>
    
  • (cs) in reply to Kooch
    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;
    }
  • (cs)

    He's just future proofing his code incase the specifications for '!' change!  He can simply change one 4 line function while all you suckers have to go through every single file finding-replacing exclamation marks. 

  • (cs) in reply to DavidK
    Anonymous:
    Ah, the joys of PHP.

    You know if you had mentioned the lack of type checking, then he probably would end up with:

    <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($val == 0)
    return 1;
    if($val == 1)
    return 0;
    if(is_null($val))
    return "";
    if($val == "")
    return NULL;
    if($val == "FileNotFound")
    return "FileFound";
    }
    														Or something to that effect ;)<br></div></BLOCKQUOTE><br><br>Actually, that would return after the first two checks. You'd have to use the '===' operator.<br>
    
  • CMDR-ZOD (unregistered) in reply to DavidK

    == means "like". "$x == true" is always true if $x is boolean true or the number 1.
    === checks for type too.

    switch(gettype($val))
        {
        case 'boolean': return $val == true ? true : false; break;
        default:
             return $val; // or return false, handle all types differently or what you want to do
        }


    yeah... bad php ^^.

    dont accept anything with a different type or do something like:
    function bla((boolean) $val)

    than you end with the same thing as in every other language so whats the problem? ;)

  • (cs)

    <FONT color=#006600><FONT face="Times New Roman" color=#000000>I like exclamation points.</FONT></FONT>
    <FONT color=#000099>function</FONT> invert_bool($val){
      <FONT color=#000099>if</FONT>($val != <FONT color=#000099>true</FONT>)
        <FONT color=#000099>return !false</FONT>;
      <FONT color=#000099>if</FONT>($val != <FONT color=#000099>false</FONT>)
        <FONT color=#000099>return !true</FONT>;
    }
  • Dave (unregistered)

    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.

     

  • anonymous (unregistered)

    WOW!!!!!!

     

    [:P]

  • (cs)

    You're all missing the point... he's avoiding programmers double negative confusion syndrome.  How many of you get flustered when your other half asks "aren't you going out?"

  • My Name (unregistered) in reply to bugsRus
    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;
    }
  • (cs)

    Time for the inevitable "What I would do if I saw this in code at my company" jokes:

     

    I wouldve given the coder a sheet of paper that said simply "You are !smart" to see how long it took him to catch on. 

  • (cs) in reply to Kooch

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

    I really hope that people are joking when they suggest using a ternary to perform a negation. That's right up there with: return condition ? true : false instead of plain old return condition.

    That being said, we can now implement isTrue as invert_bool(invert_bool(foo))

  • (cs)

    While there are simpler ways of doing it, I don't really think this is a WTF.  The number rounding thing yesterday was a WTF.  This is just some guy who isn't aware of the negation operator (although I suppose you could argue that in itself is a WTF).  The code he wrote isn't going to blow up the space shuttle; it's just a few lines longer than it has to be.  In my mind this is akin to using a simple if-else instead of the ternary operator.

  • Random Human (unregistered) in reply to OneFactor

    And the corresponding "return_bool" function would be:

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

  • David (unregistered)

    holly php!

  • ChiefCrazyTalk (unregistered) in reply to Joshua
    Anonymous:

    But return !$val is so much less readable... :-)

    the real wtf is the ugly need for '$'!

     

    Maybe its a string variable from the Commodore PET days, circa 1978

  • (cs) in reply to Dave
    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>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>;
    }
    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.
  • (cs)

    I have to agree with some of the suggestions here that he is planning for the eventual change of the meaning of boolean.  When it changes to true, false and unknown we will be scrambling to update all our code and this brillant fellow will only need to add another if statement, recompile and take a vacation.

  • (cs) in reply to versatilia
    versatilia:
    You're all missing the point... he's avoiding programmers double negative confusion syndrome.  How many of you get flustered when your other half asks "aren't you going out?"



    You laugh, but a product I used to support had a boolean configuration variable that contained a double negative in its name. So, to turn this feature off, you'd set the variable to true. It was something like:

    DontNotDoSomethingSomething = 1

    We eventually had to ask the development team to change the name of the config variable, since this name prompted no end to the confusion. It was one of those things that we had to touch just often enough to know it was confusing the last time, but not remember what the solution was.

  • (cs) in reply to DavidK
    Anonymous:
    Ah, the joys of PHP.

    You know if you had mentioned the lack of type checking, then he probably would end up with:

    <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($val == 0)
    return 1;
    if($val == 1)
    return 0;
    if(is_null($val))
    return "";
    if($val == "")
    return NULL;
    if($val == "FileNotFound")
    return "FileFound";
    }
    														Or something to that effect ;)<br></div></BLOCKQUOTE><br>
    

    PHP's loose typing can be a pain, but there are ways around it, namely === and !==.

    if ($val === false)
      return true;
    else if ($val === true)
      return false;
    else
      return 42;
  • (cs)

    Maybe the guy hated Anthrax and got the heebies just thinking about using NOT!

  • (cs)
    /**
    * @return true | false
    * @param $val bool The bool to invert
    * @desc Returns the
    */

    What kind of comment it is!
    desc: Returns the what?
    WTF

  • tweakt (unregistered) in reply to rogthefrog

    Should be rewritten as:

    return isFalse($val);


  • (cs) in reply to luke727

    While there are simpler ways of doing it, I don't really think this is a WTF.  The number rounding thing yesterday was a WTF.  This is just some guy who isn't aware of the negation operator (although I suppose you could argue that in itself is a WTF). 

    You're right, it is a WTF.

  • Rene Saarsoo (unregistered) in reply to mlathe
    mlathe:
    I like exclamation points.

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

    It would be better with even more:

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

  • (cs) in reply to ChiefCrazyTalk
    Anonymous:
    Anonymous:

    But return !$val is so much less readable... :-)

    the real wtf is the ugly need for '$'!

     

    Maybe its a string variable from the Commodore PET days, circa 1978



    Wasn't the $ after the variable name on the PET?  Oh well, thanks for the flashback.
  • Nick (unregistered) in reply to kipthegreat
    kipthegreat:

    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.


    Actually, that'd be phpdoc. It's pretty standard and is based on javadoc. It's quite possible that he hasn't had any experience with Java.
  • (cs) in reply to bugsRus
    bugsRus:
    Anonymous:
    Anonymous:

    But return !$val is so much less readable... :-)

    the real wtf is the ugly need for '$'!

     

    Maybe its a string variable from the Commodore PET days, circa 1978



    Wasn't the $ after the variable name on the PET?  Oh well, thanks for the flashback.


    Tell us all about it dinosaurs ;)
  • (cs)

    Cool, 11 more lines of code, and yet none of it is obviously useless. Plus it won't be hard at all to use this function to create even more lines of code, and yet none will be obviously redundant.

    Instead of: if(!$foo) he can do: $invertedFoo = invert_bool($foo); if($nvertedFoo)

    See, 1 extra line of code, and the "style checker" won't flag it as needless, and thus cut into my wages. $invertedFoo = !$foo; if($invertedFoo) as bad style.

    Personally I'd rather be paid by number of bugs fixed than lines of code, but either way he is well on his way to that new Corvette.

  • internet (unregistered)

    The real wtf  is a  function to invert a bool  in the first place.

  • (cs) in reply to internet

    I am glad to see someone taking the time to make sure the dreaded "Maybe" Bool value doesn't creep into our programs and databases. Me, I would have just done this:

    if(x == true){
    return true;
    }else{
    return false;
    }

    And think of the carnage that might have ensued.

  • Kiss me, I'm Polish (unregistered)
    Alex Papadimoulis:

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


    I don't understand why there's so much checks and stuff.
    The @desc part of the comment clearly states that the function should actually contain just this one line:
    function invert_bool($val) {
        return 'the';
    }
  • Mystery (unregistered) in reply to Djinn

    A useful feature of this code is that it always returns a boolean, even though its parameter may be a string, integer, or anything else. This provides a useful way to convert something to a boolean type:

    $myvar = invert_bool(invert_bool($myvar));

  • (cs) in reply to Satanicpuppy
    Satanicpuppy:
    I am glad to see someone taking the time to make sure the dreaded "Maybe" Bool value doesn't creep into our programs and databases. Me, I would have just done this:

    if(x == true){
    return true;
    }else{
    return false;
    }

    And think of the carnage that might have ensued.


    Err, flip that: return false/return true. More wtfy than I first appreciated.
  • (cs)

    No references to Terry Pratchett yet?

    "Five exclamation marks, the sure sign of an insane mind." etc.

    One of my customers seems to know only two different punctuation marks: tripple+ exklamation marks and tripple+ question marks. His emails read like this:

    Hello Mr. Kitzmueller !!!!

    I have a question to you!!! Can we change xxx in program yyy????

    etc.

    Always a funny read.

  • diaphanein (unregistered) in reply to Cyresse
    Cyresse:

    While there are simpler ways of doing it, I don't really think this is a WTF.  The number rounding thing yesterday was a WTF.  This is just some guy who isn't aware of the negation operator (although I suppose you could argue that in itself is a WTF). 

    You're right, it is a WTF.

    I'd sure hope it is.  I'd sure hope this "developer" would think that there's got to be a better way first time he did this:

    if ($foo){
    } else {
       print $bar
    }

  • EV (unregistered)

    Now for those loves of exclemation points:

    <font>function</font> 1nV3r7_b0oL($val){
    return !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!$val; // OMG!!!!LOL!!!!!!
    }
    														<br>
    
  • (cs) in reply to Djinn
    Djinn:
    bugsRus:
    Anonymous:
    Anonymous:
    But return !$val is so much less readable... :-)

    the real wtf is the ugly need for '$'!

    Maybe its a string variable from the Commodore PET days, circa 1978


    Wasn't the $ after the variable name on the PET?  Oh well, thanks for the flashback.


    Tell us all about it dinosaurs ;)


    Oh, look!  Another hairless monkey we forgot to eat.  This old age bit is wearing.  At least we have our several hundred million year record to fall back on.

    Sincerely,

    Gene Wirchenko

  • (cs) in reply to Gene Wirchenko
    Gene Wirchenko:
    Djinn:
    bugsRus:
    Anonymous:
    Anonymous:
    But return !$val is so much less readable... :-)

    the real wtf is the ugly need for '$'!

    Maybe its a string variable from the Commodore PET days, circa 1978


    Wasn't the $ after the variable name on the PET?  Oh well, thanks for the flashback.


    Tell us all about it dinosaurs ;)


    Oh, look!  Another hairless monkey we forgot to eat.  This old age bit is wearing.  At least we have our several hundred million year record to fall back on.

    Sincerely,

    Gene Wirchenko



    You'll have to forgive us kids.  We haven't been around long enough for the old age bit to start wearing.

    Sincerely,

    !dinosaur
  • (cs) in reply to bugsRus
    bugsRus:
    Anonymous:
    Anonymous:

    But return !$val is so much less readable... :-)

    the real wtf is the ugly need for '$'!

     

    Maybe its a string variable from the Commodore PET days, circa 1978



    Wasn't the $ after the variable name on the PET?  Oh well, thanks for the flashback.


    String variables like A$ were common in all basic dialects of the home computer era, e.g. Commodore 64, Timex/Sinclair ZX81, Acorn BBC, TI 99/4A, MSX, CPC464 etc.

    The opposite  style, e.g. $A is a relict of the unix shell (can't say if it appeared even earlier) and it is definitely a WTF in languages like PHP and Perl, because the $ sign is meaningless there. In the unix shell, $foobar is a variable but foobar is a string (identically to "abc") but in PHP (I think also in Perl, correct if I am wrong), it has no such meaning.
  • (cs) in reply to ammoQ

    Ooops.
    but foobar is a string (identically to "foobar") but ...

  • (cs)

    I really don't see what all the fuss is about. There's nothing wrong with the code (yes, I know if you pass 1 or null or whatever into it, but lets assume he doesn't). The measely 6 lines of code replacing an operator isn't that bad... hell, it's better than brillant Paula.

    It's not wrong, just a little pointless. But for the amount of effort that would've been put into that function, it doesn't really matter that much eh.

    I think the real wtf is, "what is this doing here as a wtf?"

Leave a comment on “Avoiding The Dreaded Exclamation Point”

Log In or post as a guest

Replying to comment #:

« Return to Article