Better way to compare booleans



  • It's the same old story: i inherited a bad website which i now need to maintain while convincing management to scrap it and invest in a new site.
    Anyways, getting to know the code, and i find this:

    $result = check_email_address($email);
    $result = $result ? "true" : "false";
    if ($result == "false" ) {.........

    why do if ($result) or if (!$result) when you can create strings then compare them with string literals, right ?



  • You never know you might need a "FileNotFound".



  •  It's a long time ago I did something with PHP, which this seems to be, but I remember something with the == operation that made them also need ===. Something with the type or something?

     I don't think so, but could it be that check_email_address can do funny things and this is a defense against that?



  • @irreal said:

    if ($result == "false" ) {.........
     

    Should be

     if ((($result == "false")==false)==false) {.........

    for maximum falseness.

     



  • You never know you might need a "FileNotFound".

    why would you ever need "FileNotFound" ?

    it's obviously on an embedded system :)



  •  ITOH, we don't know here (but YOU do ^^) what type of value is returned by the check_email_adress function : could it return a non-boolean value (used elsewhere), that would be evaluated here as true/false for another purpose ?It seems weird but itcould be a way to understand why (the fuck) are our heads aching in front of that odd code. ^^



  • hmm, i thought it'd be obvious enough. check_email_address does a few regex checks, if all of them pass, it returns true, if any fails, returns false.

    the check_email_address function is also pasted from some tutorial, along with comments clearly aimed at people who are seeing regex, or maybe even php for the first time.
    EDIT: here, this is the check_email_address function http://pastebin.com/x39G7gEM



  • @irreal said:

    hmm, i thought it'd be obvious enough. check_email_address does a few regex checks, if all of them pass, it returns true, if any fails, returns false.



    the check_email_address function is also pasted from some tutorial, along with comments clearly aimed at people who are seeing regex, or maybe even php for the first time -_-



    -EDIT-

    here, this is the check_email_address function

    http://pastebin.com/x39G7gEM

    Actually that is pretty obvious. toshir0 is just retarded (I can tell from the double-hat type emoticons).



  •  RETARD_NOT_FOUND

     

    (and, btw, if you haven't ever found an error or bug or logic fail hidden behind something that is "pretty obvious", either you're the retard, either, ... erm... you'll soon be 4 years old like my daughter ? ;) (OMG he DID use a smiley again... it's pretty obvious that he IS a retard !)



  • Yes, yes you are. Does your daughter lack as much humor as you do?



  • @ammoQ said:

     if ((($result == "false")==false)==false) {.........

    for maximum falseness.

     

    It's pretty hard to surpass the tremendous falseness of the code as-is.



  • @RogerWilco said:

    I remember something with the == operation that made them also need ===. Something with the type or something?
     

    I suppose that check_email returns a proper bool, which means the OP code is just plain shit.

     The == operator does type conversion, which is also shit, but because it's implementation in both PHP and JS/ECMA is shitty. It's nice and compact to have "" and undefined evaluate to false, but it also creates logical paradoxes such as:

    ('0' == false) => true
    ('' == false) => true

    therefore:

    ('' == '0')

    It also means the literal 0 (zero) evaluates to false, despite the fact that in nearly any business case, 0 is simply a numerical value that exists and should not convert to false.

    Hence the extraneous operators === and !== which mean (sametype && equal) and (sametype && !equal) respectively.



  • What is the WTF in this code? I can't see it.

    Please send your responses to my email adress at <font face="courier new,courier">""@.......</font>.



  • @derula said:

    Does your daughter lack as much humor as you do?
     

    Ok, then. Maybe i've been too defensive-aggressive here. Although being a still-nearly-beginner in IT, I do love the cynic, ironic and somewhat "elitist" ways of TDWTF, but I wouldn't ever post here without my stronghat on and my gun loaded. In case. MorbiusSomething could show up. chills



  • @toshir0 said:

    MorbiusSomething
     

    It's "wilters", so named after the process which erect penises undergo when confronted with his "ebullient" personality.



  • @toshir0 said:

     ITOH, we don't know here (but YOU do ^^) what type of value is returned by the check_email_adress function : could it return a non-boolean value (used elsewhere), that would be evaluated here as true/false for another purpose ?It seems weird but itcould be a way to understand why (the fuck) are our heads aching in front of that odd code. ^^

    Of course, in that case, [code]!$result[/code] would yield a proper boolean that has the opposite truth value of the result (and [code]!!$result[/code] would return a proper boolean with the same truth value as the result). There is no excuse for the OP.



  • @dhromed said:

    @toshir0 said:

    MorbiusSomething
     

    It's "wilters", so named after the process which erect penises undergo when confronted with his "ebullient" personality.

     

    That would be 'withers'...



  • @steenbergh said:

    That would be 'withers'...
     

    That means the same.



  • You're right, my bad.



  • @toshir0 said:

    Filed under: [...], yes i'm french click here to hate me

    I clicked and it didn't work. Can you fix that?



  • @derula said:

    Can you fix that?
    Yes*.

    But it would make the tag lie. No way, baby.

     

    * in fact, not yet (don't forget i'm a newbie). Can you help me now that we're very close friends ? :-)



  • @derula said:

    @toshir0 said:
    Filed under: [...], yes i'm french click here to hate me

    I clicked and it didn't work. Can you fix that?

    Maybe it has problems targeting the correct person, because I didn't click it but I hate him anyway.



  • @bstorer said:

    Maybe it has problems targeting the correct person, because I didn't click it but I hate him anyway.
    it's not a bug, it's a feature.



  • @toshir0 said:

    * in fact, not yet (don't forget i'm a newbie). Can you help me now that we're very close friends ? :-)

    Try to add me on facebook.



  • @dhromed said:

    It also means the literal 0 (zero) evaluates to false, despite the fact that in nearly any business case, 0 is simply a numerical value that exists and should not convert to false.
     

     Quoted for truth.



  • @Mason Wheeler said:

    @dhromed said:
    It also means the literal 0 (zero) evaluates to false, despite the fact that in nearly any business case, 0 is simply a numerical value that exists and should not convert to false.
    Quoted for truth.

    So 0 terminated strings are a good idea why?



  • @derula said:

    I'm not on facebook because I have no friends.
    Strange indeed. That's precisely the reason why most facebook-addicts use it.

    Btw, I make my best to keep up in that humour-lacking, deaf-to-irony style, but it's hard. I so much want to cheer and sing with you all, my friends. Too bad.



  • But please for the sake of the next guy maintaining your code, use

    (bool) instead of !! to cast a non bool to a bool in php. 



  • @derula said:

    @Mason Wheeler said:
    @dhromed said:
    It also means the literal 0 (zero) evaluates to false, despite the fact that in nearly any business case, 0 is simply a numerical value that exists and should not convert to false.
    Quoted for truth.

    So 0 terminated strings are a good idea why?

     

    They aren't.

    @Joel Spolsky said:

    Is this the only way to store strings? No, in fact, it's one of the
    worst ways to store strings. For non-trivial programs, APIs, operating systems, class libraries, you
    should avoid [null-terminated] strings like the plague.



  • @dhromed said:

    ('' == '0')

    This evalutes as false in PHP.

     

    I love type coercion.  I suppose it creates problems if you don't know how it works, but that's true of any language feature.  And in 99% of cases it is easier and more elegant than having to cast shit all the time.



  • @dhromed said:

    @toshir0 said:

    MorbiusSomething
     

    It's "wilters", so named after the process which erect penises undergo when confronted with his "ebullient" personality.

    Look, man, I don't know about you but I'm okay with the fact that guys don't want to fuck me.



  • @morbiuswilters said:

    I love type coercion.  I suppose it creates problems if you don't know how it works, but that's true of any language feature.  And in 99% of cases it is easier and more elegant than having to cast shit all the time.

     

    Not knowing how it works doesn't create problems.  Not knowing that it's happening creates problems.  I actually know what I'm doing and still prefer the type safety; seeing as how 99% of PHP programmers don't know what the hell they're doing, maybe it should require explicit casts.



  •  In a sane and consistent world, == would be an assignment operator analogous to += and .=

     a == 'true' would then be equivalent to a = a = 'true'

    The fact that this is not the intended meaning of the op is one of the reasons there's such a thing as an "advanced PHP expert".



  • @da Doctah said:

     In a sane and consistent world, == would be an assignment operator analogous to += and .=

     

    Yes, of course, in a sane and consistent world you would use the same "=" operator for both assignment and equality, just like VB.



  • @Aaron said:

    @morbiuswilters said:

    I love type coercion.  I suppose it creates problems if you don't know how it works, but that's true of any language feature.  And in 99% of cases it is easier and more elegant than having to cast shit all the time.

     

    Not knowing how it works doesn't create problems.  Not knowing that it's happening creates problems.  I actually know what I'm doing and still prefer the type safety; seeing as how 99% of PHP programmers don't know what the hell they're doing, maybe it should require explicit casts.

    Right, because type safety eliminates all bugs.  1965 called, they want their outdated computer science theories back.  Seriously, bad programmers will find a way to fuck up no matter what.  For most business apps that don't need strict typing for performance reasons (only kernels and embedded stuff usually needs that), I would say type safety creates a false sense of security in otherwise competent developers.  For the incompetent ones, it's just another "feature" to work around, say by creating their own boolean with TRUE, FALSE and FILE_NOT_FOUND.



  • @da Doctah said:

     In a sane and consistent world, == would be an assignment operator analogous to += and .=

     a == 'true' would then be equivalent to a = a = 'true'

    The fact that this is not the intended meaning of the op is one of the reasons there's such a thing as an "advanced PHP expert".

    So... "===" would assign the value 3 times in a row?  Also, what the hell does PHP have to do with it?  99% of languages use == for comparison, = for assignment.  PHP just adds === for "comparison without type coercion".



  • @morbiuswilters said:

    So... "===" would assign the value 3 times in a row?  Also, what the hell does PHP have to do with it?  99% of languages use == for comparison, = for assignment.  PHP just adds === for "comparison without type coercion".
    99% of languages don't say that "00" == "0000", because you know, they both look like number, so they must be.



  • @Zecc said:

    99% of languages don't say that "00" == "0000", because you know, they both look like number, so they must be.
    Perl does.



  • @Aaron said:

    @da Doctah said:

     In a sane and consistent world, == would be an assignment operator analogous to += and .=

     

    Yes, of course, in a sane and consistent world you would use the same "=" operator for both assignment and equality, just like VB.

     

    Thank you for going out on a limb and characterizing PL/I as sane and consistent.



  • @da Doctah said:

    Thank you for going out on a limb and characterizing PL/I as sane and consistent.
    GAH!  PL1 FTL, COBOL FTW.  BURN IN HELL PL/1



  • @bstorer said:

    @Zecc said:

    99% of languages don't say that "00" == "0000", because you know, they both look like number, so they must be.
    Perl does.

    Is that supposed to be an argument in favour of PHP?



  • @belgariontheking said:

    Filed
    under: This thread is now about COBOL and PL/1
    Can we also discuss apatosaurus and others, or are we limited to those two specific dinosaurs?



  •  Quick comparison....

    Q:  "What does this three-page PL/I program do?"

    A:  "What doesn't it do?"

     

    Q:  "What does this twenty-page COBOL program do?"

    A:  "Nothing yet.  We're only up to the Environment Division."



  • @toshir0 said:

    @derula said:
    I'm not on facebook because I have no friends.
    Strange indeed. That's precisely the reason why most facebook-addicts use it.

    The difference is that I'm happy with it.



  • @Zecc said:

    @morbiuswilters said:

    So... "===" would assign the value 3 times in a row?  Also, what the hell does PHP have to do with it?  99% of languages use == for comparison, = for assignment.  PHP just adds === for "comparison without type coercion".
    99% of languages don't say that "00" == "0000", because you know, they both look like number, so they must be.

    They both are numbers.  What, exactly, does strict typing buy you here?  If you are comparing two strings that are numerical, they are compared as numbers, not strings.  If you are comparing strings and there is a possibility they could both be numbers, then you need to either use ===.  How is that complicated, or "wrong"?  It's a useful shortcut.  Meanwhile, comparing strings with == will not usually give you what you want in Java or C, so I suppose that means static typing is teh ghey.



  • @da Doctah said:

     Quick comparison....

    Q:  "What does this three-page PL/I program do?"

    A:  "What doesn't it do?"

     

    Q:  "What does this twenty-page COBOL program do?"

    A:  "Nothing yet.  We're only up to the Environment Division."

    Goddamn, pstorer and btk.  It's all fun and games until somebody posts something like this.  Are you happy now?  This is why we can't have nice things.



  • @morbiuswilters said:

    Right, because type safety eliminates all some bugs.

     

    Corrected.

    Language features aren't an all-or-nothing proposition.  Shall we also eschew garbage collection because it doesn't eliminate every conceivable null-pointer or resource leak error? Also, Pascal strings don't completely eliminate subscripting errors, so we probably should just go back to null-terminated strings.



  • @Aaron said:

    @morbiuswilters said:

    Right, because type safety eliminates all some bugs.

     

    Corrected.

    Language features aren't an all-or-nothing proposition.  Shall we also eschew garbage collection because it doesn't eliminate every conceivable null-pointer or resource leak error? Also, Pascal strings don't completely eliminate subscripting errors, so we probably should just go back to null-terminated strings.

    I guess I don't buy that type safety eliminates any bugs.  In fact, with all the extra code overhead of casting, extending, etc. I would say it increases the number of bugs.  Strict typing makes you do all sorts of annoying bullshit to work with generics, collections, etc.  Taken to its logical conclusion, you end up with C++ templates, which are a huge source of fuck-ups.  I've done professional development for awhile and I can recall only a handful of bugs I've encountered that strict typing would have prevented.  However, I have encountered plenty of bugs caused by a fairly competent developer implenting all sorts of type-management boilerplate just to get the fucking compiler to shut up.



  • @morbiuswilters said:

    Goddamn, pstorer and btk.  It's all fun and games until somebody posts something like this.
    This is infinitely better than any of the bantering that was going on before. 

    I generally don't like to ruin good threads, so this one was ripe for the picking.

    So, I just installed FF again on a new comp.  How do I turn on autocomplete?


  • Discourse touched me in a no-no place

    @toshir0 said:

    (but YOU do ^^)
    Isn't that like ROT26. Or something?



    I'm thinking the latter, but wondering why it isn't the former.


Log in to reply
 

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