▲ 150 ▼ proportional reaction (lemmy.blahaj.zone) submitted 11 months ago by guber@lemmy.blahaj.zone to c/lemmyshitpost@lemmy.world 45 comments fedilink hide all child comments
[–] BeigeAgenda@lemmy.ca 24 points 11 months ago* (2 children) Don't you just love the readability a = a > b ? (b > c ? (a < d ? c : a) : d) : (b < c ? a : d ) permalink fedilink source hideshow 4 child comments replies: [–] subignition@fedia.io 8 points 11 months ago this is way more nested ternary operators than I would ever use (which I understand is for the sake of example) but if you rearrange them so that the simplest statements are in the true branches, and use indentation, you can make it at least a little more readable a = a <= b ? (b < c ? a : d) : b <= c ? d : (a < d ? c : a); permalink fedilink source parent [–] kryptonianCodeMonkey@lemmy.world 7 points 11 months ago (1 child) 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 permalink fedilink source parent hideshow 2 child comments replies: [–] BeigeAgenda@lemmy.ca 1 point 11 months ago 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. permalink fedilink source parent
[–] subignition@fedia.io 8 points 11 months ago this is way more nested ternary operators than I would ever use (which I understand is for the sake of example) but if you rearrange them so that the simplest statements are in the true branches, and use indentation, you can make it at least a little more readable a = a <= b ? (b < c ? a : d) : b <= c ? d : (a < d ? c : a); permalink fedilink source parent
[–] kryptonianCodeMonkey@lemmy.world 7 points 11 months ago (1 child) 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 permalink fedilink source parent hideshow 2 child comments replies: [–] BeigeAgenda@lemmy.ca 1 point 11 months ago 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. permalink fedilink source parent
[–] BeigeAgenda@lemmy.ca 1 point 11 months ago 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. permalink fedilink source parent