3 ms·
Nested ternary
by sethammons 9y ago
Nested 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.