- 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
ternary operators....
[image]Admin
Can we get a classic from before this year or is this some kind of Discourse/new site design limitation?
Admin
Ask that exact same question tomorrow and your wish will be granted.
At least, probably, until the next non-classic article gets published...
Admin
Tomorrow's article: "Happy New Year! Take a break!"
Friday's article: "Classic WTF 2015 - Happy New Year! Take a break!"
Admin
I like ternaries to keep things short and simple, but once you get over 3 or 4 simple nested ones or a couple complicated ones, you're probably better off going to if statements.
Admin
I think ternary operators address a need for conditional expressions (as opposed to statements). I use them in languages that don't provide conditional expressions. But ideally, I'd rather do something like:
This is easier to follow than two separate assignments to
foo
and it makes it makes the purpose of the whole conditional clearer.Admin
Admin
Yeah, ternary operators are actually awesome. Nested ones are usually WTF-ey.
Admin
Didn't really read all of it but at a quck glance I would replace most of the code with some hefty binary mask operators...
Admin
Hold on, I've got a reaction for code like this...
[image]Ah, there we go.
Admin
The way I tend to use ternary operators is in special cases for strings. For example, in C++:
Otherwise, I tend to use the if-else structures.
Admin
Christmas is special and sacred. Meant to be spent with loved ones.
New Years though is about drinking, staying up late, and (occasionally) making poor decisions as a result. Since any of these three things can be pinpointed as the root cause behind many, if not most, of the WTFs that we publish, anything goes!
Oh, I plan to run an Errord on Friday.
Admin
I don't think a human wrote this code, but I can't decide between decompiler or orangutan.
ETA: I don't always use ternaries, but when I do, I'm usually trying to a. avoid dereferencing a null pointer, b. get NULLIF or two-argument COALESCE action, or c. making my code harder to internationalize like Nutster.
Curly brace languages are simply a PDP-11 macro assembler that is about 2/3 of the way to reading mail.
Admin
Nested ternaries are awesome, if you follow the correct association rule. Consider:
If you end up needing parentheses to do the nesting, you're doing it wrong and will regret it. lalalalaalla body is too similar to what I recently posted wah discourse wah why
Admin
PHP gets the ternary's asssociativity wrong, of course.
Admin
I use ternaries for all assignements and returns, if statements are for flow control.
Good indentation makes most ternaries very readable.
But for more complex or nested ternaries I usually try to break out part of them into functions that through the name makes the intention clear.
Deeply nested ifs are no better than deeply nested ternaries for readability ;)
If I still end up with unreadable code I probably need to refactor the whole part of the code :D
Admin
FTFY
Admin
No, it just follows its own rules.
Admin
Its own rules which are wrong.
Admin
The comments on the article @CoyneTheDup linked explain that well enough.
Admin
Then we are all in agreement then.
<img src=http://whydoesitsuck.com/public/why-does-php-suck/thumbnail.png" alt="PHP sucks!">
Admin
QFT
Admin
The funny thing is, the code in article is basically concatenation of string constants based on bit fields values. No ifs or ?:s needed at all.
Admin
Ternaries are bad, yes. But the real problem are those && || destoperators. Aside from the few l33tkings, no programmer really knows what they mean or how they work. They often just turn a piece of code into an unreadable mess of symbol soup.
You tell me - what's more readable: (A)
(B)
Admin
:trolleybus:
Admin
Don't do this! Plurals don't work the same way in all languages! Use
ngettext
and friends instead!Admin
###(C)
also either put spaces around your operators or don't but mixing modes between examples is cheating! :-P
Admin
Does (B) even test things the same way as (A) does?
Admin
this too.
the second example might (and i say might, because i think (C) is best) be more readable, but it also gets different answers for same input compared to (A)
Admin
(D)
Admin
http://i.imgur.com/bj3qaA6.gif
Admin
(E)
Admin
Added note: I only advocate pattern (E) in the illustrated example, where all lines are of acceptable line length. If one of the conjunctive (or disjunctive, if the logic is the other way around) subclauses does not fit on a line, this pattern very quickly becomes unreadable.
Admin
(F)
http://www.reactiongifs.us/wp-content/uploads/2013/03/kill_it_with_fire.gif
Admin
What I want to know is why nobody uses a good old fashioned
switch
for this sort of thing. It might take more lines of code and might be one whole lot clearer. That might be seen as a good idea…Admin
(A) is in disjunctive normal form. So... (A).
Admin
It can be okay to have long clauses. I do prefer prefix notation to postfix notation. For example:
Admin
What unmerciful god rules over you?
Admin
(G)