3 ms·
It's not about that specific code - it's about in general having side effects inline in an expression. I can't recall perfectly from the top of my head the rul
by chrisseaton 8y ago
It's not about that specific code - it's about in general having side effects inline in an expression.
I can't recall perfectly from the top of my head the rules for the ordering of inline side effects in general in C and Java, and I work in the VM research group at Oracle. Can you? Are you sure about that?
- watwut 8y agoindex++ uses the variable and then increments it. From top of head, no doubt and 100% sure. ++index would do the opposite order. It is easy to remember and makes sense. The ++ operator is not obscure at all and used more often then for example += operator.
- chrisseaton 8y agoI've said 'in general' several times. I shy away from inline side effects because in general I do not intuitively understand how they sequence. I'd rather just avoid in general.
- Retra 8y agoThat's your prerogative then, isn't it?
- chrisseaton 8y agoWhat does the JVM language specification say about this? That's a pretty universal opinion we can all agree to follow. 15.7, Evaluation Order: > It is recommended that code not rely crucially on this specification. Code is usually clearer when each expression contains at most one side effect, as its outermost operation, and when code does not depend on exactly which exception arises as a consequence of the left-to-right evaluation of expressions. So when I review I advise, as per the spec, that expressions contain a single side effect.
- deleted 8y ago[deleted]
- r-w 8y agoI’m not really sure what you mean by “the rules for the ordering of inline side effects”. I think they’re usually relatively simple to understand, such as with postfix and prefix incrementation operators; but I’m sure there are other cases I’m not aware of. Care to expand on these?
- chrisseaton 8y agoIs everyone deliberately ignoring the bit where I said I was talking about inline side effects and how they sequence in general and how it wasn’t about this specific code? Inline side effects are subtle and confusing. Best to avoid in general in my opinion. Edit: You’ve completely changed your comment out from underneath me after I replied. That’s rude. To answer your new question - you're not alone in not know what I mean by sequencing of side effects! Not many people understand the rules for sequencing side effects! That's my point it's subtle. The JVMLS gives you the rules, but it also says > It is recommended that code not rely crucially on this specification. Code is usually clearer when each expression contains at most one side effect, as its outermost operation, and when code does not depend on exactly which exception arises as a consequence of the left-to-right evaluation of expressions. I'm just following that advice! For C it's worse - what order are two side effects run in a C expression? Usually the rule is 'it's up to the compiler - it can do them in any order it wants.'
- Dylan16807 8y agoThe ordering/sequencing of side effects is not the problem here. It doesn't matter at all whether index updates before data, or data updates before index. Few people know, and even fewer need to know. What you need here is just knowing what "index++" means, nothing about sequencing at all. I could understand your objection with the original version of the code, where it referred to both "i++" and "i" in a single line. That depends on sequencing rules and is a bad idea to allow. But this version, that only refers to "index++" once, doesn't have any subtleties.
- chrisseaton 8y agoMy rule is really simple to explain to a junior - "don't use any inline side effects." How do you explain your rule to a junior? You're going to have to tell them that inline side effects are fine, as long as you follow another set of rules about sequencing - a set of rules you admit most people don't know at all and fewer people understand correctly!