Good if paid by number of lines!
-
Came across the following code during a peer code review :
if (x == 0) { y = 0; } else { y = x; }
Interesting, isn't it! Asad
-
Came across the following code during a peer code review :
if (x == 0) { y = 0; } else { y = x; }
Interesting, isn't it! Asad
Very prosaic. P.S. And only counts as one statement anyway. :-D
modified on Monday, December 29, 2008 2:07 PM
-
Came across the following code during a peer code review :
if (x == 0) { y = 0; } else { y = x; }
Interesting, isn't it! Asad
Interesting. Not a word that comes to mind when reading that.
-
Interesting. Not a word that comes to mind when reading that.
this seems to be one of the most common horrors' posted here and also one of the most common I come across...
-
Came across the following code during a peer code review :
if (x == 0) { y = 0; } else { y = x; }
Interesting, isn't it! Asad
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!
-
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!
-
Came across the following code during a peer code review :
if (x == 0) { y = 0; } else { y = x; }
Interesting, isn't it! Asad
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).
-
Came across the following code during a peer code review :
if (x == 0) { y = 0; } else { y = x; }
Interesting, isn't it! Asad
-
this seems to be one of the most common horrors' posted here and also one of the most common I come across...
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...
-
I always do ... switch (x) { case 0 : y = 0; break; case 1 : y = 1; break; case 2 : y = 2; break; ... ... } ... for Int32 :-)
-
I always do ... switch (x) { case 0 : y = 0; break; case 1 : y = 1; break; case 2 : y = 2; break; ... ... } ... for Int32 :-)
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