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. Good if paid by number of lines!

Good if paid by number of lines!

Scheduled Pinned Locked Moved The Weird and The Wonderful
code-review
11 Posts 11 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.
  • A Offline
    A Offline
    Asad Abbasi
    wrote on last edited by
    #1

    Came across the following code during a peer code review :

    if (x == 0)
    {
        y = 0;
    }
    else
    {
        y = x;
    }
    

    Interesting, isn't it! Asad

    P H L S R 5 Replies Last reply
    0
    • A Asad Abbasi

      Came across the following code during a peer code review :

      if (x == 0)
      {
          y = 0;
      }
      else
      {
          y = x;
      }
      

      Interesting, isn't it! Asad

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

      Very prosaic. P.S. And only counts as one statement anyway. :-D

      modified on Monday, December 29, 2008 2:07 PM

      1 Reply Last reply
      0
      • A Asad Abbasi

        Came across the following code during a peer code review :

        if (x == 0)
        {
            y = 0;
        }
        else
        {
            y = x;
        }
        

        Interesting, isn't it! Asad

        H Offline
        H Offline
        hammerstein05
        wrote on last edited by
        #3

        Interesting. Not a word that comes to mind when reading that.

        S 1 Reply Last reply
        0
        • H hammerstein05

          Interesting. Not a word that comes to mind when reading that.

          S Offline
          S Offline
          singh iz king
          wrote on last edited by
          #4

          this seems to be one of the most common horrors' posted here and also one of the most common I come across...

          M 1 Reply Last reply
          0
          • A Asad Abbasi

            Came across the following code during a peer code review :

            if (x == 0)
            {
                y = 0;
            }
            else
            {
                y = x;
            }
            

            Interesting, isn't it! Asad

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

            don't waste unnecessary assignment cycles, especially when you get payed by the line:

            if (x == 0)
            {
            if (y != 0)
            {
            y = 0;
            }
            }
            else
            {
            if (y != x)
            {
            y = x;
            }
            }

            :)

            Luc Pattyn [Forum Guidelines] [My Articles]


            Love, happiness and fewer bugs for 2009!


            L 1 Reply Last reply
            0
            • L Luc Pattyn

              don't waste unnecessary assignment cycles, especially when you get payed by the line:

              if (x == 0)
              {
              if (y != 0)
              {
              y = 0;
              }
              }
              else
              {
              if (y != x)
              {
              y = x;
              }
              }

              :)

              Luc Pattyn [Forum Guidelines] [My Articles]


              Love, happiness and fewer bugs for 2009!


              L Offline
              L Offline
              LarryDM
              wrote on last edited by
              #6

              There are some seriously twisted people on here. :rolleyes:

              Larry Miller

              1 Reply Last reply
              0
              • A Asad Abbasi

                Came across the following code during a peer code review :

                if (x == 0)
                {
                    y = 0;
                }
                else
                {
                    y = x;
                }
                

                Interesting, isn't it! Asad

                S Offline
                S Offline
                supercat9
                wrote on last edited by
                #7

                If x and y are simple variables of matching type, the code is pretty nonsensical. There are some situations, however, where such code could save a tiny bit of time, and others where its behavior could be slightly different from 'y=x'. For example of the former situation, suppose that 'x' is an integer and 'y' is a double, and 99.9% of the time 'x' will equal zero. If one were to code 'y=x' the computer would always perform a library call to do the integer-to-double promotion. Even if the library routine did an early-exit check for a value of zero, calling the library in the first place would still add overhead which the above code would eliminate. As for the second situation, if 'x' and 'y' are of a numeric type where two variables may both test equal to zero and yet not have identical representations (true of some floating-point implementations), code like the above may be useful to ensure that only one form of zero is used. For example, consider the following, on an implementation that features positive and negative zero, and positive and negative infinity:

                {
                double x1,x2,z1,z2;

                x1= 1e-30/1e30; /* x1 is positive zero */
                x2=-1e-30/1e30; /* x2 is negative zero */
                z1=1/x1; /* z1 is positive infinity */
                z2=1/x2; /* z2 is negative infinity */
                }

                Note that x1==0 and x2==0, but (1/x1)!=(1/x2). Forcing x1 and x2 to the same zero would ensure that (1/x1)==(1/x2).

                1 Reply Last reply
                0
                • A Asad Abbasi

                  Came across the following code during a peer code review :

                  if (x == 0)
                  {
                      y = 0;
                  }
                  else
                  {
                      y = x;
                  }
                  

                  Interesting, isn't it! Asad

                  R Offline
                  R Offline
                  Rauhotz
                  wrote on last edited by
                  #8

                  I always do ... switch (x) { case 0 : y = 0; break; case 1 : y = 1; break; case 2 : y = 2; break; ... ... } ... for Int32 :-)

                  L D 2 Replies Last reply
                  0
                  • S singh iz king

                    this seems to be one of the most common horrors' posted here and also one of the most common I come across...

                    M Offline
                    M Offline
                    Megidolaon
                    wrote on last edited by
                    #9

                    Yeah, most so calld horrors are just verbose code that wouldn't even get optimized for speed by writing it in less lines. I mean it's fine to write in fewer ines but this isn't actually wrong...

                    1 Reply Last reply
                    0
                    • R Rauhotz

                      I always do ... switch (x) { case 0 : y = 0; break; case 1 : y = 1; break; case 2 : y = 2; break; ... ... } ... for Int32 :-)

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

                      u r jerk

                      Ravie Busie Coding is my birth-right and bugs are part of feature my code has!

                      1 Reply Last reply
                      0
                      • R Rauhotz

                        I always do ... switch (x) { case 0 : y = 0; break; case 1 : y = 1; break; case 2 : y = 2; break; ... ... } ... for Int32 :-)

                        D Offline
                        D Offline
                        Divyang Mithaiwala
                        wrote on last edited by
                        #11

                        Rauhotz wrote:

                        I always do ... switch (x) { case 0 : y = 0; break; case 1 : y = 1; break; case 2 : y = 2; break; ... ... }

                        At last you forgot to write default case.

                        default: y = x;


                        Do not trust a computer... Always check what computer is doing regards, Divyang Mithaiwala Software Engineer

                        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