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

                P Offline
                P Offline
                patbob
                wrote on last edited by
                #25

                Why not use i++? Obfuscation. The original coder was trying to obfuscate it by using an if statement, so people are running with that theme :)

                We can program with only 1's, but if all you've got are zeros, you've got nothing.

                1 Reply Last reply
                0
                • A ASkoro

                  And for sqrt(-2)????

                  M Offline
                  M Offline
                  Member 4608898
                  wrote on last edited by
                  #26

                  And for sqrt(-1/64) do we get indigestion tablets?

                  1 Reply Last reply
                  0
                  • T thoiness

                    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 Offline
                    O Offline
                    Oscar0
                    wrote on last edited by
                    #27

                    Bug Alert. I think you meant perhaps: return (i == 0) ? 1 : ++i;

                    1 Reply Last reply
                    0
                    • A ASkoro

                      And for sqrt(-2)????

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

                      ASkoro wrote:

                      And for sqrt(-2)????

                      Computers ignore complexity. Or is that irrationality? I'm almost sure that's a complex number. If that is true, what is an irrational number? I know they both exist, but can't definitively define them.

                      1 Reply Last reply
                      0
                      • S sergiogarcianinja

                        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 Offline
                        K Offline
                        KP Lee
                        wrote on last edited by
                        #29

                        Sorry, I fail to see how you fixed it. Computers aren't very good at determining there is an unreachable path. Put an unconditional return 1 - abs(i) + abs(i); after all the if statements should fix it. (Especially if i is uint. Checking for negative numbers is really interesting in that case.)

                        1 Reply 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++?

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

                          MehGerbil wrote:

                          Why not just i++?

                          For one thing that would be the same as returning i. (Unless the i was passed with ref. Then you get two values for the price of one.)

                          MehGerbil wrote:

                          For the sake of learning here

                          That's rich. Trying to learn better coding by studying poor code harder.

                          L 1 Reply Last reply
                          0
                          • K KP Lee

                            MehGerbil wrote:

                            Why not just i++?

                            For one thing that would be the same as returning i. (Unless the i was passed with ref. Then you get two values for the price of one.)

                            MehGerbil wrote:

                            For the sake of learning here

                            That's rich. Trying to learn better coding by studying poor code harder.

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

                            The intent was to write: return i++;

                            KP Lee wrote:

                            That's rich. Trying to learn better coding by studying poor code harder.

                            I think fixing bad code is a great way to learn, especially if you learn the "why" along the way.

                            1 Reply 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++?

                              B Offline
                              B Offline
                              BobJanova
                              wrote on last edited by
                              #32

                              It should be 'return i + 1'. ++i is a wasteful update of the variable i, assuming it's local (there's some serious issues if it isn't anyway), and i++ is just wrong because it returns i (before the statement) and not i + 1.

                              L 1 Reply Last reply
                              0
                              • B BobJanova

                                It should be 'return i + 1'. ++i is a wasteful update of the variable i, assuming it's local (there's some serious issues if it isn't anyway), and i++ is just wrong because it returns i (before the statement) and not i + 1.

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

                                That was interesting. Why is it that ++i is less efficient then returning i + 1? Isn't a calculation made (total of i + 1) made in memory somewhere regardless?

                                R 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
                                  Vitaly Tomilov
                                  wrote on last edited by
                                  #34

                                  I'm not surprised, see it all the time. This happens as a result of changing the condition, i.e. the code was written for one condition, then the condition changed, and the code was updated without logical refactoring. Also, some developers like making code temporarily unreachable rather than commenting it out, i.e. putting the code into a block like:

                                  if(false){...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;

                                    A Offline
                                    A Offline
                                    Albert Holguin
                                    wrote on last edited by
                                    #35

                                    Developer took special math classes... :-D

                                    1 Reply Last reply
                                    0
                                    • L Lost User

                                      That was interesting. Why is it that ++i is less efficient then returning i + 1? Isn't a calculation made (total of i + 1) made in memory somewhere regardless?

                                      R Offline
                                      R Offline
                                      Rotted Frog
                                      wrote on last edited by
                                      #36

                                      It's likely that compiler optimisations make it irrelevant in this case. But in the general case, she using ++i the value is copied into i after being calculated, then copied into the return variable. Using i+1 only copies it to the return variable (one less copy).

                                      L 1 Reply Last reply
                                      0
                                      • R Rotted Frog

                                        It's likely that compiler optimisations make it irrelevant in this case. But in the general case, she using ++i the value is copied into i after being calculated, then copied into the return variable. Using i+1 only copies it to the return variable (one less copy).

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

                                        Interesting. Thank you for the informative response.

                                        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