4 ms·
> Would easily pass any reasonable code review. It's not that smelly or unusual. I wouldn't pass that in a code review without comment! I'd ask the author to
by chrisseaton 8y ago
> Would easily pass any reasonable code review. It's not that smelly or unusual.
I wouldn't pass that in a code review without comment!
I'd ask the author to consider breaking down the two actions into two separate lines so that it's more clear that there are two actions and what the sequential ordering of them is.
Inline side effects like that are a mess. Without looking it up, I could not even tell you if the sequence point ordering of that kind of expression in general is defined in C!
- watwut 8y agoIn Java, the sequence of point ordering is remarkably clear and I would expect professional to understand it. I tend t be conservative in syntax and avoid less known parts, but this part is well known.
- deleted 8y ago[deleted]
- chrisseaton 8y agoWhy write code that takes a professional to understand, when you can easily write code that anyone can understand?
- watwut 8y agoYou dont need to be professional to understand that code. It is not obscure what it means. You need someone who knows what ++ operator means and that is usually lesson 1 or 2 in Java for complete beginners class. If you never ever worked with Java and coded in C, then it might be different. Typical Java programmer, even weak one, is fluent in Java 101, but does not really know what might confuse C programmer. In any case, I would expect C programmer finding job as Java programmer for some reason to simply ask someone in office.
- chrisseaton 8y agoIt'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]
- projectileboy 8y agoBecause, although it’s only one of many factors to consider, brevity matters.
- whatshisface 8y agoIf you're writing Java, brevity is already out of the window, just on the grounds of how namespaces work and the library/framework naming conventions.
- romwell 8y agoI'm firmly in the clarity over brevity camp[1] If you think brevity means clarity, try reading some old Fortran code with comments stripped out and single-letter variable names like i, j, k, b, g, h, m, p, q, etc. (I say Fortran because in that era, such variable names were much more in vogue to save a couple of bytes here and there - but really, this can be done in any language). Even simple algorithms (like finding the center of the circle through three given points) become puzzles. But sure, they are brief. [1]https://signalvnoise.com/posts/3250-clarity-over-brevity-in-variable-and-method-names https://signalvnoise.com/posts/3250-clarity-over-brevity-in-...
- projectileboy 8y agoI don’t necessarily disagree. But I do think that all other things being equal, less code is better than more code - fewer bugs, easier to understand. The catch is what falls into “all other things being equal”. Where I’ve been frustrated with others is when they reject brevity in situations where it’s idiomatic for the language (e.g., ‘it’ as an implicit variable in Groovy list comprehensions that aren’t especially complicated). Forgive me if I sound crazy; maybe it’s just a reaction to having spent so many years in the Java community, where no name is long enough, and no amount of code is too much.
- romwell 8y agoTrue, the world could do with less code that looks like Fizz Buzz Enterprise Edition :) In this case, though, the OP makes a strong point that constructs like data[index++] = value; aren't idiomatic to Java - even the spec advises against this. And in any case, side effects in assignments can be a tricky thing to understand. Sometimes they are necessary, as in status = perform_computation_on_data(some_mutable_data); But in the example discussed is not in this vein. I'd say it's not pretty for the same reason as cramming two statements on one line: data[i]=value; i++; is not pretty: it's trying to do two things with one line, which kind of defeats the point of breaking up code into lines. So I don't disagree with you - but there's a happy medium between Fizz Buzz Enterprise and Code Golf - style code :)
- caf 8y agoSequence point ordering is not relevant to the example code (assuming of course that there are no macros involved here): data[index++] += 'a'; All that matter is what the ++ operator evaluates to - the sequencing of the side-effect modification to 'index' is irrelevant. The ++ operator evaluates to the value of 'index' before the increment is applied, so we know that the is array location which will be modified. As it happens, in C the modifications to the location in the 'data' array and the 'index' variable are not sequenced relative to each other, but that is not observable.
- kllrnohj 8y ago> Inline side effects like that are a mess. Without looking it up, I could not even tell you if the sequence point ordering of that kind of expression in general is defined in C! What are you calling "inline side effects" here? The ++ operator or the += operator? Because the index-of a ++ operator is extremely well defined. In fact that code snippet works in every language with a ++ operator exactly as you'd expect, there's really no tricks in play here. Drop that code into C, C++, Java, JavaScript, etc... and it does the exact same thing in all of them via well-defined rules. It will append 'a' to data[index] then increment index. We're not even into the wierd land of mixing bitwise & boolean operators on the same line where you operator precedence becomes non-obvious.
- adrianratnapala 8y ago> It will append 'a' to data[index] then increment index. Of course, you can always do stupid stuff to make life difficult. In C++ int data[100]; memset(data, 0, sizeof(data)); int &index = data[0]; data[index++] = 'a'; Which cell is the ++ applied to, data[0] or data[97]?