- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
Anyone else notice Gilroy in the image attached to the article linked at the beginning?
Admin
What language is this in?
Perhaps a loosely-typed language (PHP?), where test could be sth. other than true/false/file_not_found? Then there might be a fraction of sense in these lines...
Admin
Probably C# or Java...
Admin
Addendum: Since some of those tests can be proved to fail or pass (e.g. if you've tested a condition to be true, then an immediate test for false can never succeed) would a decent compiler could remove some of the redundant logic....unless the code was multithreaded allowing the value of test to switch values between the first and subsequent conditional. Maybe that's what the original coder was intending; the code is double-checking the return condition in a multithreaded program and the author never heard about atomicity.
Admin
The language could support properties, as such the blah.variable could actually be a method call. And who knows what that method call is doing. Maybe it only returns the correct response every second execution?
Admin
ED: Fixed!
Admin
Actually, a true shortened form would be
if (test == false) return false; return true;
Admin
I vote for "the programmer is just an idiot."
Admin
Admin
You would have to return !test and then update the calling code to reverse the logic.
Admin
Assuming that "test" could hold a third value (like 'null'), then false returns false, and true and anything else returns true. Of course, it would help to know the language and what type of variable "test" is...
This logic is not covered by "return test".
Admin
At least the operator== is not overloaded. Is it? And test is of type boolean?
Admin
A reasonably tidy way of resolving possibly-true-or-false things to a nice binary is return !!test;
Admin
Heh, if it's not boolean you can use my favourite bool cast operator: return !!test;
Admin
Admin
Well, the code depends on what type 'test' is. If test was an instance of this:
class T { public: bool operator == (bool rhs) { bool result = rand () < (RAND_MAX >> 1); return result; } };
then you'd get really random true / false values because calling rand more than once increases the randomality.
Skizz
Admin
Admin
This reminds me of a similar chunk of code I found when looking through one of our projects
Var = Var ? Var : True
Admin
I just tried this in C# with test just a regular bool and ReSharper simplified it to this:
The suggestions were:
Too bad it didn't refactor completely to return test; but at least you know then what it does :P
Admin
You guys are missing the REAL reason. The original programmer is an evil alien trying to drive all human programmers insane thus making the Earth ripe for invasion by intelligent computer viruses.
BWAHAHAHAAHAHAHAHA
Admin
Admin
Never ask for permission to change bad code.
Admin
Hmm, but what if test is a property in the current object, whose get function is stateful?
Admin
It gets worse... Apparently the poor guy also started into the heart of the Great Codethulhu with his eyes or something.
I kind of agree... The guy you're asking may have been it's author :pAdmin
Admin
There is no way someone could have come up with that code by being a bad programmer. A vindictive one perhaps...
Admin
This is not PHP (uses $ sign for variables!)
So if the first evaluation (test == true) is true it will commence the second evaluation ((test == false) ? false : true) and return true.
But that is redundant since we already evaluated test to be true
If the first evaluation is false, then it does the third evaluation (test == false) ? false : true) and return false.
IMHO this double test is redundant even if its a non bool variable! I would just do
return (test != null) ? test : false;
Admin
I prefer: return test != 0;
It generates better code on a lot of compilers, and its intent is clearer. You'd have to be a fool to "optimize" it to just return test, where as a !! could easily be accidently "optimized".
Admin
Let me guess - the original programmer was either extremely bored and did this for the kicks of it, or he did it for "job security" obfuscation reasons :)
Admin
If test's type has evil operator overloadings, the most you can reduce this to is:
test == true; return ((test == false) ? false : true);
That is, assuming the language doesn't allow for ?: overloading.
Admin
Maybe the programmer tried to avoid to accidentally return FILE_NOT_FOUND. But on the other hand, is there any case where you might want to avoid FILE_NOT_FOUND to be returned? I don't think so.
Admin
The BCFH - Bastard coder from Hell?
Admin
Admin
That's why an explicit cast is clearer than either "!!" or "!=0"; it makes clear that you aren't really interested in doing a test or comparison, you're making sure that what you have really is a bool.
[*] - after folding, of course.
Admin
Assuming that test is a nullable bool you could use the null coalescing operator "??", which returns the value to the left if it is not null, and the value to the right if it is.
return test ?? false;
Admin
Does Randy ask his lead's permission every time he changes a line of code? Sounds inefficient.
Admin
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.
Admin
Well, likely they decided it's better to be divided by truth than by lies.
Admin
define('test', rand(0,1));
Admin
What is it with this crew and booleans?
Admin
Sooooooooo true. I dump junk code on a regular basis when I find it. Unfortunately, other 'developers' I know of aren't so enlightned.
Admin
A friend was writing some subsystem in PHP some time ago. Thing is the subsystem could cause many different faults which should be handled differently, and the system above it would launch only one error handler and only if the subsystem returned 'false' (and it wasn't permitted to change the system, only the error handler).
So my friend designed an ingenious method of encoding error codes = integers inside 'false'.
array(false) = 0 array(array()) = 1 array(array(array())) = 2 array(array(array(array())))) = 3 ... array(array(),false) = 10 ... array(array(array()),array()) = 21
The error would be dispatched because they all cast to bool return false, but he could extract what was returned from the procedure, identify the error code by the structure shape and react properly.
We thought, what about encoding ASCII error messages that way.
Admin
Aw, wotta wimp! He asked permission to change it, fer cryin' out loud... you're a maintenance developer, man! Here's the definition of "maintain": "To keep in an appropriate condition, operation, or force; keep unimpaired." All right, it's impaired; fix it! That's your job. Does a maintenance engineer (formerly "janitor") ask permission to mop up a spill in the hallway?
Admin
I encounter it every day. As well as stuff like this:
if (string == null || "".equalsIgnoreCase(string)) { return false; } else { return true; }
Good, though, that in our company you do not have to ask a manager before making the world a better place.
Admin
XIU,
WTF? You need a tool to tell you that
is equivalent to any logical obfuscation that returns 'true' if the boolean is true and 'false' if the boolean is false?...and your tool couldn't even do it! Language doesn't really matter.
Admin
No, there still wouldn't be. If there were other values that test could contain, the if statement should have been testing for them as well.
You can't justify this - it's a WTF.
Admin
Nope. You still can't justify it. It's a WTF.
Of course, it's no surprise that the "code" (using the term loosely) author didn't know about short-circuit boolean evaluation.
Admin
It is only because people don't understand the true nature of a boolean.
Booleans are only ever bi-valued, true or false. Boolean variables can have the possibility of having a null value. Boolean variables with a null value should be safely assumed to be either true or false. If a boolean variable with a null value can not be safely assumed true or false, you don't have a boolean, but a tri-state enumerator, so don't confuse things by attempting to handle it with a bi-state representation.
Addendum (2008-04-04 10:14): Oh and the joke illustrates that without saying this specifically.
Admin
If the compiler you're using in this day and age still requires that you do these kind of optimizations, it's time you got out of the '80s. The 21st century is waiting for you.
Admin
http://www.jetbrains.net/jira/browse/RSRP-63747