5 ms·
What's the point? Why not just make it an if-elseif-else block and make it obvious?
by macspoofing 8y ago
What's the point? Why not just make it an if-elseif-else block and make it obvious?
- verall 8y agoTernary isn't the same as a branch and you want your this simple comparison function to get inlined every time
- masklinn 8y ago> Ternary isn't the same as a branch It should be. The C ternary is just an expression-oriented version of if/elseif/else. Languages with a functional bend simply make if/elseif/else an expression in the first place e.g. in Rust it'd be return if a < b { Ordering::Less } else if a > b { Ordering::Greater } else { Ordering::Equal } Though obviously that specific version is an overly complex way of writing: return a.cmp(b);
- naasking 8y agoMost compilers will probably reduce if-else and ternary to the same instructions. If it's a conditional value binding it might not be a branch but a conditional move instruction.
- convolvatron 8y agoi find it really odd that of all the knocking back and forth on this thread, you are the only person to suggest that using symbolic values for 'greater than', etc, improves readability.
- masklinn 8y agoIt's not really my suggestion though, it's just what Rust does (inherited from Haskell, interestingly enough OCaml does not do that) and I figured I'd post "proper" rust code rather than a bastardisation.
- macspoofing 8y agoIf you need to optimize to that extent, then do whatever you need to do. In the vast majority of applications it makes no difference.
- naasking 8y agoNested ternary is obvious when formatted as above, and it's dramatically more compact. Try it for a bit, you'll see how simple and clear it is.
- macspoofing 8y agoYou see the problem though, I need to try it and practice it to see how simple and clear it is. I can write code that I can easily understand. I can write terse, clever code that I can easily understand. But I want to write code so that the next person can also easily understand it. More importantly, code that's easier to understand has less chance of getting buggy when it inevitably gets rfactored or extended. >and it's dramatically more compact Why do I want that?
- munchbunny 8y agoCompactness and readability are related, in the sense that your working memory also works somewhat in terms of lines of code. There's a natural trade-off in the sense that in order to make something more compact, you have to rely on the context to provide whatever information you remove. For example, the ternary pattern of chaining ?:'s. You have to be used to it. However, once you know it, the more compact pattern works fairly well.
- naasking 8y ago> You see the problem though, I need to try it and practice it to see how simple and clear it is. You need to practice it to overcome your skepticism resulting from your ingrained habits that prejudice you against it, not because it's inherently unreadable. It literally takes 5 seconds to understand the idiom: conditions/guards on the left, value on the right. It's essentially a truth table. > Why do I want that? To add to the other poster: the more context you can fit on your screen, the less scrolling, jumping you need to do to understand a program's behaviour. Compactness that doesn't sacrifice readability speaks for itself. The code sample we're discussing is compact and very readable.
- macspoofing 8y ago
- dllthomas 8y agoWith the ternary it's more obvious that you're always putting the result in the same place.
- Roboprog 8y agoIs it obvious that the same variable is assigned in every branch, though?