3 ms·
Since everyone agrees, I'll disagree :). Seriously though, there's nothing in ternary operator that says it should only be used for side-effect free code. Espe
by TeMPOraL 9y ago
Since everyone agrees, I'll disagree :).
Seriously though, there's nothing in ternary operator that says it should only be used for side-effect free code. Especially in C++ (or Java), which doesn't even differentiate between stateless and stateful code on the language level.
IMO ternary is a perfect solution for the case where you want to assign or return a different value based on a boolean condition. Whether or not the code in branches is side-effect free is irrelevant.
Personally, where I try to avoid using ternary operator, is places where I don't care about returned value. So "yes" for:
auto foo = [condition] ? possibly_stateful_then() : possibly_stateful_else();
no for:
[condition] ? then_statement() : else_statement();
- Mithaldu 9y ago> Personally, where I try to avoid using ternary operator, is places where I don't care about returned value. As i asked below: Why though?
- TeMPOraL 9y agoNo solid reason, just a feeling. I feel that if-else block communicates intent more clearly when you're dealing with statements - because it can't return a value in languages we're discussing, it's specialized for that job. The same motivation makes ternary better when we care about a value.
- Mithaldu 9y agoTo be honest, with that explanation the decision you made has nothing to do with ternaries, and is entirely just the mirroring of the decision the if forced on you. Also, > in languages we're discussing Don't bury that lede. If you're restricting what you're saying to a specific language say that bold and as the very first thing, so people don't end up wasting their time parsing your post in a different context. Rude.
- TeMPOraL 9y ago> To be honest, with that explanation the decision you made has nothing to do with ternaries, and is entirely just the mirroring of the decision the if forced on you. I like to think of it as using the dedicated tool for a job it was designed for. In my mind, it's a good rule of thumb because it enhances readability (compare "the principle of least surprise"). > Don't bury that lede. If you're restricting what you're saying to a specific language say that bold and as the very first thing, so people don't end up wasting their time parsing your post in a different context. Rude. Why? Come on, at this point in the thread we'll still discussing C++ derivatives. I thought it was obvious. Also: if a lanuguage has an if/else as an expression and has a ternary operator, then IMO there's a problem with that language being cluttered with duplicate syntax for the same thing.
- TimJYoung 9y agoI agree. If a function isn't side-effect-free, then you've got other problems besides whether it's used with a ternary operator. My primary language, Object Pascal, even goes to the length of naming functions (return values) and procedures (don't return values) differently. It really helps to focus the mind on the purpose of each: one almost always expects procedures to do something and, most likely, modify something that may introduce side effects. Functions are almost always side-effect-free in "decent" Object Pascal code.
- mickronome 9y agoTernaries are often used as single-usage substitites for a variable, but it's not rare to see the ternary statement simply copied somewhere else if the same value is needed again. Using ternaries only when it is free of side-effect can both helo avoid a class of unnecessary errors, and add meaningful semantic information that some code is, or should be, side-effect free. It would be great if the compiler(s) in various languages supported enforcing it, but sometimes you take whatever you get.