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. Today's entry for the "kill it with fire" list

Today's entry for the "kill it with fire" list

Scheduled Pinned Locked Moved The Weird and The Wonderful
c++
22 Posts 14 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.
  • G Offline
    G Offline
    Gary Wheeler
    wrote on last edited by
    #1

    bool flags[2];
    //...
    if ( (flags[0] == true) ||
    (flags[1] == true) )
    {
    //...
    if ( (flags[0] == TRUE) ||
    (flags[1] == TRUE) )
    {
    }
    }

    X| I just found this in some code I have to extend to support a new feature. I wonder if Mike Rowe[^] knows C++...

    Software Zen: delete this;

    P R R B M 11 Replies Last reply
    0
    • G Gary Wheeler

      bool flags[2];
      //...
      if ( (flags[0] == true) ||
      (flags[1] == true) )
      {
      //...
      if ( (flags[0] == TRUE) ||
      (flags[1] == TRUE) )
      {
      }
      }

      X| I just found this in some code I have to extend to support a new feature. I wonder if Mike Rowe[^] knows C++...

      Software Zen: delete this;

      P Offline
      P Offline
      phil o
      wrote on last edited by
      #2

      One can never be too sure :)

      noop()

      1 Reply Last reply
      0
      • G Gary Wheeler

        bool flags[2];
        //...
        if ( (flags[0] == true) ||
        (flags[1] == true) )
        {
        //...
        if ( (flags[0] == TRUE) ||
        (flags[1] == TRUE) )
        {
        }
        }

        X| I just found this in some code I have to extend to support a new feature. I wonder if Mike Rowe[^] knows C++...

        Software Zen: delete this;

        R Offline
        R Offline
        raddevus
        wrote on last edited by
        #3

        Check them in the outer...then checks them in the inner??? :wtf:

        Sheer Poetry

        He's checkin' his flags, he's checkin' them twice. He knows if them flags have been naughty or nice!

        G 1 Reply Last reply
        0
        • R raddevus

          Check them in the outer...then checks them in the inner??? :wtf:

          Sheer Poetry

          He's checkin' his flags, he's checkin' them twice. He knows if them flags have been naughty or nice!

          G Offline
          G Offline
          Gary Wheeler
          wrote on last edited by
          #4

          Yes, and the flags aren't changed anywhere inside the intervening 'logic'. Not only does he compare a bool to true, he also compares it to the Windows-defined TRUE as well, which is not really the same thing.

          Software Zen: delete this;

          D 1 Reply Last reply
          0
          • G Gary Wheeler

            bool flags[2];
            //...
            if ( (flags[0] == true) ||
            (flags[1] == true) )
            {
            //...
            if ( (flags[0] == TRUE) ||
            (flags[1] == TRUE) )
            {
            }
            }

            X| I just found this in some code I have to extend to support a new feature. I wonder if Mike Rowe[^] knows C++...

            Software Zen: delete this;

            R Offline
            R Offline
            Ron Anders
            wrote on last edited by
            #5

            :-D

            1 Reply Last reply
            0
            • G Gary Wheeler

              bool flags[2];
              //...
              if ( (flags[0] == true) ||
              (flags[1] == true) )
              {
              //...
              if ( (flags[0] == TRUE) ||
              (flags[1] == TRUE) )
              {
              }
              }

              X| I just found this in some code I have to extend to support a new feature. I wonder if Mike Rowe[^] knows C++...

              Software Zen: delete this;

              B Offline
              B Offline
              Bernhard Hiller
              wrote on last edited by
              #6

              Oh no, that can be not only valid, but also necessary... Can you be sure that there's not some strange #define somewhere changing the meaning of TRUE or true? And when the flags array is a member of the class (instead of locally declared in a function), its values can be changed from where ever in a different thread. Just make sure you cope with such threats. :-D

              Oh sanctissimi Wilhelmus, Theodorus, et Fredericus!

              G 1 Reply Last reply
              0
              • B Bernhard Hiller

                Oh no, that can be not only valid, but also necessary... Can you be sure that there's not some strange #define somewhere changing the meaning of TRUE or true? And when the flags array is a member of the class (instead of locally declared in a function), its values can be changed from where ever in a different thread. Just make sure you cope with such threats. :-D

                Oh sanctissimi Wilhelmus, Theodorus, et Fredericus!

                G Offline
                G Offline
                Gary Wheeler
                wrote on last edited by
                #7

                Bernhard Hiller wrote:

                #define somewhere changing the meaning of TRUE or true?

                Even more reason to break out the flamethrower... :laugh:

                Software Zen: delete this;

                1 Reply Last reply
                0
                • G Gary Wheeler

                  Yes, and the flags aren't changed anywhere inside the intervening 'logic'. Not only does he compare a bool to true, he also compares it to the Windows-defined TRUE as well, which is not really the same thing.

                  Software Zen: delete this;

                  D Offline
                  D Offline
                  DJ van Wyk
                  wrote on last edited by
                  #8

                  What if, 15 years down the line, someone inserts code between the two different checks that changes the value of TRUE? You have to make absolutely sure that it is still true when the really important piece of code hits. I suggest you add a few inline ifs so that there will never be any doubt.

                  My plan is to live forever ... so far so good

                  N 1 Reply Last reply
                  0
                  • G Gary Wheeler

                    bool flags[2];
                    //...
                    if ( (flags[0] == true) ||
                    (flags[1] == true) )
                    {
                    //...
                    if ( (flags[0] == TRUE) ||
                    (flags[1] == TRUE) )
                    {
                    }
                    }

                    X| I just found this in some code I have to extend to support a new feature. I wonder if Mike Rowe[^] knows C++...

                    Software Zen: delete this;

                    M Offline
                    M Offline
                    Member 9167057
                    wrote on last edited by
                    #9

                    Hey, I recently removed code toying with the global BoolStr variable because some table-internal-to-the-program had to use yes/no instead of true/false which as easily readable but doesn't require meddling with globals.

                    1 Reply Last reply
                    0
                    • G Gary Wheeler

                      bool flags[2];
                      //...
                      if ( (flags[0] == true) ||
                      (flags[1] == true) )
                      {
                      //...
                      if ( (flags[0] == TRUE) ||
                      (flags[1] == TRUE) )
                      {
                      }
                      }

                      X| I just found this in some code I have to extend to support a new feature. I wonder if Mike Rowe[^] knows C++...

                      Software Zen: delete this;

                      E Offline
                      E Offline
                      englebart
                      wrote on last edited by
                      #10

                      just add this code before the first if statement:

                      if (flags[0] == TRUE) flags[0] = true;
                      if (flags[1] == TRUE) flags[1] = true;
                      if (flags[0] == FALSE) flags[0] = false;
                      if (flags[1] == FALSE) flags[1] = false;

                      Then you can trust !(not), && and || for the rest of the checks. Or better yet, with 2 Booleans, there are only four possible states, so normalize and then turn it into a switch statement with 0-3.

                      if (flags\[0\] == TRUE) flags\[0\] = true;
                      if (flags\[1\] == TRUE) flags\[1\] = true;
                      if (flags\[0\] == FALSE) flags\[0\] = false;
                      if (flags\[1\] == FALSE) flags\[1\] = false;
                      int state;
                      if (flags\[0\]) 
                          state = 1
                      else 
                          state = 0;
                      if (flags\[1\]) state += 2;
                      switch (state) {case 0-3:...}
                      
                      G 1 Reply Last reply
                      0
                      • E englebart

                        just add this code before the first if statement:

                        if (flags[0] == TRUE) flags[0] = true;
                        if (flags[1] == TRUE) flags[1] = true;
                        if (flags[0] == FALSE) flags[0] = false;
                        if (flags[1] == FALSE) flags[1] = false;

                        Then you can trust !(not), && and || for the rest of the checks. Or better yet, with 2 Booleans, there are only four possible states, so normalize and then turn it into a switch statement with 0-3.

                        if (flags\[0\] == TRUE) flags\[0\] = true;
                        if (flags\[1\] == TRUE) flags\[1\] = true;
                        if (flags\[0\] == FALSE) flags\[0\] = false;
                        if (flags\[1\] == FALSE) flags\[1\] = false;
                        int state;
                        if (flags\[0\]) 
                            state = 1
                        else 
                            state = 0;
                        if (flags\[1\]) state += 2;
                        switch (state) {case 0-3:...}
                        
                        G Offline
                        G Offline
                        Gary Wheeler
                        wrote on last edited by
                        #11

                        My gastrointestinal tract just inverted itself... X|

                        Software Zen: delete this;

                        E 1 Reply Last reply
                        0
                        • G Gary Wheeler

                          bool flags[2];
                          //...
                          if ( (flags[0] == true) ||
                          (flags[1] == true) )
                          {
                          //...
                          if ( (flags[0] == TRUE) ||
                          (flags[1] == TRUE) )
                          {
                          }
                          }

                          X| I just found this in some code I have to extend to support a new feature. I wonder if Mike Rowe[^] knows C++...

                          Software Zen: delete this;

                          P Offline
                          P Offline
                          Paulo_JCG
                          wrote on last edited by
                          #12

                          Is it possible we're talking about Quantum bool ????

                          Paulo Gomes Measuring programming progress by lines of code is like measuring aircraft building progress by weight. —Bill Gates Everything should be made as simple as possible, but not simpler. —Albert Einstein

                          G 1 Reply Last reply
                          0
                          • P Paulo_JCG

                            Is it possible we're talking about Quantum bool ????

                            Paulo Gomes Measuring programming progress by lines of code is like measuring aircraft building progress by weight. —Bill Gates Everything should be made as simple as possible, but not simpler. —Albert Einstein

                            G Offline
                            G Offline
                            Gary Wheeler
                            wrote on last edited by
                            #13

                            Only this[^] knows.

                            Software Zen: delete this;

                            1 Reply Last reply
                            0
                            • G Gary Wheeler

                              bool flags[2];
                              //...
                              if ( (flags[0] == true) ||
                              (flags[1] == true) )
                              {
                              //...
                              if ( (flags[0] == TRUE) ||
                              (flags[1] == TRUE) )
                              {
                              }
                              }

                              X| I just found this in some code I have to extend to support a new feature. I wonder if Mike Rowe[^] knows C++...

                              Software Zen: delete this;

                              S Offline
                              S Offline
                              S Mercurio
                              wrote on last edited by
                              #14

                              Maybe it's an existential program looking for the meaning of truth and it couldn't get a hold of Stephen Colbert.

                              1 Reply Last reply
                              0
                              • G Gary Wheeler

                                My gastrointestinal tract just inverted itself... X|

                                Software Zen: delete this;

                                E Offline
                                E Offline
                                englebart
                                wrote on last edited by
                                #15

                                Even worse: If the //... code that initializes the flags array really assigns those values into it, then it looks like the original code does not even consider these cases: x == true && y == TRUE. 2 variables with 4 possible values each means there should be 16 possible states. Normalizing the values reduces it to 2 variables with 2 possible values which gives 4 states. If true vs TRUE produce different outcomes, then God bless you and convert the "bools" into an enum. Consider creating a multidimensional array where the indexes are your "flags" and you initialize each array element to be the correct answer for that combination of states.

                                1 Reply Last reply
                                0
                                • G Gary Wheeler

                                  bool flags[2];
                                  //...
                                  if ( (flags[0] == true) ||
                                  (flags[1] == true) )
                                  {
                                  //...
                                  if ( (flags[0] == TRUE) ||
                                  (flags[1] == TRUE) )
                                  {
                                  }
                                  }

                                  X| I just found this in some code I have to extend to support a new feature. I wonder if Mike Rowe[^] knows C++...

                                  Software Zen: delete this;

                                  D Offline
                                  D Offline
                                  DSewhuk
                                  wrote on last edited by
                                  #16

                                  might get corrupted to equal, say 42. Code that didn't explicitly check consistently for "==true" would take the wrong path. if(flags[x]) would be true, if(flags[x]==true) would be false. Then you also get the VB true, which is -1!

                                  1 Reply Last reply
                                  0
                                  • D DJ van Wyk

                                    What if, 15 years down the line, someone inserts code between the two different checks that changes the value of TRUE? You have to make absolutely sure that it is still true when the really important piece of code hits. I suggest you add a few inline ifs so that there will never be any doubt.

                                    My plan is to live forever ... so far so good

                                    N Offline
                                    N Offline
                                    Nathan Minier
                                    wrote on last edited by
                                    #17

                                    To quote my "favorite comment":

                                    #define TRUE FALSE
                                    //Happy debugging suckers

                                    "Never attribute to malice that which can be explained by stupidity." - Hanlon's Razor

                                    1 Reply Last reply
                                    0
                                    • G Gary Wheeler

                                      bool flags[2];
                                      //...
                                      if ( (flags[0] == true) ||
                                      (flags[1] == true) )
                                      {
                                      //...
                                      if ( (flags[0] == TRUE) ||
                                      (flags[1] == TRUE) )
                                      {
                                      }
                                      }

                                      X| I just found this in some code I have to extend to support a new feature. I wonder if Mike Rowe[^] knows C++...

                                      Software Zen: delete this;

                                      S Offline
                                      S Offline
                                      Super Lloyd
                                      wrote on last edited by
                                      #18

                                      what if var TRUE = false; hey!? I betcha didn't think of it, did ya?! :omg: ;P

                                      A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

                                      G 1 Reply Last reply
                                      0
                                      • S Super Lloyd

                                        what if var TRUE = false; hey!? I betcha didn't think of it, did ya?! :omg: ;P

                                        A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

                                        G Offline
                                        G Offline
                                        Gary Wheeler
                                        wrote on last edited by
                                        #19

                                        This is C++, fortunately.

                                        Software Zen: delete this;

                                        S 1 Reply Last reply
                                        0
                                        • G Gary Wheeler

                                          This is C++, fortunately.

                                          Software Zen: delete this;

                                          S Offline
                                          S Offline
                                          Super Lloyd
                                          wrote on last edited by
                                          #20

                                          well, you know #undef TRUE #define TRUE 0

                                          A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!

                                          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