6 ms·
Wasn't aware that this was a consensus; I always avoid ternary expressions for this reason.
by jcapote 16y ago
Wasn't aware that this was a consensus; I always avoid ternary expressions for this reason.
- RyanMcGreal 16y agoThe syntax for ternary expressions in Python seems pretty hard to confuse: x if condition else y
- modoc 16y agoIt's uglier in Java: minVal = a < b ? a : b; I never use them, and typically enforce coding standards preventing their use as well. Good code should be easy to read, and if/else blocks are easy to read.
- ShardPhoenix 16y agoI find inline Java ternaries often make the code easier to read overall than a whole if-else block, for two reasons: 1. The code is shorter, so there's less to read in the first place. 2. The value returned is being used right where it's needed, so you don't have to carry it so far in your head. I certainly don't use them all the time though, just for short/simple expressions.
- moron4hire 16y agoThe use of the ternary operator for such things as: String a = (b == null) ? "" : b; is okay with me, at least, when I'm working in languages other than C#, where it's just: string a = b ?? ""; I also use it within other operations, like: var output = string.Format("Something {0} something {1}", string1.Length > 0 ? string1 : "N/A", string2.Length > 0 ? string2 : "whoops"); My general rule of thumb is, if breaking the ternary operation into multiple lines will make it more readable, then you're going too far with the ternary operator.
- watmough 16y agoThe use of ternary operators is a standard idiom, at least in C, and reduces the visual noise of if..then..else used to implement code like your example. Enforcing coding standards to ban the use of standard idiom is exactly the sort of reason that I refuse to work for bone-headed cubicle farm-type companies.
- loewenskind 16y agoAs ugly as the ternary ?: operator is, it's an expression so that makes it nicer than if/else blocks that require variable mutation.
- tomjen3 16y agoLets better hope we never end up on the same team then. I have no patience for dumbing down shit to people who have no business writing code. The ternary operator is a standard and it is in the language for a good reason - it can make certain statements smaller and involve simpler code (that is, less places to screw up). Yes it can be abused, but so can everything else. The solution is not to cut out features but to cut out those who are not fit to program.
- brownegg 16y agoTo be honest, this surprises me a lot. I'm not a professional programmer, for some definition of same; but I do write a great deal of code and make my living from using and maintaining it. And for me, saying foo = bar<baz ? 42 : 0; is really quite nice.