3 ms·
Are you saying this suggests it does something different from what it does? What does it suggest? boolean_flag ? do_this() : do_that(); It seems
by Mithaldu 9y ago
Are you saying this suggests it does something different from what it does? What does it suggest?
boolean_flag
? do_this()
: do_that();
It seems so terse as to be impossible to misinterpret.
The only possibly feasible way i see for it doing something unexpected would be if it were the last statement in a function in a language that implicitly returns the value returned by the last statement in a function; but users of such languages usually are aware that can happen.
- emodendroket 9y agoTernaries strongly suggest "side effect-free statement being run for its return value." Perhaps a more apt analogy would be to say it's like writing a method called "getX" with side effects.
- Mithaldu 9y ago> Ternaries strongly suggest "side effect-free How do they suggest that? As far as i am aware ternaries put no restriction whatsoever on what kind of code you can put in its branches. I don't buy the comparison with getX either, since ternaries have no name, they're just some punctuation, and even if implemented as if/else word pairs are completely vague.
- TeMPOraL 9y ago> How do they suggest that? It might be a convention in some circles. I don't think it's a well-established one, though. For me, ternaries strongly suggest "expressions being run for its return value", but without any indication whether or not it's side effect-free. In practice, most of the time such expressions end up being side effect-free (getters, finders, etc.), but every now and then you do want to have a stateful expression (e.g. getting something from a map, and storing a default value there if the key is missing in the map). I believe such stateful expressions are fine to use with ternaries as well.
- emodendroket 9y agoOK, maybe I don't quite literally mean side effect-free. In that case I probably wouldn't think it's bad. What I would not expect in a ternary statement is code that, say, makes a network request, or writes to disk, or changes several different properties on the current object.
- emodendroket 9y agoOf course it's technically possible to do whatever. What I'm saying is that it violates expectations most programmers would have.
- tripzilch 9y ago> As far as i am aware ternaries put no restriction whatsoever on what kind of code you can put in its branches. You just called someone out for this: "If you're restricting what you're saying to a specific language say that bold and as the very first thing (...)" Are you talking about a specific language now? Because some languages do put restrictions on that. Python makes a clear distinction between statements and expressions, so you don't even get the option to use a ternary in place of an if/else block. That said, (in general) I disagree that ternaries suggest side-effect free. However, they do suggest that you care about the return value of the ternary expression. Some languages enforce this suggestion, others don't. But even for languages that don't, a ternary that silently discards its return value feels like a "cute hack" to me.