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. Special Case

Special Case

Scheduled Pinned Locked Moved The Weird and The Wonderful
37 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.
  • L Lost User

    And let those who have never failed to see the obvious throw the first stone :)

    At least artificial intelligence already is superior to natural stupidity

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

    Hey now, maybe it was originally written with constants or enumerations. :~

    S 1 Reply Last reply
    0
    • P PIEBALDconsult

      Hey now, maybe it was originally written with constants or enumerations. :~

      S Offline
      S Offline
      Sentenryu
      wrote on last edited by
      #6

      yeah, sure, increment a enumeration...

      I'm brazilian and english (well, human languages in general) aren't my best skill, so, sorry by my english. (if you want we can speak in C# or VB.Net =p)

      1 Reply Last reply
      0
      • R Rotted Frog

        Just a quick one, but amused me when I saw it in code today.

        if (i == 0)
        return 1;
        else
        return i + 1;

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

        That is funny++.

        "You get that on the big jobs."

        1 Reply Last reply
        0
        • R Rotted Frog

          Just a quick one, but amused me when I saw it in code today.

          if (i == 0)
          return 1;
          else
          return i + 1;

          H Offline
          H Offline
          hollysong
          wrote on last edited by
          #8

          Interesting :) But i can not imagine the reason of the code. Can you give the full scope of code? Thinking but nothing find about the code :)

          1 Reply Last reply
          0
          • R Rotted Frog

            Just a quick one, but amused me when I saw it in code today.

            if (i == 0)
            return 1;
            else
            return i + 1;

            Y Offline
            Y Offline
            YvesDaoust
            wrote on last edited by
            #9

            A nice piece indeed. Here is a slightly more defensive version that makes sure the sign is properly handled:

            if (i < 0)
            return 1 - abs(i);
            else if (i == 0)
            return 1;
            else if (i > 0)
            return 1 + abs(i);

            (with the added benefit that out-of-range values are left unchanged)

            A A S 3 Replies Last reply
            0
            • Y YvesDaoust

              A nice piece indeed. Here is a slightly more defensive version that makes sure the sign is properly handled:

              if (i < 0)
              return 1 - abs(i);
              else if (i == 0)
              return 1;
              else if (i > 0)
              return 1 + abs(i);

              (with the added benefit that out-of-range values are left unchanged)

              A Offline
              A Offline
              Andrei Straut
              wrote on last edited by
              #10

              return (i < 0) ? (1 - abs(i)) : ((i == 0) ? 1 : 1 + abs(i));

              Here. Shorter now, and less obvious to spot. The benefits of multiple ternaries :-D :-D

              Y 1 Reply Last reply
              0
              • A Andrei Straut

                return (i < 0) ? (1 - abs(i)) : ((i == 0) ? 1 : 1 + abs(i));

                Here. Shorter now, and less obvious to spot. The benefits of multiple ternaries :-D :-D

                Y Offline
                Y Offline
                YvesDaoust
                wrote on last edited by
                #11

                Right. This allows us to move the common constant in front and factor out the abs call:

                return 1 + abs(i) * ((i < 0) ? - 1 : ((i == 0) ? 0 : + 1));

                But how do we make the i > 0 case explicit ??? Maybe

                return 1 + abs(i) * ((i < 0) ? - 1 : ((i == 0) ? 0 : ((i > 0) ? + 1 : abort(), 0)));

                A 1 Reply Last reply
                0
                • Y YvesDaoust

                  Right. This allows us to move the common constant in front and factor out the abs call:

                  return 1 + abs(i) * ((i < 0) ? - 1 : ((i == 0) ? 0 : + 1));

                  But how do we make the i > 0 case explicit ??? Maybe

                  return 1 + abs(i) * ((i < 0) ? - 1 : ((i == 0) ? 0 : ((i > 0) ? + 1 : abort(), 0)));

                  A Offline
                  A Offline
                  Andrei Straut
                  wrote on last edited by
                  #12

                  And even better, we can abstract away the "1", who knows, maybe its value will change somewhere in the future:

                  final int _CONST = 1;
                  return _CONST + abs(i) * ((i < 0) ? - _CONST : ((i == 0) ? 0 : ((i > 0) ? + _CONST : abort(), 0)));

                  Can I have that mind bleach now, please? :-D

                  Y 1 Reply Last reply
                  0
                  • R Rotted Frog

                    Just a quick one, but amused me when I saw it in code today.

                    if (i == 0)
                    return 1;
                    else
                    return i + 1;

                    A Offline
                    A Offline
                    AshenFlowersFalling
                    wrote on last edited by
                    #13

                    Came across this piece of solid-gold coding in Android the other day, in good old SurfaceFlinger.cpp:

                    if (mCurrentState.orientation != orientation) {
                    if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
                    mCurrentState.orientationType = flags;
                    mCurrentState.orientation = orientation;
                    setTransactionFlags(eTransactionNeeded);
                    mTransactionCV.wait(mStateLock);
                    } else {
                    orientation = BAD_VALUE;
                    }
                    }

                    Sometimes I just don't know what to think any more. :D

                    +++DIVIDE BY CUCUMBER ERROR+++

                    A 1 Reply Last reply
                    0
                    • A Andrei Straut

                      And even better, we can abstract away the "1", who knows, maybe its value will change somewhere in the future:

                      final int _CONST = 1;
                      return _CONST + abs(i) * ((i < 0) ? - _CONST : ((i == 0) ? 0 : ((i > 0) ? + _CONST : abort(), 0)));

                      Can I have that mind bleach now, please? :-D

                      Y Offline
                      Y Offline
                      YvesDaoust
                      wrote on last edited by
                      #14

                      Sure.

                      return -~i;

                      1 Reply Last reply
                      0
                      • R Rotted Frog

                        Just a quick one, but amused me when I saw it in code today.

                        if (i == 0)
                        return 1;
                        else
                        return i + 1;

                        V Offline
                        V Offline
                        VallarasuS
                        wrote on last edited by
                        #15

                        Guess the author is afraid of "AddWithZeroException" :laugh: :laugh: :laugh:

                        Regards Vallarasu S | FSharpMe.blogspot.com

                        1 Reply Last reply
                        0
                        • A AshenFlowersFalling

                          Came across this piece of solid-gold coding in Android the other day, in good old SurfaceFlinger.cpp:

                          if (mCurrentState.orientation != orientation) {
                          if (uint32_t(orientation)<=eOrientation270 || orientation==42) {
                          mCurrentState.orientationType = flags;
                          mCurrentState.orientation = orientation;
                          setTransactionFlags(eTransactionNeeded);
                          mTransactionCV.wait(mStateLock);
                          } else {
                          orientation = BAD_VALUE;
                          }
                          }

                          Sometimes I just don't know what to think any more. :D

                          +++DIVIDE BY CUCUMBER ERROR+++

                          A Offline
                          A Offline
                          Andrei Straut
                          wrote on last edited by
                          #16

                          Well, just hold your phone at 42 degrees :-D. And also, there were worse f'ups: (Steve Jobs "Don't hold it that way", anyone?) Actually, there were none :confused:

                          1 Reply Last reply
                          0
                          • Y YvesDaoust

                            A nice piece indeed. Here is a slightly more defensive version that makes sure the sign is properly handled:

                            if (i < 0)
                            return 1 - abs(i);
                            else if (i == 0)
                            return 1;
                            else if (i > 0)
                            return 1 + abs(i);

                            (with the added benefit that out-of-range values are left unchanged)

                            A Offline
                            A Offline
                            ASkoro
                            wrote on last edited by
                            #17

                            Hey but what about i being sqrt(2)???

                            Y 1 Reply Last reply
                            0
                            • Y YvesDaoust

                              A nice piece indeed. Here is a slightly more defensive version that makes sure the sign is properly handled:

                              if (i < 0)
                              return 1 - abs(i);
                              else if (i == 0)
                              return 1;
                              else if (i > 0)
                              return 1 + abs(i);

                              (with the added benefit that out-of-range values are left unchanged)

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

                              I just tried your method, and my compiler is generating a error about a method must return a value, so I fixed it. There is a version without bugs, hope it helps:

                              if (i < 0)
                              return 1 - abs(i);
                              else if (i == 0)
                              return 1;
                              else if (i > 0)
                              return 1 + abs(i);

                              K 1 Reply Last reply
                              0
                              • R Rotted Frog

                                Just a quick one, but amused me when I saw it in code today.

                                if (i == 0)
                                return 1;
                                else
                                return i + 1;

                                S Offline
                                S Offline
                                sergiogarcianinja
                                wrote on last edited by
                                #19

                                I a very humble opinion, I think the original developer cared about performance. There is a big and ugly monster living in or closes that will eat us if we write less performing code. The problem is, that almost all developers don't understand about performance and do wrong things. Here, I think he/she are trying to avoid a sum using a comparison. In some cases, like division, it will be a great code.

                                1 Reply Last reply
                                0
                                • A ASkoro

                                  Hey but what about i being sqrt(2)???

                                  Y Offline
                                  Y Offline
                                  YvesDaoust
                                  wrote on last edited by
                                  #20

                                  Function will return sqrt(2) + 1

                                  A 1 Reply Last reply
                                  0
                                  • Y YvesDaoust

                                    Function will return sqrt(2) + 1

                                    A Offline
                                    A Offline
                                    ASkoro
                                    wrote on last edited by
                                    #21

                                    And for sqrt(-2)????

                                    Y M K 3 Replies Last reply
                                    0
                                    • A ASkoro

                                      And for sqrt(-2)????

                                      Y Offline
                                      Y Offline
                                      YvesDaoust
                                      wrote on last edited by
                                      #22

                                      SquareRootException + 1

                                      1 Reply Last reply
                                      0
                                      • R Rotted Frog

                                        Just a quick one, but amused me when I saw it in code today.

                                        if (i == 0)
                                        return 1;
                                        else
                                        return i + 1;

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

                                        For the sake of learning here, why do some of the examples use the abs function in their answers. Why not just i++?

                                        T P K B 4 Replies Last reply
                                        0
                                        • L Lost User

                                          For the sake of learning here, why do some of the examples use the abs function in their answers. Why not just i++?

                                          T Offline
                                          T Offline
                                          thoiness
                                          wrote on last edited by
                                          #24

                                          Rewritten:

                                          return (i == 0) ? 1 : i++;

                                          In division, specifically in the denominator, this code eliminates the divide by zero issue. I think the OP (original programmer) had good intentions.

                                          O 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