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. It's not the most obvious piece of logic.

It's not the most obvious piece of logic.

Scheduled Pinned Locked Moved The Weird and The Wonderful
phprubycomtoolsquestion
30 Posts 21 Posters 1 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.
  • K KChandos

    Another alternative that I didn't see mentioned in the replies. Was this code hand written or produced by a generator? I've been playing with the CodeDOM and there are some structures that you can define that would likely generate exactly that code.

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

    Hand written, sadly.

    I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be

    Forgive your enemies - it messes with their heads

    My blog | My articles | MoXAML PowerToys | Onyx

    1 Reply Last reply
    0
    • Richard DeemingR Richard Deeming

      It doesn't make much sense for an int, but it might make sense for an int? (Nullable<Int32>). The ordering operators (<, <=, >, >=) on Nullable<T> will always return false if either operand is null, so !(value > 0) would be equivalent to value == null || value <= 0.


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

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

      It's an int, nothing but the int, so help me glod.

      I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be

      Forgive your enemies - it messes with their heads

      My blog | My articles | MoXAML PowerToys | Onyx

      1 Reply Last reply
      0
      • P Pete OHanlon

        So, today I came across this gem:

        // If the value is NOT greater than zero, throw an exception.
        if (!(value > 0))
        {
        throw new ArgumentOutOfRangeException("....");
        }

        Could we not do this?

        if (value <= 0)

        :rolleyes:

        I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be

        Forgive your enemies - it messes with their heads

        My blog | My articles | MoXAML PowerToys | Onyx

        K Offline
        K Offline
        Keith Barrow
        wrote on last edited by
        #23

        Hopefully you'll be gratified to learn I just used this as an example to one of my students, introducing him to refactoring poor code and [specifically] bad if statements.

        Sort of a cross between Lawrence of Arabia and Dilbert.[^]

        P K 2 Replies Last reply
        0
        • K Keith Barrow

          Hopefully you'll be gratified to learn I just used this as an example to one of my students, introducing him to refactoring poor code and [specifically] bad if statements.

          Sort of a cross between Lawrence of Arabia and Dilbert.[^]

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

          If it stops anybody else making the same mistake, then I'm glad to help.

          I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be

          Forgive your enemies - it messes with their heads

          My blog | My articles | MoXAML PowerToys | Onyx

          1 Reply Last reply
          0
          • P Pete OHanlon

            So, today I came across this gem:

            // If the value is NOT greater than zero, throw an exception.
            if (!(value > 0))
            {
            throw new ArgumentOutOfRangeException("....");
            }

            Could we not do this?

            if (value <= 0)

            :rolleyes:

            I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be

            Forgive your enemies - it messes with their heads

            My blog | My articles | MoXAML PowerToys | Onyx

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

            Your alternate solution is just as valid. To me the original is perfectly readable, but I'd prefer: if (value > 0) {//do something useful} else (throw ...); Since you have CDO, this is probably too much reading to do before you get to the else statement. :)

            1 Reply Last reply
            0
            • P Pete OHanlon

              So, today I came across this gem:

              // If the value is NOT greater than zero, throw an exception.
              if (!(value > 0))
              {
              throw new ArgumentOutOfRangeException("....");
              }

              Could we not do this?

              if (value <= 0)

              :rolleyes:

              I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be

              Forgive your enemies - it messes with their heads

              My blog | My articles | MoXAML PowerToys | Onyx

              D Offline
              D Offline
              Dalek Dave
              wrote on last edited by
              #26

              It is accurate, but inelegant!

              ------------------------------------ I will never again mention that I was the poster of the One Millionth Lounge Post, nor that it was complete drivel. Dalek Dave CCC League Table Link CCC Link[^]

              1 Reply Last reply
              0
              • K Keith Barrow

                Hopefully you'll be gratified to learn I just used this as an example to one of my students, introducing him to refactoring poor code and [specifically] bad if statements.

                Sort of a cross between Lawrence of Arabia and Dilbert.[^]

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

                Ahhh, teaching him a lifetime skill huh? I hope most of us, who read bad code go: Is it working correctly? if no, write a bug and hope someone assigns you ownership so you can fix it. Hope it's approved, and whoever is assigned the bug takes the time to fix it. else: Is this egregiously bad, causing performance issues or something else that might cause severe system problems? if yes, write a bug etc. else: Do you work in a best practice shop and this violates a best p? if yes, write a bug etc. else: Do you work in a best practice shop if yes, suggest a new best practice reffering to your source and hope it is adopted and the source is addressed. else: ask yourself if it is worth suggesting to your manager one more thing that could be improved. if yes, wow, you have a manager who listens to you and acts on your suggestions, or you are working for a new manager, or you are a perrenial optimist. else: Join the regular ranks, shake your head about the code you see, and go on with your life. Maybe I have a gift for seeing bad logic. It took me three days to convince someone this was a bug: if (current_thread_count <= maximum_thread_count + thread_count_to_add) add_new_threads(thread_count_to_add); It took asking for the specification document because I was told this met specifications. I agreed that this exactly met specifications, the only problem is, that specifications aren't asking for what is intended. Huh, what? It took going to the lap, finding out what values they were configured for (maximum_thread_count=120, thread_count_to_add=25) and showing what that would do. (Say your current count is 119, if you add 25 more, the current count would go to 144. You don't want that to happen, right? "Right." Well, 119 is less than 145... "How did you come up with 145" Well, the first value is 120, the second value is 25, added together they are 145. Well, the light dawns, but we continue the exercise proving that the count would go up to 169 because the loop is immediate when if the statement was true and the request is relatively immediate. In theory the count could go to 170. Anyway, if there is a heavy enough load the service machines should blow up with a thread allocation error. The lab guy who gave us the numbers, pipes up "Oh, yea, we have that problem all the time!" I'm thinking it would have been nice to know that the problem existed instead of just reading C# code to get up to speed on what the group was doing while waiting for my next assignment. Also, how can the specs get

                1 Reply Last reply
                0
                • F fjdiewornncalwe

                  It might just be a case of lazy programming. I've seen code where if statements were initially written in the opposite state of what was intended and then instead of fixing the whole statement, the ! was simply thrown in front.

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

                  J Offline
                  J Offline
                  James Lonero
                  wrote on last edited by
                  #28

                  Sounds like it was written by a lawyer.

                  1 Reply Last reply
                  0
                  • P Pete OHanlon

                    So, today I came across this gem:

                    // If the value is NOT greater than zero, throw an exception.
                    if (!(value > 0))
                    {
                    throw new ArgumentOutOfRangeException("....");
                    }

                    Could we not do this?

                    if (value <= 0)

                    :rolleyes:

                    I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be

                    Forgive your enemies - it messes with their heads

                    My blog | My articles | MoXAML PowerToys | Onyx

                    S Offline
                    S Offline
                    Stephen Hewitt
                    wrote on last edited by
                    #29

                    Firstly, with most languages both examples will produce the same code, so there is no need to prefer one from a performance standpoint. So the choice comes down to clarity and depending on the specific situation (and perhaps the individual) either one could be preferable. The first example has the advantage that it more directly reflects the comment. I don't really regard this as a coding horror.

                    Steve

                    1 Reply Last reply
                    0
                    • S Super Lloyd

                      Indeed! :)

                      A train station is where the train stops. A bus station is where the bus stops. On my desk, I have a work station.... _________________________________________________________ My programs never have bugs, they just develop random features.

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #30

                      I use less ! on my messenger :laugh:

                      - Bits and Bytes Rules! 10(jk)

                      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