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 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.
  • 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

    A Offline
    A Offline
    AspDotNetDev
    wrote on last edited by
    #2

    Depends on the language and implementation. Perhaps the operators are implemented differently (if they are overloaded). For example, sometimes it makes sense to compare two butterflies for equality, but you can't say one is greater than another. Or maybe only one operator was overloaded, but another wasn't (a poor practice in itself). That being said, whoever wrote that was probably just being a doofus. :)

    [Forum Guidelines]

    P 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

      P Offline
      P Offline
      Peter_in_2780
      wrote on last edited by
      #3

      When I see code like you quoted, I go looking for things like

      unsigned short value;

      just in case the comparison *might* be intentionally smart. [OT, but you can see the thought process] Java's lack of unsigned integer types is: (a) a PITA (b) a lifesaver (c) both (d) none of the above. Discuss.

      Software rusts. Simon Stephenson, ca 1994.

      P L 2 Replies Last reply
      0
      • P Peter_in_2780

        When I see code like you quoted, I go looking for things like

        unsigned short value;

        just in case the comparison *might* be intentionally smart. [OT, but you can see the thought process] Java's lack of unsigned integer types is: (a) a PITA (b) a lifesaver (c) both (d) none of the above. Discuss.

        Software rusts. Simon Stephenson, ca 1994.

        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #4

        e) A symptom of scripting languages.

        1 Reply Last reply
        0
        • A AspDotNetDev

          Depends on the language and implementation. Perhaps the operators are implemented differently (if they are overloaded). For example, sometimes it makes sense to compare two butterflies for equality, but you can't say one is greater than another. Or maybe only one operator was overloaded, but another wasn't (a poor practice in itself). That being said, whoever wrote that was probably just being a doofus. :)

          [Forum Guidelines]

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

          To put in context - it's a C# int.

          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

            X Offline
            X Offline
            Xiangyang Liu
            wrote on last edited by
            #6

            Pete O'Hanlon wrote:

            if (!(value > 0)){ throw new ArgumentOutOfRangeException("....");} Could we not do this? if (value <= 0)

            There could be a perfectly reasonable explanation: The code maybe like the following previously if (!isValid(value)){ throw new ArgumentOutOfRangeException("....");} Then someone decided to simplify it by replacing isValid(value) with (value>0).

            My Younger Son & His "PET"

            P 1 Reply Last reply
            0
            • X Xiangyang Liu

              Pete O'Hanlon wrote:

              if (!(value > 0)){ throw new ArgumentOutOfRangeException("....");} Could we not do this? if (value <= 0)

              There could be a perfectly reasonable explanation: The code maybe like the following previously if (!isValid(value)){ throw new ArgumentOutOfRangeException("....");} Then someone decided to simplify it by replacing isValid(value) with (value>0).

              My Younger Son & His "PET"

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

              Sadly not. I know the person who wrote this - he's not that big on any form of refactoring.

              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 Peter_in_2780

                When I see code like you quoted, I go looking for things like

                unsigned short value;

                just in case the comparison *might* be intentionally smart. [OT, but you can see the thought process] Java's lack of unsigned integer types is: (a) a PITA (b) a lifesaver (c) both (d) none of the above. Discuss.

                Software rusts. Simon Stephenson, ca 1994.

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

                Unsignedness would not excuse the author (value == 0), floats may have (NaN would behave differently)

                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

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

                  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.

                  B T J 3 Replies 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.

                    B Offline
                    B Offline
                    BillW33
                    wrote on last edited by
                    #10

                    That is the most likely explanation.

                    Just because the code works, it doesn't mean that it is good code.

                    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
                      supercat9
                      wrote on last edited by
                      #11

                      It may be that the person is using a general pattern of testing whether required input conditions are not met by throwing on their inverse. When preconditions are more complicated, it can sometimes be cleaner say something like:

                      /* Either foo or bar must be true */
                      if (!(foo || bar))
                      throw new InvalidArgumentException("Neither foo nor bar was valid");

                      Than to say:

                      /* Either foo or bar must be true */
                      if (!foo && !bar)
                      throw new InvalidArgumentException("Neither foo nor bar was valid");

                      If one habitually writes pre-checks based upon preconditions, code will probably read better if one consistently uses the same style. If there will be any need to test preconditions using throw-if-not-met logic, it may be best to do so consistently. Also, btw, if one is going to be pasting code into something like a web-based forum, it may be helpful to write conditions so as to avoid the less-than sign. My usual rewrite of a less-than-zero condition for web posting would typically be "0 > whatever" rather than "!(whatever >= 0)", but some people may prefer the latter style.

                      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

                        N Offline
                        N Offline
                        normanS
                        wrote on last edited by
                        #12

                        Of course you could use your form:

                        if (value <= 0)

                        But if values greater than zero are OK, and anything else should give an exception, then why not write it in the original form:

                        if (!(value > 0))
                        {
                        throw new ArgumentOutOfRangeException("....");
                        }

                        This could be considered a more direct translation of the requirement, and therefore more understandable. Of course, it may just have been a lack of thought, but I'd argue there are times when it would be beneficial not to simplify expressions, particularly when you have a compiler that will reduce it to the same form for you so there is no performance penalty.

                        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
                          Super Lloyd
                          wrote on last edited by
                          #13

                          I can top it off! :laugh:

                          static void Main(string[] args)
                          {
                          var b = true;
                          if (!!!!!!!!b != false)
                          Console.WriteLine("hu?");
                          else
                          Console.WriteLine("ha!");
                          }

                          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.

                          T 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.

                            T Offline
                            T Offline
                            Thomas Vanderhoof
                            wrote on last edited by
                            #14

                            I think this is the case also. Every once in a while (once in a blue moon), I do it too. Not that I'm lazy...I just had a logical bug, decided to see if the opposite would work, it did work, so I move to the next problem thinking I'll come back and fix it later.

                            1 Reply Last reply
                            0
                            • S Super Lloyd

                              I can top it off! :laugh:

                              static void Main(string[] args)
                              {
                              var b = true;
                              if (!!!!!!!!b != false)
                              Console.WriteLine("hu?");
                              else
                              Console.WriteLine("ha!");
                              }

                              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.

                              T Offline
                              T Offline
                              Thomas Vanderhoof
                              wrote on last edited by
                              #15

                              "hu?" is printed. :)

                              S 1 Reply Last reply
                              0
                              • T Thomas Vanderhoof

                                "hu?" is printed. :)

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

                                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 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

                                  J Offline
                                  J Offline
                                  Jeff Connelly
                                  wrote on last edited by
                                  #17

                                  I wouldn't really call that a "gem".

                                  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
                                    KChandos
                                    wrote on last edited by
                                    #18

                                    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 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

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

                                      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

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

                                      P 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

                                        F Offline
                                        F Offline
                                        FrankLaPiana
                                        wrote on last edited by
                                        #20

                                        In Visual C++ MFC, there's a CString. And to check if a CString has values, it's "if (!str.IsEmpty()). So the QA person, who had a degree from Stevens Institute of Technology, sat there and argued with me for 2 HOURS, and brought it all the way up to the VP, because it wasn't the "obvious way to do the logical operation." Didn't matter at all that is was the way Microsoft implemented it, and we had to use it.

                                        1 Reply Last reply
                                        0
                                        • 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
                                          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