|
|
|
| Non-WTF Job: Software Developer at Rustici Software (Franklin, Tennessee) |
| « Tech Support Heck | Jan-48 » |
A few years back, Randy A took a contract as a maintenance developer on a wretched abomination of an application. Like those who've stared into the heart of the Great Codethulhu, Randy's retinas are forever burned with code from the system. One line that continues to haunt his dreams is as clear as the day he first encountered it...
return (test == true)? ( (test == false)? false : true) : ((test == false) ? false : true);
When he first stumbled across it in a treacherous debugging session, he resorted to a truth table to figure out what exactly that line did...
return test;
It might not have been as bad, except for the fact that, minutes later, he stumbled across a slightly altered version of it again. And, minutes later, another one. All told, the same perverted logic was repeated nine different times in that module.
He pleaded with his lead to simply replace the code with "return test". But, like so many others who feared tempting the beast, Randy's boss replied "I'm not sure why it's in there, and it's probably best we keep it in there."
|
I just tried this in C# with test just a regular bool and ReSharper simplified it to this:
return test ? (true) : (false); The suggestions were: - Comparison with true is redundant - Expression is always false - Expression is always true - Code is unreachable Too bad it didn't refactor completely to return test; but at least you know then what it does :P |
|
They finally posted it...
To answer some questions in the comments: The language was Java test was nothing more than a boolean Supposedly it was written when the author was learning Java And yes I've since learned not to 'ask' about changing bad code. |
Re: The Test of Truth
2008-04-04 10:54
•
by
Binks
(unregistered)
|
|
Oh boy...that's just messed...
So basically we first test if the variable is true. If the variable is true we then test if the variable is false. If the variable is both true and false we return false, otherwise, if the variable is both true and true, we return true. If the variable is false we then test if the variable is false. If the variable is both false and false we return false. If the variable is false and not false we return true. There are two parts of that line which are unreachable in standard boolean logic. For the first false to be reached test has to show as true for the first == and false for the second. For the second true test has to show as false for the first == and true for the second. I am in awe of the messed-up-ness of this code. Not only is it unnecessarily complex, but half of it is unreachable and the other half performs the same test twice (making sure?). It's a trifecta of bad code design! |
|
Back in my COBOL days I came across a program with three GOTO statements in a row, all to the same place. I asked the original programmer why he did that, and he replied that he wanted to make sure it really went there. Oh. I guess he only wrote one GOTO when he didn't really care if it went there or not.
|
| « Tech Support Heck | Jan-48 » |