Double Negative One Liner



  • It's Python, and it always takes me a moment to try and decide whether or not I have it backwards when I try to read it. I inherited it as part of a project originated by a non-developer, so this is far from the worst thing in it, but most of the other oddities are hard to fully appreciate in one line.  Making it better would involve touching a bunch of other places to keep everything in sync, and I have higher priorities.  As simple is it is, it always makes me chuckle for some reason...

     if not globalSettings.noDaily == True:



  • A coworker of mine, we do all of our stuff in PHP, used to be in the automotive industry, and I don't mean as a developer. When reading his code I tend to run into the following.

    [code]if(expressionA || (!expressionA && expressionB))[/code]

    I tried to tell him why he doesn't want to do this if expressionA is expensive, but it doesn't seem to sink in. Also, to nip a potentially very valid objection in the bud, in these cases it's not important that expressionA be false if expressionB is true.



  • It seems lots of developpers have a lot of problems with booleans expressions.

    Most of the time they just cannot simplify their code. Some of the code I had to maintain, and ultimately rewrote had the following all the way :

    void SomeMethod(int argument1)
    {
    bool isOkay = true;
    if (argument1 > 10)
    {
    isOkay = true;
    }
    else
    {
    isOkay = false;
    }

    bool isStillOkay = true;
    if (isOkay)
    {
    if (DoThis(argument1) == 0)
    isStillOkay = true;
    else
    isStillOkay = false;
    }

    if(isStillOkay)
    {
    if (DoThat(argument1) == 0)
    {
    isStillOkay = true;
    }
    else
    {
    isStillOkay = false;
    }
    }

    }

    At least they haven't used bool isStillStillOkay...
    EDIT : I can't get the code right ...

    [mod - reformatted code. <pre>/</pre> and html escaping helps - PJH]



  • @toon said:

    I tried to tell him why he doesn't want to do this if expressionA is expensive, but it doesn't seem to sink in.
     

    No, you don't explain it, you tell him that it is dumb and stupid and that he should be ashamed of himself.



  • @dhromed said:

    @toon said:

    I tried to tell him why he doesn't want to do this if expressionA is expensive, but it doesn't seem to sink in.
     

    No, you don't explain it, you tell him that it is dumb and stupid and that he should be ashamed of himself.

    Lazy evaluation is something people don't know about, if how they learned programming was through one of those really, really crappy PHP tutorials some amateur DTP guy wrote. But it's a pitfall, and an optimization tool, and an important one at that. And it's not even that hard to grok, even for amateur DTP guys. It boggles my mind why he doesn't understand it.



  • @toon said:

    Lazy evaluation is something people don't know about, if how they learned programming was through one of those really, really crappy PHP tutorials some amateur DTP guy wrote. But it's a pitfall, and an optimization tool, and an important one at that. And it's not even that hard to grok, even for amateur DTP guys. It boggles my mind why he doesn't understand it.
    This isn't about lazy evaluation. First, it's called conditional evaluation. Second, what's lazy about a || (!a && b)? If a, it gets evaluated once, if !a, it gets evaluated twice, and the end result is the same. Only if there had been a clever side effect in a, it would have made sense (as in: obfuscation makes a lot of sense).



  • @toon said:

    @dhromed said:

    @toon said:

    I tried to tell him why he doesn't want to do this if expressionA is expensive, but it doesn't seem to sink in.
     

    No, you don't explain it, you tell him that it is dumb and stupid and that he should be ashamed of himself.

     

    When you talk to him, begin aligning your speech patterns to his logic structures, eg:

    You: Are you $person?

    Him: No.

    You: So, if you're NOT $person, this message is intended for you. Are you NOT $person?

    Him: No. I mean, I'm not $person. 

    You: so YES, you are NOT $person. Correct?

    Him: yes. No. I think.

    You: So as this message is intended for people that are NOT $person, and you are NOT $person, this message is intended for you. Correct?

    Him: YES!

    You: .. so here is the message...

     

    When teaching dependency chains and logic trees, I often use examples such as the following 

    You: Would you like a nice cup of tea?

    Him: Yes

    You: Would you like a nice cup of tea with milk?

    Him: Yes.. 

    You: Would you like a nice cup of tea with milk and sugar?

    Him: .. erm... yes..

    etc.

     



  • @forkazoo said:

     if not globalSettings.noDaily != False:
     

    I'd like it better this way, for pure sadistic pleasure.  



  • Why TF did flatdablet's post disappear?


    Anyway, here's my response anyway, converting to valid Python:

    if True if not globalSettings.noDaily != False else False:

     


  • Discourse touched me in a no-no place

    @Zecc said:

    Why TF did flatdablet's post disappear?
    It's in the deleted folder for some reason.



  • @toon said:

    A coworker of mine, we do all of our stuff in PHP, used to be in the automotive industry, and I don't mean as a developer. When reading his code I tend to run into the following.

    <font size="2" face="Lucida Console">if(expressionA || (!expressionA && expressionB))</font>

    I tried to tell him why he doesn't want to do this if expressionA is expensive, but it doesn't seem to sink in. Also, to nip a potentially very valid objection in the bud, in these cases it's not important that expressionA be false if expressionB is true.

    Why does it matter if expressionA is expensive?  If it doesn't short-circuit on the first part of the expression, you know expressionA is false, so you can just write

    if(expressionA || expressionB)



  • @Cassidy said:

    You: Are you $person?

    Him: No.

    You: So, if you're NOT $person, this message is intended for you. Are you NOT $person?

    Him: No. I mean, I'm not $person. 

    You: so YES, you are NOT $person. Correct?

    Him: yes. No. I think.

    You: So as this message is intended for people that are NOT $person, and you are NOT $person, this message is intended for you. Correct?

    Him: YES!

    http://www.ebaumsworld.com/video/watch/801568/ ... Sorry for the ebaum's world, it's the best place I could find it ..



  • @Sutherlands said:

    @toon said:

    A coworker of mine, we do all of our stuff in PHP, used to be in the automotive industry, and I don't mean as a developer. When reading his code I tend to run into the following.

    <font size="2" face="Lucida Console">if(expressionA || (!expressionA && expressionB))</font>

    I tried to tell him why he doesn't want to do this if expressionA is expensive, but it doesn't seem to sink in. Also, to nip a potentially very valid objection in the bud, in these cases it's not important that expressionA be false if expressionB is true.

    Why does it matter if expressionA is expensive?  If it doesn't short-circuit on the first part of the expression, you know expressionA is false, so you can just write

    if(expressionA || expressionB)

    First of all, that is the whole point. Anyhow, it matters because that pattern of his is a comfort thing. I sometimes find it in a permissions context, he'll think: "well you need permissions to see this block of code, but if you don't have them, then you should still be able to see it if you're an admin". This way, the code aligns with his train of thought. That's all fine by me, except if the code to check for permissions needs a gazillion database queries. In that case, if you don't have any, then you're doing two gazillion queries where you only need one. And if the is-admin check is cheap, then you're actually doing an entire gazillion database queries where you need zero.



  • @Cassidy said:

    @toon said:

    @dhromed said:

    @toon said:

    I tried to tell him why he doesn't want to do this if expressionA is expensive, but it doesn't seem to sink in.
     

    No, you don't explain it, you tell him that it is dumb and stupid and that he should be ashamed of himself.

     

    When you talk to him, begin aligning your speech patterns to his logic structures, eg:

    You: Are you $person?

    Him: No.

    You: So, if you're NOT $person, this message is intended for you. Are you NOT $person?

    Him: No. I mean, I'm not $person. 

    You: so YES, you are NOT $person. Correct?

    Him: yes. No. I think.

    You: So as this message is intended for people that are NOT $person, and you are NOT $person, this message is intended for you. Correct?

    Him: YES!

    You: .. so here is the message...

     

    When teaching dependency chains and logic trees, I often use examples such as the following 

    You: Would you like a nice cup of tea?

    Him: Yes

    You: Would you like a nice cup of tea with milk?

    Him: Yes.. 

    You: Would you like a nice cup
    of tea with milk and sugar?

    Him: .. erm... yes..

    etc.

     

    I remember this movie where Gene Hackman plays a prosecutor who is charged with rape and interrogated by Morgan Freeman IIRC.

    MF You have never been arrested before?

    GH Yes.

    MF stupid expression

    GH (sigh) Yes, I have never been arrested before.



  • @jetcitywoman said:

    @forkazoo said:

     if not globalSettings.noDaily != False:
     

    I'd like it better this way, for pure sadistic pleasure.  

    You know you have to remove the not, right? So

    if  (globalSettings.noDaily != False) != True

    is much better.


  • Discourse touched me in a no-no place

    @toon said:

    MF You have never been arrested before?
    GH Yes.
    MF stupid
    expression

    GH (sigh) Yes, I have never been arrested before.
    Not uncommon in real life. Add it to the 'Would you prefer X or Y' sorts of questions where 'yes' is a valid (but usually obstreperous) answer.


  • Considered Harmful

    Would you prefer cream or sugar? 'Yes' is usually interpreted to mean you want both, though technically it could mean one or the other.



  • @PJH said:

    but usually obstreperous
    Not seeing how this word applies.



  • @TGV said:

    @jetcitywoman said:

    @forkazoo said:

     if not globalSettings.noDaily != False:
     

    I'd like it better this way, for pure sadistic pleasure.  

    You know you have to remove the not, right? So

    if  (globalSettings.noDaily != False) != True

    is much better.

     

     

     

    I'm glad to know I'm not the only one who thinks it should be False in order to maintain the weir rhythm of the line.  Here is another excerpt from it.  It's actually two lines of code, so it isn't quite as dense, but it still shows some of the character without really posting too much.  Showing some of the repetetive Pokemon exception handling would simply be too lengthy and dull to be entertaining.

     

                print "INSERT INTO Look_{0}_shots (Shot_Numb,Shot_Seq,Shot_jobName,Shot_Durat,Shot_Etat,Shot_TcIN,Shot_RefFrame,Shot_TcFrRate) \
                    VALUES ('{1}','{2}','{3}',{4},'Offline','{5}',{6},'{7}')".format(jobNumb,leJob.shotname,leJob.scene,leJob.shot,leclip.duration(),\
                    leclip.fileAttrib.tc,leclip.frame('in'),leJob.jobDetail.fps
                    )
                cursor.execute("INSERT INTO Look_{0}_shots (Shot_Numb,Shot_Seq,Shot_jobName,Shot_Durat,Shot_Etat,Shot_TcIN,Shot_RefFrame,Shot_TcFrRate) \
                    VALUES ('{1}','{2}','{3}',{4},'Offline','{5}',{6},'{7}')".format(jobNumb,leJob.shotname,leJob.scene,leJob.shot,leclip.duration(),\
                    leclip.fileAttrib.tc,leclip.frame('in'),leJob.jobDetail.fps
                    ))

     

    I'm going to start by ignoring the fact that an SQL query is constructed manually.  This isn't web code, and in this context it isn't really a security issue vulnerable to injection.  It's a python script with a abked in username and password, so anybody who can run it can trivially pwn the DB.  Instead, I'll draw your attention to printing the query string for debugging purposes, but instead of stuffing the query into a variable and the printing and executing it, we construct the query in two separate places, guaranteeing that as you debug it, you will eventually get these two lines out of sync at some point.  In the string formatting to build the query string, you also have one hard coded value in the middle of the substitutions, so that the list of columns and the list of variables to stuff int those columns are *almost* the same.  And of course, the fun mixing of under_scores and camelCase, English and French, occasional definite articles, which make a veritable smorgasboard of variable naming conventions.  Almost no two variable are ever named according to exactly the same logic anywhere in the project.

     



  • @setasensei said:

    void SomeMethod(int argument1)
    {
    bool isOkay = true;
    if (argument1 > 10)
    {
    isOkay = true;
    }
    else
    {
    isOkay = false;
    }

    bool isStillOkay = true;
    if (isOkay)
    {
    if (DoThis(argument1) == 0)
    isStillOkay = true;
    else
    isStillOkay = false;
    }

    if(isStillOkay)
    {
    if (DoThat(argument1) == 0)
    {
    isStillOkay = true;
    }
    else
    {
    isStillOkay = false;
    }
    }

    }

    Did he WANT to do this?

    void SomeMethod(int argument1)
    {
         if (argument1 <= 10 || DoThis(argument1) == 0) DoThat(argument1);
    }

    Or was he trying to do that?

    void SomeMethod(int argument1)
    {
         if (argument1 > 10 && DoThis(argument1) == 0) DoThat(argument1);
    }

    (Where he should have initialized isStillOkay with false instead of true.) (Edit: assuming that the method really ended there)



  • if x is true
        y = true
    else if x is false
        y = false
    else if x is null
        y = null
    else if x is zero
    


  • @Sutherlands said:

    @toon said:

    A coworker of mine, we do all of our stuff in PHP, used to be in the automotive industry, and I don't mean as a developer. When reading his code I tend to run into the following.

    <font size="2" face="Lucida Console">if(expressionA || (!expressionA && expressionB))</font>

    I tried to tell him why he doesn't want to do this if expressionA is expensive, but it doesn't seem to sink in. Also, to nip a potentially very valid objection in the bud, in these cases it's not important that expressionA be false if expressionB is true.

    Why does it matter if expressionA is expensive?  If it doesn't short-circuit on the first part of the expression, you know expressionA is false, so you can just write

    if(expressionA || expressionB)

    No, you cannot write it like that. What happens when both sides of the || are true? You have to make sure, if the left is true, that the right is false...

    For a programmer it is obvious that True || True -> True. For the non-programmer, they might confuse it with the XOR.



  • Non-programmers programming: the root of so many troubles. And this site.


  • Discourse touched me in a no-no place

    @Thomfox said:

    No, you cannot write it like that.
    Of course you can.
    @Thomfox said:
    What happens when both sides of the || are true?
    If you rely on side effects that depend on both them being evaluated, you evaluate both of them before the expression. Then use the results in the expression. The whole point of the shortcut is that you don't need the second evaluation if the first determines the result. There are idioms in C that reply on it for example [if (p && p->member)]@Thomfox said:
    You have to make sure, if the left is true, that the right is false
    Er - no you don't. If you want XOR, you - as a programmer - write it as such.@Thomfox said:
    For the non-programmer
    Why are you wanting people, who know nothing about logic, writing programs?



  • @Thomfox said:

    For the non-programmer, they might FUCK IT UP TOTALLY BY INTERFERING IN THINGS THAT THEY ARE UNSKILLED AND UNQUALIFIED IN AND SHOULD LEARN TO STAY WELL AWAY.
     

    FTFY.

    I'LL STOP SHOUTING NOW.



  • @PJH said:

    @Thomfox said:
    What happens when both sides of the || are true?
    If you rely on side effects that depend on both them being evaluated, you evaluate both of them before the expression. Then use the results in the expression. The whole point of the shortcut is that you don't need the second evaluation if the first determines the result. There are idioms in C that reply on it for example [if (p && p->member)]

    PHP has two sets of logical operators, && and || vs "and" and "or". The latter has very low precedence so there's an idiom in PHP like this:

    mysql_connect(...parameters here...) or die('MySQL connection failed, call Fat Joe!!!11!');

    Personally, I'm not sure I'm a fan of that. What's to stop someone from doing this:

    (init_database() and init_session() and init_page()) or (prepare_error_message(FATAL_ERROR) and display_error_message())

    @PJH said:

    @Thomfox said:
    For the non-programmer
    Why are you wanting people, who know nothing about logic, writing programs?

    Also, why the hell would a non-programmer know about XOR? I'd bet a nice sum of dough, that not all of the programmers I work with know about XOR.



  • @toon said:

    @PJH said:
    @Thomfox said:
    For the non-programmer
    Why are you wanting people, who know nothing about logic, writing programs?

    Also, why the hell would a non-programmer know about XOR? I'd bet a nice sum of dough, that not all of the programmers I work with know about XOR.

    The "or" in natural language is more understood as an either or, which translates to XOR. There the confusion of OR working like XOR might come from and thus this odd pattern to ensure that either parameter is true, not both at the same time.

    Yes I know this forum is all programmers and we got used to how boolean logic works. Have you ever had the moment, that you get confused by natural language for a moment?



  • @Thomfox said:

    @toon said:
    @PJH said:
    @Thomfox said:
    For the non-programmer
    Why are you wanting people, who know nothing about logic, writing programs?

    Also, why the hell would a non-programmer know about XOR? I'd bet a nice sum of dough, that not all of the programmers I work with know about XOR.

    The "or" in natural language is more understood as an either or, which translates to XOR. There the confusion of OR working like XOR might come from and thus this odd pattern to ensure that either parameter is true, not both at the same time.

    Yes I know this forum is all programmers and we got used to how boolean logic works. Have you ever had the moment, that you get confused by natural language for a moment?

    To be perfectly honest, no. But that doesn't mean much, I started when I was like 8 or 9 on my dad's Atari ST and I am a Dutchman, so the words OR and AND were foreign to me anyway. The confusion was no issue for me, because to me it was computer language. But I can certainly see your point. Apparently, some people get confused by those words in SQL queries, like they might want all rock albums and all blues albums, so they write WHERE genre='rock' AND genre='blues'. I would be lying if I said that were a pitfall I've ever fallen into but I can certainly see why it might confuse a beginner. Also I was warned about that particular one when I learned SQL... from a book (you read that right! if only every so called programmer did that...).



  • @Thomfox said:

    The "or" in natural language is more understood as an either or, which translates to XOR.
    Is this in fact the case?  Consider the following:

    1. A mother tells her child "you may have one cookie or one piece of candy before dinner".  Exclusive "or" is meant here; the child is not allowed to take one of each.
    2. A boss tells his employee "if you show up for work drunk or naked, I'll fire you".  Here, natural language interprets the word "or" as inclusive.


  • @Thomfox said:

    No, you cannot write it like that. What happens when both sides of the || are true? You have to make sure, if the left is true, that the right is false...

    For a programmer it is obvious that True || True -> True. For the non-programmer, they might confuse it with the XOR.

    I was hoping you were kidding, but unfortunately it looks as though you weren't.  I don't want anybody writing programs that I have to work on that doesn't understand basic boolean logic in their language of choice.  In the same vein, I don't want anyone cooking a meal for me that doesn't know the difference between salt and pepper.



  • @da Doctah said:

  • A boss tells his employee "if you show up for work drunk or naked, I'll fire you".  Here, natural language interprets the word "or" as inclusive.
  • But my employment tribunal interpreted it as exclusive.


    That's why I still work here.



  • @Zecc said:

    Non-programmers programming: the root of so many troubles. And this site.

    Not to mention what happens when "programmers are non-programming" <eek>


  • Considered Harmful

    @Hatshepsut said:

    @da Doctah said:
  • A boss tells his employee "if you show up for work drunk or naked, I'll fire you".  Here, natural language interprets the word "or" as inclusive.
  • But my employment tribunal interpreted it as exclusive.


    That's why I still work here.

    The errata published as a result of his trial now reads "if you show up for work drunk and/or naked, I'll fire you."



  • @toon said:

    A coworker of mine, we do all of our stuff in PHP, used to be in the automotive industry, and I don't mean as a developer. When reading his code I tend to run into the following.

    <font face="Lucida Console" size="2">if(expressionA || (!expressionA && expressionB))</font>

    I tried to tell him why he doesn't want to do this if expressionA is expensive, but it doesn't seem to sink in. Also, to nip a potentially very valid objection in the bud, in these cases it's not important that expressionA be false if expressionB is true.

    I don't think that objection would be valid anyway. The way that's written, the overall condition is true if expressionA is true. If it's really important that expressionA be false under any circumstance, then that expression is broken.



  • @bridget99 said:

    @toon said:

    A coworker of mine, we do all of our stuff in PHP, used to be in the automotive industry, and I don't mean as a developer. When reading his code I tend to run into the following.

    <font face="Lucida Console" size="2">if(expressionA || (!expressionA && expressionB))</font>

    I tried to tell him why he doesn't want to do this if expressionA is expensive, but it doesn't seem to sink in. Also, to nip a potentially very valid objection in the bud, in these cases it's not important that expressionA be false if expressionB is true.

    I don't think that objection would be valid anyway. The way that's written, the overall condition is true if expressionA is true. If it's really important that expressionA be false under any circumstance, then that expression is broken.

    TROLL!! HOW COULD YOU BELIEVE THAT?!? YOU ARE LEADING THE YOUNG ONES ASTRAY WITH THIS DRIVEL!!



    Sincerely,



    Some millimeter-shallow member of "Generation Don't-Tase-me-Bro" who's never kissed a woman
    (CISA)



  • @bridget99 said:

    @bridget99 said:
    @toon said:

    A coworker of mine, we do all of our stuff in PHP, used to be in the automotive industry, and I don't mean as a developer. When reading his code I tend to run into the following.

    <font face="Lucida Console" size="2">if(expressionA || (!expressionA && expressionB))</font>

    I tried to tell him why he doesn't want to do this if expressionA is expensive, but it doesn't seem to sink in. Also, to nip a potentially very valid objection in the bud, in these cases it's not important that expressionA be false if expressionB is true.

    I don't think that objection would be valid anyway. The way that's written, the overall condition is true if expressionA is true. If it's really important that expressionA be false under any circumstance, then that expression is broken.

    TROLL!! HOW COULD YOU BELIEVE THAT?!? YOU ARE LEADING THE YOUNG ONES ASTRAY WITH THIS DRIVEL!!



    Sincerely,



    Some millimeter-shallow member of "Generation Don't-Tase-me-Bro" who's never kissed a woman
    (CISA)


    Somebody hijack your account? Or did somebody break rule #1: don't drink and surf...



  • @bridget99 said:

    @toon said:

    A coworker of mine, we do all of our stuff in PHP, used to be in the automotive industry, and I don't mean as a developer. When reading his code I tend to run into the following.

    <font face="Lucida Console" size="2">if(expressionA || (!expressionA && expressionB))</font>

    I tried to tell him why he doesn't want to do this if expressionA is expensive, but it doesn't seem to sink in. Also, to nip a potentially very valid objection in the bud, in these cases it's not important that expressionA be false if expressionB is true.

    I don't think that objection would be valid anyway. The way that's written, the overall condition is true if expressionA is true. If it's really important that expressionA be false under any circumstance, then that expression is broken.

     

    You're right.  For that possible objection to be valid, the expression would have to read:

     <font face="Lucida Console" size="2">if((expressionA && !expressionB) || (!expressionA && expressionB))</font>

    or perhaps, to more "intuitively" capture the XOR intent:

      <font face="Lucida Console" size="2">if((expressionA<font size="2"> ||</font> expressionB) <font size="2">&&</font> (!<font size="2">(</font>expressionA && expressionB)))</font>

    @bridget99 said:

    TROLL!! HOW COULD YOU BELIEVE THAT?!? YOU ARE LEADING THE YOUNG ONES ASTRAY WITH THIS DRIVEL!! 

    Sincerely, 

    Some millimeter-shallow member of "Generation Don't-Tase-me-Bro" who's never kissed a woman (CISA)

    You were so close to making a decent contribution to a thread....

    Also, why would you proactively complain about being labelled a troll when you already admitted you are one?

     


Log in to reply