6 ms·
Ternary operators are the best! Highly expressive and concise, and everybody instantly understands it. Is there a single drawback about it?
by dingo_bat 9y ago
Ternary operators are the best! Highly expressive and concise, and everybody instantly understands it. Is there a single drawback about it?
- sethammons 9y agoNested ternary
- masklinn 9y agoEven more so. Between var v T if (cond0) { v = value0 } else if (cond1) { v = value1 } else if (cond2) { v = value2 } else { v = default } and val v T = cond0 ? value0 : cond1 ? value1 : cond2 ? value2 : default I'd much rather have the latter.
- randomdata 9y agoHowever, your if statement is unidiomatic and quite unlike how you would normally write that particular logic in Go. If you really want to make a fair comparison, you might consider writing it in the way that Go is normally written, not the way that someone familiar with another C-style language might write the code if the ternary operator was taken from them.
- masklinn 9y agoPlease do go ahead and demonstrate how you would normally write that particular logic in Go. Would it perchance be this piece of finery? var v T switch { case cond0: v = value0 case cond1: v = value1 case cond2: v = value2 default: v = default }
- baby 9y agoI think GP pointed out the if () construction that doesn't make sense in Go. There are no parenthesis in Go. Shows that you pretty much never used it?
- masklinn 9y agoYes I'm sure > However, your if statement is unidiomatic and quite unlike how you would normally write that particular logic in Go. If you really want to make a fair comparison, you might consider writing it in the way that Go is normally written, not the way that someone familiar with another C-style language might write the code if the ternary operator was taken from them. meant > the parens are unnecessary and they didn't just write that because reasons, which would also be the why they commented an other half dozen times since then but just couldn't come around to showing "how you would normally write that particular logic in Go" because it would take all of 5 minutes.
- baby 9y agoMaybe because you're used to it, but your second example looks unreadable to me and I know ternaries.
- citrin_ru 9y agoTernary operators can be abused. E. g. somebody can write code with nested ternary operator and long expressions. If/else will be more readable in this case. Go is a nanny language - Go authors give to coders on Go as little foredoom as possible.
- drdaeman 9y agoAnything can be abused. There is no technological or design measure that can prevent one from writing crappy code in Go or any other language. I'm certain of this as I had personally written some. Except for esoteric languages, of course. I've yet to see someone claiming some code in Malbodge is crappy and offering a refactored version. ;) Just kidding.
- code_sloth 9y agoHave you seen what atrocities people can do with nested ternaries?
- masklinn 9y agoThe only language in which you can't see atrocities is whitespace, because you can't see the atrocities.
- bluedino 9y agoThey're easily abused: H = (C == 0 ? null : V == r ? (g - b) / C : V == g ? (b - r) / C + 2 : (r - b) / C + 4);
- deleted 9y ago[deleted]
- vesak 9y agoI've been programming in languages that have them, and almost every time I have to read code that has a ternary operator, I have to stop and think what's the condition, what's the "true" branch and what's the "false" branch. This is even worse in languages that have truthy values, since you don't even have the "== True" thing to help.