guber@lemmy.blahaj.zone to Lemmy Shitpost@lemmy.world · 4 days agoproportional reactionlemmy.blahaj.zoneexternal-linkmessage-square45fedilinkarrow-up1164arrow-down118
arrow-up1146arrow-down1external-linkproportional reactionlemmy.blahaj.zoneguber@lemmy.blahaj.zone to Lemmy Shitpost@lemmy.world · 4 days agomessage-square45fedilink
minus-squarekryptonianCodeMonkey@lemmy.worldlinkfedilinkarrow-up7·3 days agoWeird example. 3 nested conditionals is not the typical use case for a ternary, and 2 of the 5 branches result in a pointless a=a assignment. I agree this is bad code, but it’s just as bad and hard to parss in a normal if-else structure too: if (a>b) { if (b>c) { if (a<d) { a=c; } else { a=a; } } else { a=d; } } else { if (b<c) { a=a; } else { a=d; } } In another situation, though, it’s perfectly readable to have a much more typical ternary use case like: a = c > d ? c : d And a pair of parentheses never hurt readability either: a = (c > d) ? c : d
minus-squareBeigeAgenda@lemmy.calinkfedilinkarrow-up1·23 hours agoGood point, I have only seen 2. nested ternary operators in the wild, and I am pretty sure the second level was added as a bugfix.
Weird example. 3 nested conditionals is not the typical use case for a ternary, and 2 of the 5 branches result in a pointless a=a assignment. I agree this is bad code, but it’s just as bad and hard to parss in a normal if-else structure too:
if (a>b) { if (b>c) { if (a<d) { a=c; } else { a=a; } } else { a=d; } } else { if (b<c) { a=a; } else { a=d; } }
In another situation, though, it’s perfectly readable to have a much more typical ternary use case like:
a = c > d ? c : d
And a pair of parentheses never hurt readability either:
a = (c > d) ? c : d
Good point, I have only seen 2. nested ternary operators in the wild, and I am pretty sure the second level was added as a bugfix.