4 ms·
It's just syntactic sugar for if-else though, so it's right in the sweet spot of "doesn't make a big difference either way" and "is subject to preference" that
by serve_yay 12y ago
It's just syntactic sugar for if-else though, so it's right in the sweet spot of "doesn't make a big difference either way" and "is subject to preference" that makes it a prime target for bikeshedding. Personally I don't like the ternaries either, typically the syntax is ugly, but the if-else seems only slightly better to me.
- Retra 12y agoHere's how you should use ternary: return (isblue ? "Blue" : "Red");
- rootbear 12y agoMy evil twin wants to do this some day: (pickfunc? func1: func2)(pickarg? arg1: arg2); Works great!
- the_af 12y agoIt's not syntactic sugar. The ternary operator usually appears in imperative languages (or should I say, languages which are not expression-oriented) where the if-then-else construct statement is NOT an expression, and possibly has side-effects. Therefore, you need an alternative construct that really is an expression and evaluates to something. The syntax itself is irrelevant. If you can say the following in your favorite language: x = if (condition) { something } else { something else } then you don't need a "ternary operator".