- 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
The first one has some utility in being able to set breakpoints....
note; If you don't like the classic ternary operation how about "??" and "?." [I believe they all have their place]
Admin
If only there were methods for asking if a list was empty…
Admin
You know that pattern where basic string operations like atoi get reinvented in the form of a switch statement? Yeah, "fix" it by replacing every conditional with ?:. Bonus points for automating this process!
Admin
The first one definitely has value - if you need to change the boolean expression on the test you only have to do it in one place, rather than 46...
Admin
Sorry, but not enough info to check the WTF level of this. For example what if this is a method in a class that wraps the license object.
Admin
Are you actually kidding? The
>
expression will return a boolean, so you don't need a ternary. I can't think of a single language in which this is not the case.Admin
The problem isn't the existenc of hasFeatures(). It's that the whole thing could be rewritten in one line.
For clarity write:
boolean hasFeatures() { return (license.getFeatures().size() > 0); }
For brevity:
boolean hasFeatures() { return (license.getFeatures().size()); }
Admin
Or preferably
return license.getFeatures().isEmpty();
Admin
Or if you want it to be correct
return ! license.getFeatures().isEmpty();
Admin
I proudly abuse the hell out of ternaries. I admit they're not the most readable things, but they're still better than an entire eight line if-else construction for simple things.
Admin
No, you fools! You can't use ternaries here! It's wrong.
boolean hasFeatures() { Boolean returnValue = new Boolean(); try { if (license.getFeatures().isEmpty().equals ((new Boolean (Boolean.FALSE)).getValue())) { returnValue = Boolean.TRUE; endif; if (! license.getFeatures().isEmpty().equals ((new Boolean (Boolean.FALSE)).getValue())) { returnValue = Boolean.FALSE; endif; } catch {Exception e) { returnValue = null; } return returnValue; }
Brillant!
Admin
The boolean stuff I see at times boggles my mind...
If bInProduction = False Then ''do stuff Else ''do other stuff End If
Or my all time favourite I saw recently:
If dto.Order.IsNotNothing() And dto.OrderId.IsNotNothing() And dto.Order > 0 And dto.OrderId > 0 Then ''do stuff End If
Note that 'Order' and 'OrderId' are of type Integer IsNotNothing is a .net extension method that wraps around the single line of code: Return obj IsNot Nothing They literally created a function so as to not need to put a space between IsNot and Nothing... ??? And the ultimate best part, Order will only ever be the values 0 or 1!
Admin
The first one has plenty of value - in fact, it can be more WTF to not wrap complex conditionals in a method (or property, or extension method depending on your language).
I think situations for #1 OR #2 alone are enough to warrant a function representing conditional logic.
The only thing WTF about this code is the way they are writing their conditional logic and returning the result. Which is pretty damn WTF tbf.
Admin
Exactly. This is how coding should be done. It's also much more self commenting to do if(user.isActive())) than if(user.getExpireDate() < time())
The fact that it's simple today shouldn't be the point.
Addendum 2016-06-01 09:56: On second thought though I think they just mean the
if(boolean){ return true;} else {return(false);}
thing
as opposed to just
return boolean;
Admin
There are 10 kinds of people in the world: those who understand how boolean expressions work, and those who don't.
Also, the bonus of abusing ternary expressions is that they can have surprising order of precedence issues!
And there should be a special circle of hell for people who always put parens in their return statements, even constants like return(0). IT'S NOT A FUNCTION YOU BLOODY MORONS STOP WRITING IT LIKE ONE.
Admin
I guess someday I'm going to that special circle in hell. Either that or "I dunno LOL ¯(°_o)/¯" is going to have an aneurysm and nobody will care about it again.
Admin
It's about time that we've introduced affirmative action for code. Anyone have a list of the least used commands? I think we should use every command equally.
Admin
Sumireko, are you advocating wider use of goto? Bringing that one back should be fun.
Admin
Just to be extra-nerdy, if you install R and install the 'sos' package, there's a ??? operator
Admin
Could be a bit worse. A few years back someone wrote a ray-tracer for the IOCC. With no Ifs, or for or while loops, just ternaries and recursion. Urp.
Admin
History lesson: The ? : operator(s) comes from Algol 60, which one could use 'if' then' 'else' in the same way (inside an expression. One could say: a := if b < c then d else e; While needless use of the ?: operator(s) is a crime in and of itself, it has some uses, and might even be a bit easier to read than the Algol-60 version.
Life goes on.
Admin
I will care. But every language is different, so a blanket statement can't really be made about them that'd apply to all languages.
And while everyone thinks they have "their own style", I tend to side with Douglas Crockford on this issue. There is ultimately a correct way of writing a method with white-space/parenthesis/etc. in a given language based on the syntax of that language. Deviations from the "correct" spacing/parenthesis/etc. may not have an adverse effect on the programs performance or its ability to compile, but those variation can (and often do) detract from the readability of the program by other developers (or perhaps by you in 6mo?).
These are the least-important types of errors if you ask just about anyone in the industry, but given enough entropy and layered-on bug fixes, it can become a massive problem. This isn't to say that parenthesis in a return statement are the end of the world, but they're a symptom of a larger problem that is often ignored or not recognized as a problem until it's too late.
Admin
I think you missed the point. It has nothing to do with performing a single Boolean operation in a function; it has everything to with with not just returning the result of the conditional. There is no need to do the return true/false. Just return the result of the test.
Admin
This seems to work in Ruby (hoping I get line breaks in...):
Admin
Style is an argument that is pointless to make, because there will never be one camp. Are you the type that would also argue over whether line indents should be multiples of 3 or of 4 characters?
I'm also the guy who places parenthesis around every macro #define. Because the first time you debug a problem that turned out to be because of macro expansion in an expression causing an "precedence of operators" should be the last time you ever fall victim to that problem.
Admin
var i = { think = function(object) { return { "is": ((function isSimple(what) { return (Boolean(what.keys) === false || what.keys().length === 0) ? true: false; })(object).valueOf() != 0 ? false : true) ? "mischievous" :"evil"}; } }; i.think(this).is;
Admin
Which just goes to show that any feature can be abused. Though in this case I think the nested functions contribute much more confusion than the ternaries.
Admin
And in C+, return (x) can produce different behaviour to return x as it uses a different way of looking up x. So no possibility of disastrous confusion there.
Admin
if (condition) return true; else return false; allows you to set a breakpoint on one of the two return statements, or add a logging statement, if one of the two return values would be unusual.
Admin
I'm currently dealing with the same sort of insanity on one of my projects. Right now, I am having to convince a coworker that a function (literally) named:
is poorly named... WTF.
Admin
Funny, I feel the same way about putting parentheses around the expression in an if or while statement. Sure those are because a language designer got tired of saving keystrokes after defining = to be assignment and == to be comparison. Just because the language designer forgot to make redundant parentheses mandatory in a return statement is no excuse for programmers to be inconsistent in coding style.
(Even though I'm inconsistent in coding style since I don't put redundant parentheses in return statements, that doesn't mean I have an excuse.)
Admin
Should be, but won't be. You see, some Linux driver developer decided he/she didn't need parentheses around a few macro #defines, but got lucky because the macros weren't actually used anywhere. I posted it to the linux-scsi mailing list a few years ago, and no one cared. No matter how many times you or I debug problems caused by precedence of operators in macro expansions, we should not expect any of them to be the last time.
Admin
OK, what hacker leaked the source code of the Edge browser? Or is it from Cortana? Either way, we're going to sue you.
Admin
It could be worse. He could have put in a completely useless else statement.
Admin
The real WTF is of course
which I encounter all over some code which I have inherited...
Admin
@Jolyon Perhaps true is not a keyword, and it is defined in a macro or something.........
I'm a little surprised that no one has mentioned the hidden tl;dr code that always returns true.
Admin
So who is a lone developer supposed to do a code review with?
Admin
That said in Javascript I find myself writing this sort of code
const flag = functionThatReturnsSomthingTruthyOrUndefined() ? true : false;
Becasue I've been bitten in the past assigning the result of the function to a flag and having somebody else in another part of the program use flag === true and the code fail (or JSON encoding an object and seeing a string instead of the bool they expected)
Admin
I don't know if there's bad stuff with using it in Javascript, but !! is a good way in C to ensure that you actually have nothing but a 0 or 1 value, and not some other "truthy" value.
Admin
What possible reason is there not to write the first one as:
boolean someFunction() { return someBooleanExpression; }
?
Admin
A rubber duck.
Admin
TRWTF is failure to use the correct operator.
return somebooleanexpression ? true : false : filenotfound;
Admin
Nothing WTF about the first example. It's perhaps silly, but not a WTF. As others have pointed out, it's more readable (to some) and easier to debug. The second is clearly a WTF, but the general concept is not.
However, the REAL WTF here is the complete and utter lack of null checking when doing boolean expressions like in the second example with nested object/function dereferencing. It make the result NOT boolean, but rather true, false or NPE.
Admin
I agree with GWO on this, I don't see a problem with the function existing ( hasFeatures() ), this approach is used a lot in game programming (isAlive(), isEnemy(), isVisible(), etc). The issue is that the function could have been written in one line/simplified greatly. The use of a ternary operator is also nice.
Admin
Actually you still need to negate the boolean value to return false if the list is empty, given the method hasFeatures is asking if the list is NOT empty ergo. return !license.getFeatures.isEmpty ();
Admin
I've seen similar code where the true and false of the ternary operator were swapped (effectively negating the value). Kept me on my toes rewriting that code, I can tell you.