Special Case
-
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
:-D
public class SysAdmin : Employee
{public override void DoWork(IWorkItem workItem) { if (workItem.User.Type == UserType.NoLearn){ throw new NoIWillNotFixYourComputerException(new Luser(workItem.User)); }else{ base.DoWork(workItem); } }
}
-
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
Hey now, maybe it was originally written with constants or enumerations. :~
-
Hey now, maybe it was originally written with constants or enumerations. :~
-
Just a quick one, but amused me when I saw it in code today.
if (i == 0)
return 1;
else
return i + 1; -
Just a quick one, but amused me when I saw it in code today.
if (i == 0)
return 1;
else
return i + 1; -
Just a quick one, but amused me when I saw it in code today.
if (i == 0)
return 1;
else
return i + 1;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 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)
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
-
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
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 ??? Maybereturn 1 + abs(i) * ((i < 0) ? - 1 : ((i == 0) ? 0 : ((i > 0) ? + 1 : abort(), 0)));
-
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 ??? Maybereturn 1 + abs(i) * ((i < 0) ? - 1 : ((i == 0) ? 0 : ((i > 0) ? + 1 : abort(), 0)));
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
-
Just a quick one, but amused me when I saw it in code today.
if (i == 0)
return 1;
else
return i + 1;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+++
-
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
Sure.
return -~i;
-
Just a quick one, but amused me when I saw it in code today.
if (i == 0)
return 1;
else
return i + 1;Guess the author is afraid of "AddWithZeroException" :laugh: :laugh: :laugh:
Regards Vallarasu S | FSharpMe.blogspot.com
-
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+++
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:
-
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 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)
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); -
Just a quick one, but amused me when I saw it in code today.
if (i == 0)
return 1;
else
return i + 1;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.
-
Function will return
sqrt(2) + 1
-
Function will return
sqrt(2) + 1
-
SquareRootException + 1
-
Just a quick one, but amused me when I saw it in code today.
if (i == 0)
return 1;
else
return i + 1;