5 ms·
In practice it's not that bad. One bad thing is you can't write long chains as you can do with C-style ternary operator.
by chakkepolja 5y ago
In practice it's not that bad. One bad thing is you can't write long chains as you can do with C-style ternary operator.
- Cthulhu_ 5y agoThat's not a bad thing, long ternary chains are illegible. You should never have more than one condition in a ternary.
- pdpi 5y agoThere are some specific shapes of chained ?: that are fairly legible. Eg c = condA ? valA : condB ? valB : defaultVal;
- DonHopkins 5y agoFor Turing's sake, if you insist on chaining ternary operators, then you should always use parens instead of showboating that you cleverly-by-half memorized a particular language's obscure precedence and associativity rules. PHP in all its glory and splendor actually originally used left-to-right instead of right-to-left associativity for the ternary ?: operator, so that expression you wrote works differently in PHP and (JavaScript or C or any other language). (Side note: notice how I used parens instead of depending on you guessing about the relative precedence of "and" and "or" in English.) It's easy (and perfectly valid) to blame the designers of PHP for not knowing or caring or paying much attention to the syntax of the programming languages they were imitating, and doing something that stunningly stupid, but it just goes to prove that many people (especially PHP designers like Rasmus "I don't care about this crap at all" Lerdorf and typical PHP programmers) don't even have the faintest idea what the precedence and associativity of many of their favorite programming language's operators are, or what those terms even mean, and they just willy nilly cargo cult copy and paste and translate between PHP and JavaScript code examples from Stackoverflow, introducing terrible hard-to-spot bugs. https://en.wikiquote.org/wiki/Rasmus_Lerdorf https://en.wikiquote.org/wiki/Rasmus_Lerdorf >"We have things like protected properties. We have abstract methods. We have all this stuff that your computer science teacher told you you should be using. I don't care about this crap at all." -Rasmus Lerdorf But the real fault lies not just with PHP for parroting C incorrectly, but with C for doing something that stupid and hard to remember and easy to get wrong in the first place. Please always use parens for that kind of stuff, because even if YOU memorized and love to depend on and show off your knowledge of the precedence and associativity rules of the language you're using, the people reading and modifying and maintaining your code probably don't. https://wiki.php.net/rfc/ternary_associativity https://wiki.php.net/rfc/ternary_associativity >PHP RFC: Deprecate left-associative ternary operator >Unlike most (all?) other languages, the ternary operator in PHP is left-associative rather than right-associative. The left-associative behavior is generally not useful and confusing for programmers who switch between different languages. This RFC proposes to deprecate and remove left-associativity for the ternary operator and require explicit use of parentheses instead. >As an example, the code return $a == 1 ? 'one' : $a == 2 ? 'two' : $a == 3 ? 'three' : $a == 4 ? 'four' : 'other'; >would in most (all?) other languages be interpreted as return $a == 1 ? 'one' : ($a == 2 ? 'two' : ($a == 3 ? 'three' : ($a == 4 ? 'four' : 'other'))); >which is both the useful and intuitive interpretation. In PHP, it is instead interpreted as return ((($a == 1 ? 'one' : $a == 2) ? 'two' : $a == 3) ? 'three' : $a == 4) ? 'four' : 'other'; >which is generally not what was intended. https://stackoverflow.com/questions/20559150/ternary-operator-left-associativity https://stackoverflow.com/questions/20559150/ternary-operato... >Q: Can someone please explain what is happening here and why it is printing 'four'? >A: Because your whole expression evaluates as if it was (......) ? 'four' : 'other'. Since the first element is probably something truthy, it gives you 'four'. In saner languages, where ?: has right associativity, the whole expression evaluates as if it was $a == 1 ? 'one' : (......), where if $a is not 1, you go on to test other things. http://phpsadness.com/sad/30 http://phpsadness.com/sad/30 >PHP Sadness: Ternary operator associativity >The ternary operator is left-associative and therefore behaves entirely incorrectly: https://eev.ee/blog/2012/04/09/php-a-fractal-of-bad-design/ https://eev.ee/blog/2012/04/09/php-a-fractal-of-bad-design/ >PHP: a fractal of bad design >Unlike (literally!) every other language with a similar operator, ?: is left associative.
- water8 5y agoStill beats APL
- DonHopkins 5y agoNo, consistency within a language is good! https://en.wikipedia.org/wiki/APL_(programming_language)#Design https://en.wikipedia.org/wiki/APL_(programming_language)#Des... >All primitives are defined to have the same precedence, and always associate to the right. Thus, APL is read or best understood from right-to-left. But consistency across all languages is too much to ask for, except when one language is purposefully imitating another language's syntax but subtly changing it, like PHP did to C. Or the way Perl and PHP imitated C's syntax for references with &, implying that they were somehow alike and could be used in similar ways, even though they're totally different foot guns. At least APL and FORTH and LISP pick a lane (albeit different sides of the road), and stay in it.
- chrismorgan 5y agoAs PHP’s RFC says, right-associative ternary is useful and intuitive. I don’t need no parentheses, the meaning is clear and obvious and doesn’t go wrong in any environment that I or those I work with use, and doesn’t even go wrong in PHP any more (warning in 7.4, syntax error in 8). I’m not going to compromise the beauty and editability of my code by inserting superfluous parentheses—they’d make me have to think more about the meaning of the ternary chain, not less.