Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. The Weird and The Wonderful
  4. Do you not understand booleans?

Do you not understand booleans?

Scheduled Pinned Locked Moved The Weird and The Wonderful
data-structuresquestionannouncement
65 Posts 30 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A alanevans

    This stuff drives me up the wall!!!

    bool is_queue_empty(void)
    {
    if (queue_length==0)
    {
    return true;
    }
    else
    {
    return false;
    }
    }

    Or this:

    bool counter_zero = counter==0 ? true : false;

    Or this:

    if (isUDPSetup()==true)
    {
    if ((forceSend==false))
    {
    ...
    }
    }

    (Variable names have been changed to protect the guilty) Or this *New one*:

    void setNeedsUpdate(bool update)
    {
    if ((update==true))
    NeedsUpdate=true;
    else
    NeedsUpdate=false;
    }

    R Offline
    R Offline
    Rick Shaub
    wrote on last edited by
    #40

    Maybe he saw this boolean[^], got confused, and wanted to "make sure".

    S 1 Reply Last reply
    0
    • L Lost User

      Screw the compiler. It takes longer to read and understand, therefore it sucks.

      F Offline
      F Offline
      Fabio Franco
      wrote on last edited by
      #41

      And it obviously makes the reader scared of what else might come from that code who's developer can't understand even a booleans...

      "To alcohol! The cause of, and solution to, all of life's problems" - Homer Simpson

      M 1 Reply Last reply
      0
      • A alanevans

        This stuff drives me up the wall!!!

        bool is_queue_empty(void)
        {
        if (queue_length==0)
        {
        return true;
        }
        else
        {
        return false;
        }
        }

        Or this:

        bool counter_zero = counter==0 ? true : false;

        Or this:

        if (isUDPSetup()==true)
        {
        if ((forceSend==false))
        {
        ...
        }
        }

        (Variable names have been changed to protect the guilty) Or this *New one*:

        void setNeedsUpdate(bool update)
        {
        if ((update==true))
        NeedsUpdate=true;
        else
        NeedsUpdate=false;
        }

        R Offline
        R Offline
        rcampbell12
        wrote on last edited by
        #42

        Hey, you're coding in a language that I don't use, but I'm totally with you. This is one of my pet peeves, too. One of my languages is Visual FoxPro, which has the "Immediate IF" ternary function, IIF: IIF(expr, a, b) If expr is true, a is returned, otherwise b is returned. So what I see is people doing this: MyBoolVar = IIF(SomeVar = 2, .t., .f.) Which could be simplified to: MyBoolVar = SomeVar = 2 Much cleaner. Or they expand it into a regular IF statement similar to what you posted. When I see this it does worry me. If programming should result from logical thinking and a programmer has trouble understanding logical values (boolean values), then should we wonder about the rest of their skills?

        1 Reply Last reply
        0
        • R Rick Shaub

          Maybe he saw this boolean[^], got confused, and wanted to "make sure".

          S Offline
          S Offline
          Stefan_Lang
          wrote on last edited by
          #43

          Wow - that link is worthy of a top position in the Hall of Shame all by itself! :omg:

          1 Reply Last reply
          0
          • L Lost User

            Yes, I suppose this simply comes from the old C++ days where there was no boolean type. To avoid side effects or compiler specific behavior, I always explicitly specified what I was testing. But I would write it like this:

            if((flag1 == true) &&
            (flag2 == true) &&
            (flag3 == true))
            {
            ...
            }

            Usually it does not look so uniform. If I really had so many different flags, I would think about using a flag word and testing all flags in one go.

            And from the clouds a mighty voice spoke:
            "Smile and be happy, for it could come worse!"

            And I smiled and was happy
            And it came worse.

            K Offline
            K Offline
            Kabwla Phone
            wrote on last edited by
            #44

            (edit: I posted this as a reply to a message that is now now longer there, oh well) I do not write out the boolean, but I do write each condition on a new line. In addition to that, I start the new line with the operator, this way, it is very easy to see at the beginning of the line that it is a continuation of the previous line, and what the operation is:

            if( flag1
            && (someOtherFlagThatWillSqrewWithTheLayout == MagicNumbers.Ten)
            && (flag3 == somethingElseCompletely))
            {
            ...
            }

            I use this style with anything that will make a line of code too long:

            //Contrived Deep Nesting, line too long
            var firstChildRow= SomeTypedDataSetWithSillyLongNameThatFillsTheEntireCodeWindow.Tables[0].ChildRelations[0].ChildTable.Rows[0];
            //Broken up for readability. Note that I start with the 'dot'.
            var firstChildRow= SomeTypedDataSetWithSillyLongNameThatFillsTheEntireCodeWindow
            .Tables[0]
            .ChildRelations[0]
            .ChildTable
            .Rows[0];

            1 Reply Last reply
            0
            • A alanevans

              This stuff drives me up the wall!!!

              bool is_queue_empty(void)
              {
              if (queue_length==0)
              {
              return true;
              }
              else
              {
              return false;
              }
              }

              Or this:

              bool counter_zero = counter==0 ? true : false;

              Or this:

              if (isUDPSetup()==true)
              {
              if ((forceSend==false))
              {
              ...
              }
              }

              (Variable names have been changed to protect the guilty) Or this *New one*:

              void setNeedsUpdate(bool update)
              {
              if ((update==true))
              NeedsUpdate=true;
              else
              NeedsUpdate=false;
              }

              N Offline
              N Offline
              Nunnenkamp
              wrote on last edited by
              #45

              void setNeedsUpdate(bool update)
              {
              if ((update==true))
              NeedsUpdate=true;
              else
              NeedsUpdate=false;
              }

              This would be better if they returned it too. Nothing like getting back what you put into it.

              bool setNeedsUpdate(bool update)
              {
              if ((update==true))
              NeedsUpdate=true;
              else
              NeedsUpdate=false;
              return NeedsUpdate;
              }

              F Y 2 Replies Last reply
              0
              • N Nunnenkamp

                void setNeedsUpdate(bool update)
                {
                if ((update==true))
                NeedsUpdate=true;
                else
                NeedsUpdate=false;
                }

                This would be better if they returned it too. Nothing like getting back what you put into it.

                bool setNeedsUpdate(bool update)
                {
                if ((update==true))
                NeedsUpdate=true;
                else
                NeedsUpdate=false;
                return NeedsUpdate;
                }

                F Offline
                F Offline
                fjdiewornncalwe
                wrote on last edited by
                #46

                The second is better than the first. At least if you're going to set the value in this ridiculous fashion, make sure that it worked. :)

                I wasn't, now I am, then I won't be anymore.

                1 Reply Last reply
                0
                • L Lost User

                  Yes, I suppose this simply comes from the old C++ days where there was no boolean type. To avoid side effects or compiler specific behavior, I always explicitly specified what I was testing. But I would write it like this:

                  if((flag1 == true) &&
                  (flag2 == true) &&
                  (flag3 == true))
                  {
                  ...
                  }

                  Usually it does not look so uniform. If I really had so many different flags, I would think about using a flag word and testing all flags in one go.

                  And from the clouds a mighty voice spoke:
                  "Smile and be happy, for it could come worse!"

                  And I smiled and was happy
                  And it came worse.

                  A Offline
                  A Offline
                  Addy Tas
                  wrote on last edited by
                  #47

                  I came from the same dark ages and indeed untill some time ago i also had the tendency to check the boolean value. Particularly the BOOL was a nasty one as you could (with good sense) only check that to FALSE. One other 'trick' i got used to apply was swapping the variable and the value E.g.

                  if(FALSE != flag)
                  {

                  Logically this seems a bit odd but then again it did protect me against typo's like:

                  if( flag = FALSE)
                  {

                  Today this will generate a compiler warning but that has not always been the case and if you have a special vendor type compiler; you may still face the same. Why check on FALSE? Simple; that was defined (as 0), any one could set the BOOL to TRUE, 1, 2 etc. Don't you love the compilers of today? Or better yet, those of tomorrow? Cheers, AT

                  Cogito ergo sum

                  1 Reply Last reply
                  0
                  • P PIEBALDconsult

                    CDP1802 wrote:

                    why not if (flag == true)?

                    Indeed, I prefer that when writing in C. One thing that drives me nuts with C is reading things like:

                    char* s = ...

                    if ( s ) ...

                    :mad:

                    A Offline
                    A Offline
                    Addy Tas
                    wrote on last edited by
                    #48

                    That in combination with the inabiliy to properly initialize a variable has ruined more than one day... Cheers, AT

                    Cogito ergo sum

                    1 Reply Last reply
                    0
                    • A alanevans

                      This stuff drives me up the wall!!!

                      bool is_queue_empty(void)
                      {
                      if (queue_length==0)
                      {
                      return true;
                      }
                      else
                      {
                      return false;
                      }
                      }

                      Or this:

                      bool counter_zero = counter==0 ? true : false;

                      Or this:

                      if (isUDPSetup()==true)
                      {
                      if ((forceSend==false))
                      {
                      ...
                      }
                      }

                      (Variable names have been changed to protect the guilty) Or this *New one*:

                      void setNeedsUpdate(bool update)
                      {
                      if ((update==true))
                      NeedsUpdate=true;
                      else
                      NeedsUpdate=false;
                      }

                      K Offline
                      K Offline
                      KP Lee
                      wrote on last edited by
                      #49

                      How about "if (!(forceSend==true))" ? :doh:

                      1 Reply Last reply
                      0
                      • M Member 3687319

                        And I think your reasoning would be wrong. It IS more clear to write if (X==true). Just because you don't like it does not mean it is not more clear, especially to junior programmers. I am the senior lead and I instruct ALL of our programmers under me to write if (X==true). It doesn't cost the compiler anything and it makes it understandable by even the junior most person quickly. It is all about proper maintenance and thinking about the coder behind you instead of just yourself. X is a variable so comparing it like another variable is both consistent and readable.

                        B Offline
                        B Offline
                        BobJanova
                        wrote on last edited by
                        #50

                        This is, at best, a matter of opinion. For me it is less clear to write if(x == true) because I have to read twice as much text to get the meaning &dnash; just as it's unclear to give a method a 300 character name. I have never met anyone who is confused by if(booleanVariable) and if they are then they shouldn't be programming until they learn the language they're using – if they have trouble reading that then do you really want them poking around your pointer code, or reflection in C#, or constructor injection frameworks, or any of the other million things any real world app has that are far more confusing? Readability is all about having a single, clear, unambiguous meaning for a statement as quickly as possible. if(x) and if(!x) are short, clear and obviously different from each other (as long as you're using a font where ! is more than 2 pixels wide, heh). if(x == true) adds nothing, is easier to mix up with closely related but different statements (if(x = true) or if(x == True) or if(x == "true") etc) and doesn't immediately show that x is a boolean or castable to one until you read the whole thing. You are on your way to becoming one of the micromanaging senior leads who appear on The Daily WTF issuing that kind of order based on your personal opinion of readability.

                        1 Reply Last reply
                        0
                        • L Lost User

                          Fine. Now what if a is a (signed) integer and has a negative value? Or what if a is a pointer which is currently NULL? Without having defined any value for TRUE or FALSE and without knowing how NULL was defined somewhere deep in the libraries, how do you now know which code will be executed and which not? Even if NULL is usually defined as 0x00, you cannot expect this to be true for every compiler. And what can happen if you use another compiler?

                          int* a = NULL;
                          int b = -42;

                          if(a)
                          {
                          // We should not need to know how NULL is defined and therefore can never know wether
                          // or not this code block will ever be entered
                          }

                          if(a == NULL)
                          {
                          // Now we explicitly compared with NULL and it is clear when this code will be executed
                          }

                          if(b)
                          {
                          // Negative values are undefined and it is up to the compiler wether a negative value is
                          // seen as 'true' or as 'false'
                          }

                          if(b < 0)
                          {
                          // Explicitly testing the variable again removes all uncertainties
                          }

                          And from the clouds a mighty voice spoke:
                          "Smile and be happy, for it could come worse!"

                          And I smiled and was happy
                          And it came worse.

                          B Offline
                          B Offline
                          BobJanova
                          wrote on last edited by
                          #51

                          Hey, I went and looked this up, the standard states explicitly that "In both forms, the first substatement is executed if the expression compares unequal to 0." http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1548.pdf[^] page 147 That is, negative numbers are defined by standard to be true for conditional expressions.

                          1 Reply Last reply
                          0
                          • F Fabio Franco

                            And it obviously makes the reader scared of what else might come from that code who's developer can't understand even a booleans...

                            "To alcohol! The cause of, and solution to, all of life's problems" - Homer Simpson

                            M Offline
                            M Offline
                            mostlyharmless1964
                            wrote on last edited by
                            #52

                            i might be showing my age here but there used to be a readability test known as "the telephone test" (from Kerningham and Plauger) - Read your code to someone over the phone. If they can't understand it, try writing the code again. admittedly this has its problems but one of the upshots was that you should just name your booleans for the thing they test and then it reads well. also, comparing to "== true" or "== false" obviously breaks this readability test :)

                            F 1 Reply Last reply
                            0
                            • M mostlyharmless1964

                              i might be showing my age here but there used to be a readability test known as "the telephone test" (from Kerningham and Plauger) - Read your code to someone over the phone. If they can't understand it, try writing the code again. admittedly this has its problems but one of the upshots was that you should just name your booleans for the thing they test and then it reads well. also, comparing to "== true" or "== false" obviously breaks this readability test :)

                              F Offline
                              F Offline
                              Fabio Franco
                              wrote on last edited by
                              #53

                              mostlyharmless1964 wrote:

                              i might be showing my age here

                              Wow, never heard of it. Was it the time when the compilers were birds inside stone cases? :laugh:

                              "To alcohol! The cause of, and solution to, all of life's problems" - Homer Simpson

                              1 Reply Last reply
                              0
                              • A alanevans

                                This stuff drives me up the wall!!!

                                bool is_queue_empty(void)
                                {
                                if (queue_length==0)
                                {
                                return true;
                                }
                                else
                                {
                                return false;
                                }
                                }

                                Or this:

                                bool counter_zero = counter==0 ? true : false;

                                Or this:

                                if (isUDPSetup()==true)
                                {
                                if ((forceSend==false))
                                {
                                ...
                                }
                                }

                                (Variable names have been changed to protect the guilty) Or this *New one*:

                                void setNeedsUpdate(bool update)
                                {
                                if ((update==true))
                                NeedsUpdate=true;
                                else
                                NeedsUpdate=false;
                                }

                                C Offline
                                C Offline
                                Clive D Pottinger
                                wrote on last edited by
                                #54

                                I fully agree. This kind of stuff makes the code SO hard to read. And so many people do this too - just look at all the examples you were able to find! Atrocious. When will people learn to put spaces around their operators !?!?! :cool:

                                Clive Pottinger Victoria, BC

                                1 Reply Last reply
                                0
                                • M Member 3687319

                                  And I think your reasoning would be wrong. It IS more clear to write if (X==true). Just because you don't like it does not mean it is not more clear, especially to junior programmers. I am the senior lead and I instruct ALL of our programmers under me to write if (X==true). It doesn't cost the compiler anything and it makes it understandable by even the junior most person quickly. It is all about proper maintenance and thinking about the coder behind you instead of just yourself. X is a variable so comparing it like another variable is both consistent and readable.

                                  R Offline
                                  R Offline
                                  RoelofDeVilliers
                                  wrote on last edited by
                                  #55

                                  Why do boolean variables exist? To store and retrieve boolean expressions (TRUTH values). They were invented SO THAT we can write code like

                                  if (X)

                                  otherwise we could just as well remove the boolean type and work with integer flags like

                                  if (X==1)

                                  This was one of the issues people had with C. No proper boolean type. But now we have a proper boolean type so don't reduce it to a "flag value" that needs to be compared to something to find the truth. It holds the truth all on its own. That's its job.

                                  1 Reply Last reply
                                  0
                                  • A alanevans

                                    This stuff drives me up the wall!!!

                                    bool is_queue_empty(void)
                                    {
                                    if (queue_length==0)
                                    {
                                    return true;
                                    }
                                    else
                                    {
                                    return false;
                                    }
                                    }

                                    Or this:

                                    bool counter_zero = counter==0 ? true : false;

                                    Or this:

                                    if (isUDPSetup()==true)
                                    {
                                    if ((forceSend==false))
                                    {
                                    ...
                                    }
                                    }

                                    (Variable names have been changed to protect the guilty) Or this *New one*:

                                    void setNeedsUpdate(bool update)
                                    {
                                    if ((update==true))
                                    NeedsUpdate=true;
                                    else
                                    NeedsUpdate=false;
                                    }

                                    J Offline
                                    J Offline
                                    John Hunley
                                    wrote on last edited by
                                    #56

                                    Just ran across this in code I've been asked to maintain (no kidding):

                                    if (((ucGlobalHeaterEnable & (1 << UC_BHOSE_HTR_ON) ) > 0) ? 1 : 0)
                                    {
                                    ...
                                    }

                                    Unbelievable!

                                    Y 1 Reply Last reply
                                    0
                                    • A alanevans

                                      This stuff drives me up the wall!!!

                                      bool is_queue_empty(void)
                                      {
                                      if (queue_length==0)
                                      {
                                      return true;
                                      }
                                      else
                                      {
                                      return false;
                                      }
                                      }

                                      Or this:

                                      bool counter_zero = counter==0 ? true : false;

                                      Or this:

                                      if (isUDPSetup()==true)
                                      {
                                      if ((forceSend==false))
                                      {
                                      ...
                                      }
                                      }

                                      (Variable names have been changed to protect the guilty) Or this *New one*:

                                      void setNeedsUpdate(bool update)
                                      {
                                      if ((update==true))
                                      NeedsUpdate=true;
                                      else
                                      NeedsUpdate=false;
                                      }

                                      J Offline
                                      J Offline
                                      Jeroen De Dauw
                                      wrote on last edited by
                                      #57

                                      A true classic. I got thought to not do this first weeks in programming class, before learning stuff like functions.

                                      Jeroen De Dauw (blog | Twitter | Identi.ca)

                                      1 Reply Last reply
                                      0
                                      • Y YvesDaoust

                                        Wouldn't this be more run-time efficient, as the value of update can be cached in a register ?

                                        void setNeedsUpdate(bool update)
                                        {
                                        if (update == true)
                                        {
                                        NeedsUpdate= update;
                                        }
                                        else
                                        {
                                        NeedsUpdate= update;
                                        }
                                        }

                                        ;->

                                        B Offline
                                        B Offline
                                        BloodyBaron
                                        wrote on last edited by
                                        #58

                                        Or more straightforward:

                                        void setNeedsUpdate(bool update)
                                        {
                                        NeedsUpdate = update;
                                        }

                                        Less confusing, and same behavior.

                                        Y 1 Reply Last reply
                                        0
                                        • B BloodyBaron

                                          Or more straightforward:

                                          void setNeedsUpdate(bool update)
                                          {
                                          NeedsUpdate = update;
                                          }

                                          Less confusing, and same behavior.

                                          Y Offline
                                          Y Offline
                                          YvesDaoust
                                          wrote on last edited by
                                          #59

                                          What about this:

                                          void setNeedsUpdate(bool update)
                                          {
                                          // Case anaylsis
                                          if (update == (update == update))
                                          {
                                          // When update is true, flag the Update as Needed
                                          NeedsUpdate= update;
                                          }
                                          else if (update == (update != update))
                                          {
                                          // Else when update is false, flag the Update as not Needed
                                          NeedsUpdate= update;
                                          }
                                          else
                                          {
                                          // Else, when update is neither true nor false, assign an arbitrary value
                                          NeedsUpdate= update;
                                          }
                                          }

                                          it has the advantage of using no constant at all and cleanly handles the case where update is not a boolean value. I have added enlightening comments.

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups