- 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
initialTab = 0; if (isNaN(initialTab)) { initialTab = 0; }
is probably a merge error.
Admin
Today's WTF: reading thedailywtf.com talking about the conditional operator as "the ternary operator".
Any operator that takes three arguments is a ternary operator.
Admin
from wikipedia:
Many programming languages that use C-like syntax feature a ternary operator, ?:, which defines a conditional expression. Since this operator is often the only existing ternary operator in the language, it is sometimes simply referred to as "the ternary operator". In some languages, this operator is referred to as "the conditional operator".
Admin
We all already know this. But since most commonly used languages only have at most one, then it is THE one.
Admin
I'm always amazed that in most threads you can find brilliant developers and painful idiots but no one in between.
Admin
Admin
Well, I just found you to be thoroughly mediocre. Glad I could help.
Admin
Any prime number that is divisible by 2 is an "even prime". However, I don't think it creates a great deal of ambiguity to talk about "the even prime", because there is only one.
Admin
My humble opinion on the "?:" operator (I'll avoid calling it the ternary operator so I don't offend an earlier poster):
It's handy when you would otherwise have had to declare a temporary variable, for example, if you are passing a value to a function:
Also if it allows you to combine a declaration with an assignment:
Sure, I could have declared the variable and then had an IF statement to assign the value, but that would turn one line into five.
And/or if the destination involves a complex expression:
Sure, I could do that one with an IF, but then both ends of the IF would have to repeat that whole complex expression describing where the value goes, which means that a reader would have to stop and figure out that the expressions are the same in both cases. (And of course, sooner or later someone would come along and change one but not realize he needs to also change the other.)
I avoid using it when any of the three expressions pass a certain threshold of complexity, because then it just gets hard to read.
I never, ever nest them. That's hard to read.
And the absolute worst use of a ?: I've ever seen -- stumbled across this one a couple of months ago:
Admin
We call the receptionist at our company the "unary operator". Because when she forwards a phone call to you, if you give her more than one argument, she just hangs up on you.
Admin
When your only tool is PHP, you soon want to shoot yourself in the head.
Admin
Second time I've posted this on this site:
United States, Canada, Mexico, Panama...
Admin
Why not just use if/else statements? There is nothing wrong with them. Just because it compiles doesn't make it a good idea.
Admin
We use a convention where ternaries select between to choices of data rather than code (that would be a job for if).
That said a properly formatted cascading ternary makes for a nice compact look-up table:
(Thanks to ChrisF)
Admin
Because banning a language construct just because bottom of the barrel idiots use it wrong, is both asinine and counter-constructive. Sure, you can ban it and solve a few problems. Now deal with the other 99% of stupidity these people produce because they have no affinity or feeling for writing good, clean code.
Meanwhile the loss of all the good use cases get under the skin of your talented and actually qualified developers. And then, before you know it someone tries to be clever about it. And then "life, uh... finds a way":
Admin
Yeah, you completely misunderstood his post.
Admin
Admin
Admin
Ok, But why not use a select case statement? Why people need so much to use a "?:" everywhere? it make you feel 1337?
Admin
Nested ternary operators are OK as long as 1. code is formatted property 2. you shouldn't be using a switch instead and 3. you're not using PHP. Which, thankfully, this person isn't. http://phpsadness.com/sad/30
Admin
That's because everyone in between has the good sense to shut up.
Admin
We could make it fit to anything. I rememebr seeing a show where people were given a random book and had to sing excerpts to different tunes.
Admin
I use ?: when I want an expression's value to be conditional, and if-else otherwise. I avoid nesting them if I need to use parentheses or both branches need to be complex. I think this is excessively verbose:
and should be replaced with
and likewise I'll write
instead of
Admin
Admin
Admin
hmmm (not my code)
Admin
Admin
I had a colleague who used that anti-pattern. After carefully and methodically annihilating it from the code base, seeing it again MAKES ME WANT TO CLAW MY EYES OUT!!!!!!
Thanks, hope you have a nice day!
Admin
I see your worst one, and I'll raise you one I saw nearly twenty years ago...
Admin
[quote user="Steve The Cynic"]
[/quote]Or alternatively, in English-like: [quote]If the thingy was edited, then use the percentage times the basis if we are using a percentage and one is available, or if we are not using a percentage, use the fixed amount. If none of the above conditions are met, use the null amount.[/quote] In general, I'd prefer to invert the is-null test so that the positive "use-something" conditions are clumped together, and I'd prefer a function "useFixedAmount()" that is equivalent to "!userPercentage()" so that the test looks more related to the code in the branches, but it's reasonably clear. [quote]
[/quote] Better?[/quote]Even better, thanks to the return, there's no need for else clauses.
Admin
Yes... but the point is... ANYTHING can be MADE to fit ANYTHING.
That doesn't mean that it naturally "fits", which is what is meant when you say "oh look... these words fit to that tune".
If you say that about something that doesn't naturally fit, but which can be made to do so in a clunky and unsatisfactory way, then you're saying nothing of value, and you're sounding like an idiot while you do it.
Admin
You can find it easier to fit your speach into a tune using https://play.google.com/store/apps/details?id=com.smule.autorap&hl=pl
Admin
Well I gyuess I'm in a somewhat whacked state today, as I read the shell line in the article as if ($horror = ...
instead of if ($error = ... Which seemed somehow more correct.
And now I read your line as "the evil prime"....
I guess I need more coffee
Yazeran
Plan: To go to mars one day with a hammer.
Admin
The best thing about ternary expressions is that you've got so many ways of writing them. For example you might hate using the ! operator in a ternary condition, which results in code like this:
or alternatively you might hate nesting ? : between ? and :, which results in code like this:Admin
I really wish you'd never mentioned this!
Admin
I'm a fan of Edsger Dijkstra and "grew up" on structured programming. By using if blocks the steps are clearly defined, reducing the chance of inserting operations incorrectly between the return statements.
Admin
This is exacly why you should not use a "?:" in a normal situation! Code is harder to maintain.
Admin
Or a nested IF? What's the advantage of the ?: there?
Admin
You wouldn't happen to use SFDC's Apex, would you?
Admin
Auld Lang Syne (see page source code/comments) definitely works a lot better for me - I can't get Rule Britannia to fit at all (verse or chorus).
Admin
You forgot Irish Girl! CAPTCHA = mara
Admin
Maybe comedy shouldn't be analyzed...
Admin
There is a well-known bug in the order of operations of PHP ternary operators.
Always use parentheses for nested ternaries in PHP, or this will happen:
http://www.phpsadness.com/sad/30
Admin
But we get it: you don't like PHP. A remarkably audacious stance that I'm glad you felt necessary to share.
Admin
I'm not sure how the hell you need to pronounce HTML for that to work. Is the dollar silent?
Admin
I was going to suggest switch, but it turns out PHP can't even take advantage of jump tables, and will convert it to chained ifs anyhow.