- 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
I think @chubertdev wins. He only used 2 lines to your 3. So much for your Leader-ness.
Admin
@aliceif used one line, so I think she wins. Unless someone wants to try to do it in 0 lines.
Admin
Admin
Hey, I use one line, and I did it frist:
Unless you're saying that ternary operators are disallowed.
Admin
Admin
That only works in languages which permit implicit casts from boolean to int (or some other numeric).
Admin
I took this books advice a long time ago, http://en.wikipedia.org/wiki/The_Elements_of_Programming_Style -> be clear, don't use tricks.
Ternary operators have to be tiny phrases to be readable.
I'll try refactoring the given code for clarity based on Vivek's breakdown
if failsafe == 0 { error = "No technical alarms" else error = "" if failsafe & 1 concat( error, "Detection zones staying in a given state;" ) if failsafe & 2 concat ( error, "Bad visibility;" ) if failsafe & 4 concat ( error, "Initialization;" ) if failsafe & 8 concat ( error, "Bad configuration;" ) endif
Admin
And the ternary only works in languages with ternary operators...
Admin
One I use often in
PHPTRWTF:Properly formatted I find it more readable when I have like 4 or 5 of those in a row.
Admin
Also one line. What do I win?
Admin
Oh man, I could see this:
Admin
And I fucked that example up because I never call the variable and parameter the same, for the exact reason of avoiding confusion.
And then I do it in pedantic dickville. Figures.
Admin
No pressure or anything.
Admin
Admin
Discourse aside..... WHile I think ternary operators are overused, I think are occasionally useful for succinctness without losing readability, eg:
print "you have ${NUM} carrot".${NUM}==1?"":"s"."left"
or even: printf("The answer is %d\n", x?0:*x);
Admin
That's not clear at all. What language is that, anyway? It seems to be missing like 5 endifs and a lot of parentheses.
Admin
It's the open bracket followed by an "endif" instead of a close bracket that really gets me.
Admin
Then there are languages (probably older than most of us) that allow: a := if b = 5 then 0 else 42;
This language is over 50 years old!
Admin
(define setvalue (or (and testvalue 0) 42))
Admin
Note: MySQL, didn't check other engines
Filed under: FROM dual
Admin
a := 6 * 9 == 42;
Admin
If you have a case where you might actually care about the performance of a few string concatenations, you should be returning the bit field, not a string representation of it.
Admin
Admin
The most common use for the ? operator I've had comes in very handy in slicing and math-like operations.
For instance (all variables are integers)
slice_size = num_slices / num_elements + ( num_slices % num_elements ? 1 : 0);
This is a very common idiom in SIMD code.
That being said, yeah, the submitter's line is all kinds of WTF.
Admin
Admin
In Java, I found that I can replace this beauty
with this code
Personally, I like the second much better.
Admin
The real WTF is that Intercourse (sorry, discourse) doesn't even let me view comments without JavaScript now. At least before I could read the comments even with JS disabled. Now...
Well, fuck that I ain't enabling JS for this site, 'cause it's not needed. At all.
Captcha minim. The minim you need, is for the bloody thing to work without JavaScript enabled.
Admin
Pretty sure I've seen this in the wild, too. Only works in languages designed for such horrible things.
Admin
What happens if
testValue
is @FILE_NOT_FOUND?Admin
I'm more concerned about
testValue
being @mikeTheLiar.Admin
Your example is a TRWTF, since it's instanly impossible to localise correctly. Gah, I hate coders who would think this is a good way to add plurals, even as a throw-away sample.
Admin
variable = (test_value == FILE_NOT_FOUND ? default_value) && true_value
But personally, I would introduce a
WTF
operator:variable = test_value !? fnf_value : true_value : false_value
You can also use the plain old
?
as the WTF operator, since they require a different number of arguments.Admin
Unfortunately, getting the icon right means breaking valid syntax.
x ? !y : z;
Therefore, we are removing the unary negation operator from the language.
Admin
When discourse/JS is used for all comments this site will probably lose its attraction to me, after which it gets removed from my list.
Oh well, such is life.
Admin
Admin
Well if we survived C++ getting rid of the most vexing parse, I'm sure making a special case for
?! x : y : z
vs? !x : y : z
won't be a problem.Admin
Fun fact: when a user just installs stuff from random places and gets hit by spware, malware or viri its ofcourse his own fault, as he should have choosen more reliable sources.
If however the same person doesn't allow JS, flash and other active content like it also coming from random sources he's the odd-one-out. Go figure. :-)
@People of WTF: Is it really that hard to return a bit more specific error when someone uses true HTML tags ? The way its put it could have been anything, including stuff nothing to do with the post itself. :-\
Admin
Localization is only important when the requirements say it is. I happily don't have to give a second thought to localization because the eyeballs that see the results of my code are a very limited group. Ditto for browser compatibility. If everyone uses IE8 (ugh) in your organization, it's at least one less thing to look out for.
Please provide a one line example that is BETTER at expressing "0 carrots", "1 carrot", "2 carrots". Seriously. I really want to know a better way to do this. Ternary solutions, used sparingly, can be quite elegant.
Admin
Admin
Scala lets you do
Admin
I'm surprised, over 24 hours later and still no one has posted the REAL wtf: magic constants!
I've fixed this problem in the original code and it is much more readable:
Admin
There. Much more readable.
Filed under: shipit!
Admin
I agree 100% with article-writer Mark Bow-Ties. Clearly the problem is the ternary statements, not the fact that it's an unnecessarily long case analysis which has been packed into one line! For if you just use
&&
and||
things get so much better! FailSafe==0&&'No technical alarms'||((FailSafe&1)!=0&&(FailSafe&2)!=0&&(FailSafe&4)!=0&&(FailSafe&8)!=0&&'Detection zones staying in a given state; Bad visibility; Initialization; Bad configuration'||((FailSafe&1)!=0&&(FailSafe&2)!=0&&(FailSafe&4)!=0&&'Detection zones staying in a given state; Bad visibility; Initialization'||((FailSafe&1)!=0&&(FailSafe&2)!=0&&(FailSafe&8)!=0&&'Detection zones staying in a given state; Bad visibility; Bad configuration'||((FailSafe&1)!=0&&(FailSafe&4)!=0&&(FailSafe&8)!=0&&'Detection zones staying in a given state; Initialization; Bad configuration'||((FailSafe&2)!=0&&(FailSafe&4)!=0&&(FailSafe&8)!=0&&'Bad visibility; Initialization; Bad configuration'||((FailSafe&1)!=0&&(FailSafe&2)!=0&&'Detection zones staying in a given state; Bad visibility'||((FailSafe&1)!=0&&(FailSafe&4)!=0&&'Detection zones staying in a given state; Initialization'||((FailSafe&1)!=0&&(FailSafe&8)!=0&&'Detection zones staying in a given state; Bad configuration'||((FailSafe&2)!=0&&(FailSafe&4)!=0&&'Bad visibility; Initialization'||((FailSafe&2)!=0&&(FailSafe&8)!=0&&'Bad visibility; Bad configuration'||((FailSafe&4)!=0&&(FailSafe&8)!=0&&'Initialization; Bad configuration'||((FailSafe&1)!=0&&'Detection zones staying in a given state'||((FailSafe&2)!=0&&'Bad visibility'||((FailSafe&4)!=0&&'Initialization'||((FailSafe&8)!=0&&'Bad configuration'||'Unknown'))))))))))))))Compare that to this filth:
The question marks just stare at you, questioningly. It's enough to drive a sane developer mad.
Admin
It would be
setValue = testValue || 42;
, common in Javascript ... And it's a shorthand that grows on you. It makes some sense, it's a logical extension of boolean expressions being short-circuited and the fact that values of any type can be "falsy" (if they are0
,''
,null
,undefined
,NaN
, orfalse
), or "truthy" (if they are not falsy). Thea || b
operator can be equivalently rewrittena ? a : b
, likewise thea && b
can be rewrittena ? b : a
.Only thing you have to be sure of is that
testValue
can't be a value that is both falsy and valid. If0
is a valid value, you'll accidentally overwrite it with the default.Admin
Admin
In the original
setValue
equals0
iftestValue
is false (in C and Perl or some shells with$
s thrown in). So when the first test succeeds the expression continues to42
, which is also true, and then returns that final value. IftestValue
is false, then the expression returns whatever represents falseness in that language, which for C/Perl/whatever is 0, so it's abusing the Boolean and integer nature of 0.a = b || c
is perfectly idiomatic in several languages, but it's different from what I'm trying to do above.Admin
But not on embedded devices!
Admin
Given that, if I've read it correctly(*), you are calculating
(int)ceil(num_slices/(double)num_elements);
.(*) This is my major beef with ternaries (especially with abbreviated conditions) - they make the code harder to read. I'd generally prefer my second alternative for readability but the other one for SISD performance (avoids having both / and %, although this might not be relevant on x86, which calculates both when executing a machine language integer divide instruction, so the % is basically "free").
Admin
This really depends. I tend to use ternaries mostly in repeated situations that lead to short, readable code because you know what's coming. ie: (php this time, but it applies in any language that you need to do null types of checks to load a variable)
Since you know after the first line that the whole thing is the same repeated logic, it's way easier to read than the multiple lines it takes to do an if/then/else block.
Filed Under: In before "why didn't you use array_key_exists?", and "PHP is TRWTF".
Admin
array_key_exists takes about ten times as long as isset does, according to a comment on the documentation page.