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. Variation of bad boolean check.

Variation of bad boolean check.

Scheduled Pinned Locked Moved The Weird and The Wonderful
18 Posts 10 Posters 36 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.
  • P Pete OHanlon

    While it may be correct, it could also have been written:

    bool isValue = (instance.Variable == 1);
    

    . The true/false at the end is redundant.

    Deja View - the feeling that you've seen this post before.

    D Offline
    D Offline
    David Crow
    wrote on last edited by
    #9

    Pete O`Hanlon wrote:

    . The true/false at the end is redundant.

    But hardly "Hall of Shame" worthy.


    "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

    "Judge not by the eye but by the heart." - Native American Proverb

    P H 2 Replies Last reply
    0
    • D David Crow

      Pete O`Hanlon wrote:

      . The true/false at the end is redundant.

      But hardly "Hall of Shame" worthy.


      "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

      "Judge not by the eye but by the heart." - Native American Proverb

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #10

      It is when there are about 30 of these tests in the same routine.

      Deja View - the feeling that you've seen this post before.

      1 Reply Last reply
      0
      • P Pete OHanlon

        OK - I'm just QAing some code, and I've come across the following (variable names changed to protect the innocent):

        bool isValue = (instance.Variable == 1 ? true : false);

        <Ironic>I can only assume that this is to protect from that other non-nullable boolean condition of "maybe(ish)".</Ironic>

        Deja View - the feeling that you've seen this post before.

        K Offline
        K Offline
        kakan
        wrote on last edited by
        #11

        Pete O`Hanlon wrote:

        bool isValue = (instance.Variable == 1 ? true : false);

        AFAIK, there is just one single value (for a 'bool') that's really defined, and that is the value for false (0). True is defined as 'not false', so it can be any value, except for 0. In Win32, there are cases where 'true' can be either 1 or -1. An example: TRUE is 1 VARIANT_TRUE is -1 So IMO, the code would be useful if it looked like this:

        bool isValue = (instance.Variable == 0 ? false : true);

        Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson

        J 1 Reply Last reply
        0
        • K kakan

          Pete O`Hanlon wrote:

          bool isValue = (instance.Variable == 1 ? true : false);

          AFAIK, there is just one single value (for a 'bool') that's really defined, and that is the value for false (0). True is defined as 'not false', so it can be any value, except for 0. In Win32, there are cases where 'true' can be either 1 or -1. An example: TRUE is 1 VARIANT_TRUE is -1 So IMO, the code would be useful if it looked like this:

          bool isValue = (instance.Variable == 0 ? false : true);

          Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson

          J Offline
          J Offline
          jhwurmbach
          wrote on last edited by
          #12

          kakan wrote:

          bool isValue = (instance.Variable == 0 ? false : true);

          Better still:

          bool isValue = (instance.Variable != 0);

          To me this is clearer.


          Failure is not an option - it's built right in.

          K 1 Reply Last reply
          0
          • J jhwurmbach

            kakan wrote:

            bool isValue = (instance.Variable == 0 ? false : true);

            Better still:

            bool isValue = (instance.Variable != 0);

            To me this is clearer.


            Failure is not an option - it's built right in.

            K Offline
            K Offline
            kakan
            wrote on last edited by
            #13

            I would do it the same way you do. I had two reasons for responding the way I did: 1. I wanted the smallest possible alteration to the original code. 2. In the past, I have given "smartcoded" examples and has been flamed for it, with comments like "unredable" and "impossible to understand". (My response that the compiler could handle my source code yielded heaps of "1" votes.) So now I try to keep my code examples as easy to understand as possible.

            Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson

            P 1 Reply Last reply
            0
            • K kakan

              I would do it the same way you do. I had two reasons for responding the way I did: 1. I wanted the smallest possible alteration to the original code. 2. In the past, I have given "smartcoded" examples and has been flamed for it, with comments like "unredable" and "impossible to understand". (My response that the compiler could handle my source code yielded heaps of "1" votes.) So now I try to keep my code examples as easy to understand as possible.

              Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson

              P Offline
              P Offline
              Pete OHanlon
              wrote on last edited by
              #14

              I actually posted this because I didn't see the need for the true/false test. The result of the equality test is a boolean condition, so either the item is equal to 1 (true) or it isn't (false). It just seemed that the coder had put in unnecessary code because he thought it was clever. What made it worse, was that there were about 30 of these tests in one routine. Argggh.

              Deja View - the feeling that you've seen this post before.

              K 1 Reply Last reply
              0
              • P Pete OHanlon

                I actually posted this because I didn't see the need for the true/false test. The result of the equality test is a boolean condition, so either the item is equal to 1 (true) or it isn't (false). It just seemed that the coder had put in unnecessary code because he thought it was clever. What made it worse, was that there were about 30 of these tests in one routine. Argggh.

                Deja View - the feeling that you've seen this post before.

                K Offline
                K Offline
                kakan
                wrote on last edited by
                #15

                Yes, under normal circumstances, it's a total waste, I agree. The reason why I responded in the first place was that I once spent quite some time to figure out why a function (that turned out to want a VARIANT_BOOL) didn't react when I sent a (BOOL) TRUE to it. Then I learned the difference between (BOOL) TRUE (1) and (VARIANT_BOOL) TRUE (-1). And after that, I always test for false or not false. It can't go wrong. All versions of false *is* 0 and nothing else.

                Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson

                1 Reply Last reply
                0
                • P Pete OHanlon

                  OK - I'm just QAing some code, and I've come across the following (variable names changed to protect the innocent):

                  bool isValue = (instance.Variable == 1 ? true : false);

                  <Ironic>I can only assume that this is to protect from that other non-nullable boolean condition of "maybe(ish)".</Ironic>

                  Deja View - the feeling that you've seen this post before.

                  E Offline
                  E Offline
                  Emilio Garavaglia
                  wrote on last edited by
                  #16

                  Well, may be it's not the best way, but there could be good resons: 1) instance.Variable is a generic class you don't know the type, but you kow it supports bool operator==(int). Assuming that an implicit conversion to bool will do the same is not obvious. (And such a conversion may be even not existent) 2) if instance.Variable is an int, assigning an int to a bool gives a warning. That code eliminates it 2a) But there is a int-to-bool native converter: the !! pseudo-oprerator

                  bool isValue(!!instance.Variable);

                  2 bugs found. > recompile ... 65534 bugs found. :doh:

                  P 1 Reply Last reply
                  0
                  • E Emilio Garavaglia

                    Well, may be it's not the best way, but there could be good resons: 1) instance.Variable is a generic class you don't know the type, but you kow it supports bool operator==(int). Assuming that an implicit conversion to bool will do the same is not obvious. (And such a conversion may be even not existent) 2) if instance.Variable is an int, assigning an int to a bool gives a warning. That code eliminates it 2a) But there is a int-to-bool native converter: the !! pseudo-oprerator

                    bool isValue(!!instance.Variable);

                    2 bugs found. > recompile ... 65534 bugs found. :doh:

                    P Offline
                    P Offline
                    Pete OHanlon
                    wrote on last edited by
                    #17

                    OK. Perhaps I'm being a bit too subtle here.

                    bool isVariable = (instance.Variable == 1);
                    

                    That's it. That's all you need to do. The check instance.Variable == 1 will only return true or false so there's no need to the ? true : false check. They are redundant. As I've stated before - this method did about 30 of these checks. The comparison is redundant in each case.

                    Deja View - the feeling that you've seen this post before.

                    1 Reply Last reply
                    0
                    • D David Crow

                      Pete O`Hanlon wrote:

                      . The true/false at the end is redundant.

                      But hardly "Hall of Shame" worthy.


                      "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

                      "Judge not by the eye but by the heart." - Native American Proverb

                      H Offline
                      H Offline
                      Hal Angseesing
                      wrote on last edited by
                      #18

                      I have to disagree. Amongst beginners tertiary operators are notoriously hard to read. Anybody who adds them superflously does deserve the hall of shame.

                      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