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. May be bad code or May not be!!!

May be bad code or May not be!!!

Scheduled Pinned Locked Moved The Weird and The Wonderful
help
28 Posts 18 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.
  • R Ravi Sant

    Just saw this code at work:

    if( A==B )
    {
    if( B==C && A!=C)
    {
    DoABC();
    }
    else
    {
    DoWork();
    }
    }
    else if (C==A || C == B)
    {
    DoWork();
    }

    I have examined, tested over and over, but code never goes or will go to DoABC();

    // ♫ 99 little bugs in the code, // 99 bugs in the code // We fix a bug, compile it again // 101 little bugs in the code ♫

    G Offline
    G Offline
    gumi_r msn com
    wrote on last edited by
    #4

    In any "sane" scenario its pretty obvious that if A==B and B==C then A should equal C therefore DoABC() will never be called. Still with no more information, it is plausible (even if hideous) to think that DoABC could be called. It just needs some dubious implicit cast operators to do the trick.

    1 Reply Last reply
    0
    • R Ravi Sant

      Just saw this code at work:

      if( A==B )
      {
      if( B==C && A!=C)
      {
      DoABC();
      }
      else
      {
      DoWork();
      }
      }
      else if (C==A || C == B)
      {
      DoWork();
      }

      I have examined, tested over and over, but code never goes or will go to DoABC();

      // ♫ 99 little bugs in the code, // 99 bugs in the code // We fix a bug, compile it again // 101 little bugs in the code ♫

      M Offline
      M Offline
      musefan
      wrote on last edited by
      #5

      I assume the data types for A, B and C are something basic? With custom operator overloads you could get to DoABC() using the above logic if you really wanted to. But I think you would need to have different data types

      I may or may not be responsible for my own actions

      R R 2 Replies Last reply
      0
      • M musefan

        I assume the data types for A, B and C are something basic? With custom operator overloads you could get to DoABC() using the above logic if you really wanted to. But I think you would need to have different data types

        I may or may not be responsible for my own actions

        R Offline
        R Offline
        RobCroll
        wrote on last edited by
        #6

        And then you could post a Hall Of Shame regarding implementing IComparable :-O

        "You get that on the big jobs."

        1 Reply Last reply
        0
        • D Daniel Scott

          If equality is transitive (and I really hope it is), it can indeed never execute DoABC. Given A == B and B == C, it follows from transitivity that A == C, so A != C must be false.

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #7

          Under normal circumstances equality is transitive; but it also isn't permanent... :)

          Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

          Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

          L 1 Reply Last reply
          0
          • R Ravi Sant

            Just saw this code at work:

            if( A==B )
            {
            if( B==C && A!=C)
            {
            DoABC();
            }
            else
            {
            DoWork();
            }
            }
            else if (C==A || C == B)
            {
            DoWork();
            }

            I have examined, tested over and over, but code never goes or will go to DoABC();

            // ♫ 99 little bugs in the code, // 99 bugs in the code // We fix a bug, compile it again // 101 little bugs in the code ♫

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #8

            You didn't tell us what types A, B, C are. They could be some type with the == operator overloaded by something counter-intuitive. They could also be just stupid integers, stored globally, marked volatile, and changing occasionally... So, yes it looks weird, it probably is a mistake, and OTOH it could function as intended and just be a case of bad, hardly readable, code. :)

            Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

            Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

            R 1 Reply Last reply
            0
            • R Ravi Sant

              Just saw this code at work:

              if( A==B )
              {
              if( B==C && A!=C)
              {
              DoABC();
              }
              else
              {
              DoWork();
              }
              }
              else if (C==A || C == B)
              {
              DoWork();
              }

              I have examined, tested over and over, but code never goes or will go to DoABC();

              // ♫ 99 little bugs in the code, // 99 bugs in the code // We fix a bug, compile it again // 101 little bugs in the code ♫

              N Offline
              N Offline
              Narf the Mouse
              wrote on last edited by
              #9

              My Snark Detector is going off.

              1 Reply Last reply
              0
              • L Luc Pattyn

                You didn't tell us what types A, B, C are. They could be some type with the == operator overloaded by something counter-intuitive. They could also be just stupid integers, stored globally, marked volatile, and changing occasionally... So, yes it looks weird, it probably is a mistake, and OTOH it could function as intended and just be a case of bad, hardly readable, code. :)

                Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                R Offline
                R Offline
                Ravi Sant
                wrote on last edited by
                #10

                they are string(s) :)

                // ♫ 99 little bugs in the code, // 99 bugs in the code // We fix a bug, compile it again // 101 little bugs in the code ♫

                M 1 Reply Last reply
                0
                • M musefan

                  I assume the data types for A, B and C are something basic? With custom operator overloads you could get to DoABC() using the above logic if you really wanted to. But I think you would need to have different data types

                  I may or may not be responsible for my own actions

                  R Offline
                  R Offline
                  Ravi Sant
                  wrote on last edited by
                  #11

                  Yes they are string(s) and now i thing to replace this code with just one line DoWork();

                  // ♫ 99 little bugs in the code, // 99 bugs in the code // We fix a bug, compile it again // 101 little bugs in the code ♫

                  Richard DeemingR 1 Reply Last reply
                  0
                  • R Ravi Sant

                    they are string(s) :)

                    // ♫ 99 little bugs in the code, // 99 bugs in the code // We fix a bug, compile it again // 101 little bugs in the code ♫

                    M Offline
                    M Offline
                    mahendren
                    wrote on last edited by
                    #12

                    Your tag line is classic :) 101 little bugs in the code ♫

                    R 1 Reply Last reply
                    0
                    • M mahendren

                      Your tag line is classic :) 101 little bugs in the code ♫

                      R Offline
                      R Offline
                      Ravi Sant
                      wrote on last edited by
                      #13

                      ♫ Thanks ♫

                      // ♫ 99 little bugs in the code, // 99 bugs in the code // We fix a bug, compile it again // 101 little bugs in the code ♫

                      1 Reply Last reply
                      0
                      • R Ravi Sant

                        Just saw this code at work:

                        if( A==B )
                        {
                        if( B==C && A!=C)
                        {
                        DoABC();
                        }
                        else
                        {
                        DoWork();
                        }
                        }
                        else if (C==A || C == B)
                        {
                        DoWork();
                        }

                        I have examined, tested over and over, but code never goes or will go to DoABC();

                        // ♫ 99 little bugs in the code, // 99 bugs in the code // We fix a bug, compile it again // 101 little bugs in the code ♫

                        R Offline
                        R Offline
                        R Erasmus
                        wrote on last edited by
                        #14

                        No matter how hard I try to find the good in this piece of code, I just can't, I'm sorry. 1st bug: meaningless variable names 2nd bug: No '()' to indicate and clarify precedence 3rd bug: Dead code

                        "Program testing can be used to show the presence of bugs, but never to show their absence." << please vote!! >>

                        R 1 Reply Last reply
                        0
                        • R R Erasmus

                          No matter how hard I try to find the good in this piece of code, I just can't, I'm sorry. 1st bug: meaningless variable names 2nd bug: No '()' to indicate and clarify precedence 3rd bug: Dead code

                          "Program testing can be used to show the presence of bugs, but never to show their absence." << please vote!! >>

                          R Offline
                          R Offline
                          Ravi Sant
                          wrote on last edited by
                          #15

                          yes, 1st is not bug, I did it purposely to hide actual busines variables. 2& 3 surely bad.

                          // ♫ 99 little bugs in the code, // 99 bugs in the code // We fix a bug, compile it again // 101 little bugs in the code ♫

                          R 1 Reply Last reply
                          0
                          • R Ravi Sant

                            yes, 1st is not bug, I did it purposely to hide actual busines variables. 2& 3 surely bad.

                            // ♫ 99 little bugs in the code, // 99 bugs in the code // We fix a bug, compile it again // 101 little bugs in the code ♫

                            R Offline
                            R Offline
                            R Erasmus
                            wrote on last edited by
                            #16

                            I guest that much. ;-)

                            "Program testing can be used to show the presence of bugs, but never to show their absence." << please vote!! >>

                            R D 2 Replies Last reply
                            0
                            • R R Erasmus

                              I guest that much. ;-)

                              "Program testing can be used to show the presence of bugs, but never to show their absence." << please vote!! >>

                              R Offline
                              R Offline
                              Ravi Sant
                              wrote on last edited by
                              #17

                              Thanks :)

                              // ♫ 99 little bugs in the code, // 99 bugs in the code // We fix a bug, compile it again // 101 little bugs in the code ♫

                              1 Reply Last reply
                              0
                              • R Ravi Sant

                                Just saw this code at work:

                                if( A==B )
                                {
                                if( B==C && A!=C)
                                {
                                DoABC();
                                }
                                else
                                {
                                DoWork();
                                }
                                }
                                else if (C==A || C == B)
                                {
                                DoWork();
                                }

                                I have examined, tested over and over, but code never goes or will go to DoABC();

                                // ♫ 99 little bugs in the code, // 99 bugs in the code // We fix a bug, compile it again // 101 little bugs in the code ♫

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

                                Looks like a test to see if operator==() has been implemented correctly for whatever class the variables A, B, and C are instances of. DoABC() will only be called, if operator==() does not fulfil transitivity. Of course, there is one error: it should be !A==C instead of A!=C, otherwise we cannot be sure there is an error in operator!=(). ;) Yeah, right... ;P

                                R 1 Reply Last reply
                                0
                                • S Stefan_Lang

                                  Looks like a test to see if operator==() has been implemented correctly for whatever class the variables A, B, and C are instances of. DoABC() will only be called, if operator==() does not fulfil transitivity. Of course, there is one error: it should be !A==C instead of A!=C, otherwise we cannot be sure there is an error in operator!=(). ;) Yeah, right... ;P

                                  R Offline
                                  R Offline
                                  Ravi Sant
                                  wrote on last edited by
                                  #19

                                  lol. Have 5 for the humor :laugh: :laugh:

                                  // ♫ 99 little bugs in the code, // 99 bugs in the code // We fix a bug, compile it again // 101 little bugs in the code ♫

                                  1 Reply Last reply
                                  0
                                  • R Ravi Sant

                                    Just saw this code at work:

                                    if( A==B )
                                    {
                                    if( B==C && A!=C)
                                    {
                                    DoABC();
                                    }
                                    else
                                    {
                                    DoWork();
                                    }
                                    }
                                    else if (C==A || C == B)
                                    {
                                    DoWork();
                                    }

                                    I have examined, tested over and over, but code never goes or will go to DoABC();

                                    // ♫ 99 little bugs in the code, // 99 bugs in the code // We fix a bug, compile it again // 101 little bugs in the code ♫

                                    W Offline
                                    W Offline
                                    whiteclouds
                                    wrote on last edited by
                                    #20

                                    Funny! I think if you want this code work as your meaning, you should override the operator "==", or you convey it into other language such as C#.

                                    There is some white cloud floating on the blue sky. That's the landscape I like.

                                    1 Reply Last reply
                                    0
                                    • R Ravi Sant

                                      Yes they are string(s) and now i thing to replace this code with just one line DoWork();

                                      // ♫ 99 little bugs in the code, // 99 bugs in the code // We fix a bug, compile it again // 101 little bugs in the code ♫

                                      Richard DeemingR Offline
                                      Richard DeemingR Offline
                                      Richard Deeming
                                      wrote on last edited by
                                      #21

                                      Ravi Sant wrote:

                                      replace this code with just one line DoWork();

                                      But what if A != B && A != C && B != C? The original code won't execute DoWork in that case.


                                      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                                      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                                      R 1 Reply Last reply
                                      0
                                      • Richard DeemingR Richard Deeming

                                        Ravi Sant wrote:

                                        replace this code with just one line DoWork();

                                        But what if A != B && A != C && B != C? The original code won't execute DoWork in that case.


                                        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                                        R Offline
                                        R Offline
                                        Ravi Sant
                                        wrote on last edited by
                                        #22

                                        Good Point .. :)

                                        // ♫ 99 little bugs in the code, // 99 bugs in the code // We fix a bug, compile it again // 101 little bugs in the code ♫

                                        1 Reply Last reply
                                        0
                                        • R Ravi Sant

                                          Just saw this code at work:

                                          if( A==B )
                                          {
                                          if( B==C && A!=C)
                                          {
                                          DoABC();
                                          }
                                          else
                                          {
                                          DoWork();
                                          }
                                          }
                                          else if (C==A || C == B)
                                          {
                                          DoWork();
                                          }

                                          I have examined, tested over and over, but code never goes or will go to DoABC();

                                          // ♫ 99 little bugs in the code, // 99 bugs in the code // We fix a bug, compile it again // 101 little bugs in the code ♫

                                          K Offline
                                          K Offline
                                          Kent K
                                          wrote on last edited by
                                          #23

                                          The only minute possibility of it being useful perhaps, is if there are multiple threads involved. . .and maybe if it is the case that A is static or something. . . .that if there was a CPU context switch between the lines if( A==B ) and if( B==C && A!=C) ...where A gets changed by another thread therefore the programmer had been trying to be uber careful about running DoABC(). . . . .??

                                          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