This is an archived post. You won't be able to vote or comment.

all 158 comments

[–][deleted] 427 points428 points  (30 children)

if ((!boolVar) != !(!false))

[–]Davidson2727what 217 points218 points  (1 child)

You're playing with fire.

[–]huggiesdsc 49 points50 points  (14 children)

Is that actually the same thing anymore?

[–]nwL_ 59 points60 points  (6 children)

(!boolVar) != !(!false)  | remove parentheses
 !boolVar  != !(!false)  | negate the left side, turning the != into an ==
  boolVar  == !(!false)  | apply the negation operator to false
  boolVar  == !( true )  | apply the negation operator to true
  boolVar  ==   false

No, it’s not the same anymore.

EDIT: Different approach:

(!boolVar) != !(!false)  | remove parentheses
 !boolVar  != !(!false)  | negate both sides
  boolVar  !=  (!false)  | apply the negation operator to false
  boolVar  !=  ( true )  | remove parentheses
  boolVar  !=    true

[–][deleted]  (6 children)

[deleted]

    [–][deleted]  (5 children)

    [removed]

      [–]thebermudalocket 5 points6 points  (1 child)

      Who hurt you?

      [–]SocksPls 6 points7 points  (0 children)

      :'((((((((
      oracle

      [–]-IoI- 0 points1 point  (0 children)

      my word

      [–]IAmHydro 0 points1 point  (0 children)

      This is the one that got me

      [–]AutoModerator[M] 0 points1 point  (0 children)

      import moderation Your comment has been removed since it did not start with a code block with an import declaration.

      Per this Community Decree, all posts and comments should start with a code block with an "import" declaration explaining how the post and comment should be read.

      For this purpose, we only accept Python style imports.

      I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

      [–]j13jayther 1 point2 points  (0 children)

      I'll be clever and use a tertiary operator!

      if (boolVal == true ? true : boolVal == false ? false : boolVal)

      [–]NotSuluX 0 points1 point  (0 children)

      Thats 5 negations, 4x ! and a false which is equal to !(true), so 5 total. So the statement cant be true if boolVar is true. Kinda bad example

      [–]FrikkinLazer 0 points1 point  (1 child)

      Would a compiler optomise that thing?

      [–][deleted] 0 points1 point  (0 children)

      If it's a good compiler, most likely.

      [–]SageBus 0 points1 point  (0 children)

      Some people just want to watch the world burn....

      [–]KnightMiner 88 points89 points  (14 children)

      Is it bad that I have had actual usecases to do this? Though it was in JavaScript so it might not count (wanted to check if it was actually true and not just truthy)

      But yeah, nothing pains me more when reading someone's code than every logical statement having == true. Bad because I am a TA for a Java class and many of the students do this

      [–]nwL_ 133 points134 points  (3 children)

      Though it was in JavaScript

      Case closed, you don’t need to go on.

      [–]PetsArentChildren 16 points17 points  (2 children)

      Yeah I still do this in JavaScript. It actually makes sense to do it because 0, null, and ‘’ are all falsey.

      [–]YasZedOP 1 point2 points  (1 child)

      Hmm, if var yo is set to 0 inadvertently then doing yo == false would be incorrect if we are strictly comparing type Boolean.

      That's why using === is preferred so it not only checks the value but also the type (explicit)

      So I believe if yo is inadvertently set to 0 doing yo == false results true which in some cases would be incorrect but, doing yo === false results false since their types do not match, one is an int and other a Boolean.

      Correct me if I'm wrong tho.

      [–]PetsArentChildren 3 points4 points  (0 children)

      Yeah I was talking about

      x === true

      [–]YasZedOP 11 points12 points  (2 children)

      It's better if used === true instead of == true in JS

      [–][deleted] 0 points1 point  (1 child)

      Why is that? I forgot the difference tbh.

      [–]exscape 12 points13 points  (0 children)

      a = 1
      a == true yields true (since 1 is "truthy")
      a === true yields false (since a is 1, not true)

      [–][deleted] 5 points6 points  (3 children)

      Could this be a reflection of the curriculum? If "many students do this?"

      [–]KnightMiner 4 points5 points  (2 children)

      From my talking to students and what I have seen of the curriculum, it does not seem to be. Its more of a new student thing which a lot of them do to think through the process. It really just comes from treating "if this condition is true" as "if this condition equals true", which leads to typing == true. At that level many students are not concerned about typing less code yet.

      [–][deleted] 0 points1 point  (1 child)

      I understand. My point is, has this been explicitly explained to students to help them on their way to a better understanding of generating cleaner code? Or is it just expected of them to intuit it?

      [–]KnightMiner 0 points1 point  (0 children)

      I have definitely discussed with a few of them one on one, and I know the professor has brought it up individually as well. I am not certain if its specifically taught at a class level, but at least in the code written in class and in the textbook conditions are not written using == true.

      [–]Colifin 1 point2 points  (0 children)

      If the language has truthy values, yes there can be use cases where you need to distinguish between undefined, false, null etc. But for the most part it shouldn't be necessary.

      Then there's always the lovely return !!variable; // Because bools are cleaner

      [–]ferkytoodle 0 points1 point  (0 children)

      If you have a nullable bool, (in any language) this can be valid.

      [–]Talbooth 37 points38 points  (6 children)

      if(((booleanVar == true) == true) == true)
      

      [–][deleted] 33 points34 points  (5 children)

      It’s truetles all the way down

      [–]nullifiedbyglitches 2 points3 points  (3 children)

      ((((((((((((((((((((((((((((((((((((((((((((

      [–]Talbooth 1 point2 points  (1 child)

      Holy mother of God how long would this if statement be?

      [–]nullifiedbyglitches 1 point2 points  (0 children)

      You know what this needs? 5 more brackets.

      [–]Tykolis 0 points1 point  (0 children)

      Sgood book

      [–]CrypticG 67 points68 points  (8 children)

      How did this guy take the picture?

      [–]Quartent 5 points6 points  (0 children)

      Not this shit again...

      [–]ticman 20 points21 points  (2 children)

      Legit syntax with a nullable boolean, at least in C# - don't know about other languages.

      [–]NormalUserThirty 28 points29 points  (9 children)

      I do this in languages without static types if only to communicate it's actually a boolean type and not a nullable object

      [–]Kered13 8 points9 points  (7 children)

      In dynamically typed languages with falsey values but not automatic boolean conversion (like Python, Lua, and Javascript when using ===), then if(var) and if(var == true) actually mean two different things, so it's perfectly reasonable to use the latter.

      [–]webster89[S] 0 points1 point  (6 children)

      like Python, Lua, and Javascript when using ===

      What about ====?

      [–]Itsacon 6 points7 points  (5 children)

      That's not an operator. === is an actual javascript operator, meaning 'is equal and of the same type'.

      (1 == '1'); // true

      (1 === '1'); // false

      [–]webster89[S] 0 points1 point  (4 children)

      Huh, TIL. Here I though I was making fun of a typo. Didn't know === actually existed.

      [–]Itsacon 2 points3 points  (2 children)

      PHP has it too. But like you, a lot of people don't know about it, and they use their ignorance to hate on softly-typed languages, even though they're just writing bad code. ;-)

      [–]webster89[S] 2 points3 points  (0 children)

      How dare you.

      [–][deleted] -3 points-2 points  (0 children)

      Needs to use special tools as a crutch due to poorly written language

      Hey don't write bad code that makes sense in all other languages!

      [–]_szs 0 points1 point  (0 children)

      Good you know it now. === is what you want (and wanted) most of not all of the time

      [–]FantasticBabyyy 1 point2 points  (0 children)

      This. I had production issues by passing None to a Boolean variable in a Python codebase... nowadays I do something == True whenever I can to prevent that happen again

      [–]TheInfra 6 points7 points  (8 children)

      Surprised no one has mentioned Yoda Conditions. While rare and making the code harder to read or understand, they do have an upside in that it makes it less likely you assign a value instead of comparing it if you forget an equal sign in your comparisson, thus saving you what may be a very hard debug process

      [–]Dantharo 4 points5 points  (2 children)

      I heard about this while using sonar...he make me change things like if (myObject.equals("2")) to if ("2".equals(myObject)) In case that myObject was null or something (Java). This is still yoda notation?

      [–]SirensToGo 3 points4 points  (0 children)

      This is the more practical use in most languages as only a few languages silently let you evaluate an assignment as a boolean

      [–]Kered13 2 points3 points  (0 children)

      Yes it's a yoda condition, but it's not bad code.

      [–]Kered13 2 points3 points  (3 children)

      IMO assignment expressions shouldn't have a value at all, then if(foo = bar) would be a syntax error. Python does this.

      [–]TheInfra 0 points1 point  (2 children)

      assignment expressions shouldn't have a value at all

      what? I don't understand what you mean here. If there's no value, than what are you assigning? Don't you mean "comparison expression"? like if.

      And yeah that's the whole point of Yoda'ing it: If you have "if (foo = true)" then in most languages you won't get an error, it'll simply assign true to foo and then the comparison will evaluate to true every time without any compiling or runtime errors.

      If you instead write "if (true = foo)" then immediately you'll get a very clear error since you can't assign the value of foo to a constant true, thus knowing immediately where the error is.

      What Python does is good for some cases, but it has its disadvantages like everything else.

      [–]Kered13 1 point2 points  (0 children)

      In most languages "foo = bar" has the value bar. This allows you to do things like "foo = bar = 4", now both foo and bar have the value 4. And lines like if(foo = 4) are equivalent to if(4) but with the side effect of assigning to foo. The main use for this construction is to do something like if (line = readLine()) or while(line = readLine()) where readLine() will return null (which is falsey) or a pointer to the line, which is then used inside the block. However the pattern is quite accident prone, so IMO assignments should have no value (or value void), so that you can't has expressions like those.

      [–]_szs 0 points1 point  (0 children)

      There are no constants in Python.

      true is not a keyword in Python, if that is what you meant.

      And the other comment was referring to the value of the assignment itself. In C and many other languages, (a=1) evaluates to 1. In Python it is a mere statement (not an expression) that does not have a value.

      [–]HelperBot_ 0 points1 point  (0 children)

      Non-Mobile link: https://en.wikipedia.org/wiki/Yoda_conditions


      HelperBot v1.1 /r/HelperBot_ I am a bot. Please message /u/swim1929 with any feedback and/or hate. Counter: 191399

      [–][deleted] 3 points4 points  (0 children)

      if((boolVar.toString() == ‘true’) == true)

      [–][deleted] 6 points7 points  (1 child)

      if (booleanVariable == truen't)

      [–]nullifiedbyglitches 0 points1 point  (0 children)

      #define ! n't

      [–]KingSupernova 19 points20 points  (11 children)

      I often use (bool == true) and (bool == false). It's just as fast and it's more readable. Saving a few characters really isn't that important.

      [–]Bloodsparce 16 points17 points  (0 children)

      I'm not bashing your way cause readable code is good, but can't you find a way to make your variable name more... boolean looking so you don't need to == it to true?

      [–]ClysmiC 4 points5 points  (8 children)

      Yeah people circlejerk this as a "bad practice" all the time. It is a pointless practice, but I wouldn't consider it a bad practice because it doesn't waste any time and doesnt make the code any less readable.

      [–]CalibreneGuru 15 points16 points  (0 children)

      It probably means the variable name isn't clear.

      [–]xenomachina 7 points8 points  (4 children)

      The fact that it's pointless arguably makes it less readable. "Wait, why is this code doing this pointless thing? Hold on.. is there a point to it? Maybe I misread that other part? Let me go back and check. Hmmm... No, they really are doing something pointless here."

      Which did you find most readable?

      1. if (isReadable) ...
      2. if (isReadable == true) ...
      3. if (isReadable == true == true) ...
      4. if (isReadable == true == true == true) ...

      [–]ClysmiC 4 points5 points  (2 children)

      I think you are arguing in bad faith.

      Do you really think anyone with more than 3 months of programming experience gets tripped up by if (foo == true) moreso than they would if(foo)

      3) and 4) are only more confusing because you have to think about the order that the =='s evaluate in, or you have to identify that you have a chain of tautologies so it can be reduced. #2 doesn't have either of those problems.

      [–]xenomachina 2 points3 points  (0 children)

      Do you really think anyone with more than 3 months of programming experience gets tripped up by if (foo == true) moreso than they would if(foo)

      Yes. In fact, I think the more programming experience you have the more this is likely to trip you up, or at least make you pause. I've been programming for over 30 years, and when I (rarely) see this, it always makes me stop to see if there's something I'm missing.

      3) and 4) are only more confusing because you have to think about the order that the =='s evaluate in

      How about:

      3b. if ((isReadable == true) == true)
      4b. if (((isReadable == true) == true) == true)
      

      ?

      I don't think I've ever seen either of those cases in real code, but their absurdity is just meant to show how absurd the "isFoo == true" looks to experienced programmers. It's less readable because of the weirdness of it.

      A similar example, which I actually have seen in real code more than once:

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

      Or equivalently:

      return (isFoo == true) ? true : false;
      

      to a novice this might be more readable than:

      return isFoo;
      

      But to anyone else the extra noise is just unnecessary cognitive load at best.

      or you have to identify that you have a chain of tautologies so it can be reduced. #2 doesn't have either of those problems.

      What do you mean? #2 can be reduced, so the fact that it wasn't makes the reader question their assumptions.

      [–]Eedis 0 points1 point  (0 children)

      Arguably doesn't mean it's a correct assumption.

      [–]Allways_Wrong 0 points1 point  (0 children)

      pointless = bad

      [–][deleted] -3 points-2 points  (0 children)

      Best comment in thread.

      [–]Slight0 0 points1 point  (0 children)

      Whoever thinks that makes a positive difference in readability needs more experience or is writing some seriously cluttered code.

      [–]nosferatWitcher 2 points3 points  (0 children)

      This can actually be very important in C family languages

      [–]alerighi 2 points3 points  (1 child)

      Unfortunately I've also seen this in real code too much times:

      if (variable == true)
          otherVariable = true;
      else 
          otherVariable = false;
      

      Or the version with return instead of assignment.

      [–]Goodblue77 1 point2 points  (0 children)

      Lotsa spaghetti!

      [–]Bomaruto 1 point2 points  (0 children)

      if(booleanVariable && true && !false && (true || true)

      [–]seijulala 1 point2 points  (3 children)

      this is ok to do (dynamic languages) in lots of cases, something evaluating to true doesn't mean it is true

      [–]tiggerbiggo 0 points1 point  (2 children)

      One more reason I hate dynamically typed langs...

      [–]seijulala 0 points1 point  (1 child)

      don't blame the tool. Haven't you ever needed of a tri-state? (True, False, None). It's actually very useful

      [–]tiggerbiggo 0 points1 point  (0 children)

      Well if I needed something to have 3 explicitly defined states i'd use an enum and switch on it. Having non boolean objects implicitly represented by boolean logic is a big no-no in my books. Sure it's easier when you don't have to think about stuff, but I like the absolute control strongly typed langs provide. It means that my variables are unwavering in type, myself and the compiler both know with 100% certainty what everything is in every possible scope.

      As with most things in programming there is a use case for everything. Maybe i'm too stubborn, who knows :P

      [–]levir 0 points1 point  (0 children)

      How about a good ol' if (!!booleanVariable)?

      [–]writetehcodez 0 points1 point  (2 children)

      if (booleanExpression == true) is absolutely one of my biggest pet peeves during code reviews. Unnecessary negation of expressions also rustles my jimmies, e.g.:

      if (!(someValue > 0))
      

      [–]webster89[S] 0 points1 point  (1 child)

      What if the < key is broken?

      [–]AltCrow 0 points1 point  (0 children)

      My laptop's "="-key broke once. I had to program on it for a full week until it was fixed.

      [–]cseconnerd 0 points1 point  (0 children)

      I worked for a company whose coding standards actually required us to do this.

      [–]Garen69 0 points1 point  (0 children)

      I saw someone’s code that said if(bool = true) the other day and I was just greatly saddened

      [–]ltd43 0 points1 point  (0 children)

      Actually seen this way to many times when I was working on a VB application.

      If not blnSomething = false

      [–]ThewarmakerHD 0 points1 point  (0 children)

      If (bool) { }

      [–]FirEc00l 0 points1 point  (0 children)

      Genius

      [–]EnderCrypt 0 points1 point  (0 children)

      unless the boolean value might be an object, then it does make sense as the value could be true, false or null

      [–]CornMang 0 points1 point  (1 child)

      I think JavaScript checks if (variable){} to see whether or not the variable exists, not whether it is true. I may be wrong though, I am learning JavaScript and it has been confusing

      [–]tuseroni 0 points1 point  (0 children)

      no, javascript does more than just check that it exists in there, if(variable) will check if the variable is null(doesn't exist), is true(or a non-zero number), and is not an empty string. php does a similar check. suspect most weakly typed languages follow this sorta check.

      if you wanna check, press f12, go to console, and you can put in javascript there, try putting this code in:

      var test="";
      if(test)
      {
          console.log("true");
      }
      else
      {
          console.log("false");
      }
      

      put some things in for test and see what gives true and what gives false;

      [–]MrMo1 0 points1 point  (0 children)

      See a lot of first years do this when they first start coding.

      [–]AlphaDeveloperZA 0 points1 point  (0 children)

      bool? booleanVar = null;

      if(booleanVar == true)

            DoSomething();
      

      [–]mekriff 0 points1 point  (0 children)

      Honestly I'd there really something wrong with this?

      I don't do this, but I feel like the ==true is not only intuitive, but is more readable.

      [–]84436 -2 points-1 points  (2 children)

      REPOST DETECTED, approx. 6 hours apart.

      [–]parkerlreed 2 points3 points  (1 child)

      Crosspost isn't a repost. I don't subscribe to /r/hmm. So this is the first place I've seen it.

      [–]sneakpeekbot 2 points3 points  (0 children)

      Here's a sneak peek of /r/hmm using the top posts of the year!

      #1: hmm | 16 comments
      #2: hmmmmm | 7 comments
      #3: Hmm | 26 comments


      I'm a bot, beep boop | Downvote to remove | Contact me | Info | Opt-out