Straight to the point boolean evaluation



  • It's late, and I feel too tired to understand what the author meant with

    if (!Page.IsPostBack!=false)
    {...}
    

    I feel dizzy even thinking about operator precedence in this one.



  • @H|B said:

    if (!Page.IsPostBack!=false)


    This was clearly written by a pessimist.



  • Wow, that looks ugly, but it doesn't matter which one has precedence.  Either way, it boils down to this:

    if (!Page.IsPostBack) 



  • My bet is that the origin of this line went along these lines:

    Originally, it just said

    if (Page.IsPostBack)

    Somebody who understood equality but not boolean algebra, and additionally was not very bright and/or sober, observed that this was wrong, so they miscorrected it to:

    if (Page.IsPostBack!=false)

    And then in the next revision somebody observed that the bug still hadn't been fixed, so they inserted another !

    I have seen this kind of duct-tape programming all too often - when somebody finds a flaw, they don't even attempt to improve the code or figure out a good solution, they just do the first thing they think of and move on. This process inevitably compounds stupidity with ignorance and then throws incompetence into the mix.



  • @asuffield said:

    This process inevitably compounds stupidity with ignorance and then throws incompetence into the mix.

    Which can be applied on the whole source file where that donut came from. Each page down brings a hidden surprise! Sometimes, conveniently inside a #region...



  • @Pap said:

    @H|B said:
    if (!Page.IsPostBack!=false)



    This was clearly written by a pessimist.

    A sadist, perhaps. 



  • You know, every friggin' code sample for ASP.NET that has to do with doing something only when the page is first loaded begins with:

     if (!Page.IsPostBack)

    I've seen lots of wannabe developers who don't understand that the "!" is for getting the opposite of whichever value IsPostBack holds. Many people who are just beggining to learn how to program have C# for their first language (I personally think people should begin with C or C++...) and rely too much in code samples which not always use good practices. It's not uncommon here to find some who will just take the exclamation mark for a feature of the Page object, as if it was part of the name of the variable in question.

    But "if (variable != false)" instead of "if (variable)" does strike me as odd, even for neophytes.



  • @Pap said:

    @H|B said:
    if (!Page.IsPostBack!=false)



    This was clearly written by a pessimist.

    You sir owe me a new keyboard... lmao.



  • How These Things Happen 

    "if(!Page.IsPostBack)"

    Intern: That's sloppy. I'll correct it. (not seeing the ! at the beginning)

    "if(!Page.IsPostBack==true)"

    Developer: This looks wrong. I'll submit a bug report.

    Intern: Oh, I guess I got it backwards.

    "if(!Page.IsPostBack!=true)"

    Developer 2: This doesn't work. I'll fix it.

    (Debugs)

    Developer 2: Ahh, Page.IsPostBack is false. Wait... WTF?

    (Reads)

    Developer 2: Okay, I don't know why we have this double negative, but it's probably some obscure workaround and I'm not messing with it.

    "if(!Page.IsPostBack!=false)"

     



  • @CDarklock said:

    "if(!Page.IsPostBack)"

    Intern: That's sloppy. I'll correct it. (not seeing the ! at the beginning)

     

    why in the hell would that be sloppy???



  • @campkev said:

    @CDarklock said:

    "if(!Page.IsPostBack)"

    Intern: That's sloppy. I'll correct it. (not seeing the ! at the beginning)

     

    why in the hell would that be sloppy???

    Because he's an intern.  Don't argue, my logic is infallible!

    One could possibly argue that he was taught to always use "==true" in statements like that to show that the first statement obviously returns a boolean, as does the second statement, and we are comparing for equality.  This way, even interns can understand it.



  • @campkev said:

    @CDarklock said:

    "if(!Page.IsPostBack)"

    Intern: That's sloppy. I'll correct it. (not seeing the ! at the beginning)

    why in the hell would that be sloppy???

    (sorry for highjacking the character, CDarkLock)

    Intern: yeah, it works, but the guy who wrote this could have put more effort into it by adding a few more words. Also, verbose make stuff less ambiguous. This way I can be sure that it's actually checking for a "true" value.



  • @durnurd said:

    @campkev said:
    @CDarklock said:

    "if(!Page.IsPostBack)"

    Intern: That's sloppy. I'll correct it. (not seeing the ! at the beginning)

     

    why in the hell would that be sloppy???

    Because he's an intern.  Don't argue, my logic is infallible!

    One could possibly argue that he was taught to always use "==true" in statements like that to show that the first statement obviously returns a boolean, as does the second statement, and we are comparing for equality.  This way, even interns can understand it.

     

    Anybody who would teach someone that should be shot.  That's just dumb. 



  • @campkev said:

    @durnurd said:
    @campkev said:
    @CDarklock said:

    "if(!Page.IsPostBack)"

    Intern: That's sloppy. I'll correct it. (not seeing the ! at the beginning)

     

    why in the hell would that be sloppy???

    Because he's an intern.  Don't argue, my logic is infallible!

    One could possibly argue that he was taught to always use "==true" in statements like that to show that the first statement obviously returns a boolean, as does the second statement, and we are comparing for equality.  This way, even interns can understand it.

     

    Anybody who would teach someone that should be shot.  That's just dumb. 

    It's not relevant with all languages, but sometimes it's useful to make a distinction between checking whether the variable has any value at all (cf. NULL), whether it's a boolean true or false, or whether it contains 1 or 0. Some of these concerns are, perhaps, less important in more strongly-typed languages. I have learned to make a habit of doing this in PHP projects:

    $foo=do_something();
    if ($foo===false) {

    // ...

    }

    Just in case I'm in a situation where do_something should return NULL.


Log in to reply