3 ms·
It might sound easy to fix just a little detail that disturbs you and keep the rest as it is, but it's rarely feasible. If we ban side effects from expressions
by teo_zero 7mo ago
It might sound easy to fix just a little detail that disturbs you and keep the rest as it is, but it's rarely feasible.
If we ban side effects from expressions, we must change the "for" syntax too, because the third "argument" currently is an expression. It should be a statement instead. Let's see...
for (int i=0; i<n; ++i) // old
for (int i=0; i<n; i+=1;) // new
What about the typical 2-variable loop?
for (int i=0,j=n; i<j; ++i,--j) // old
for (int i=0,j=n; i<j; {i+=1;j-=1;}) // maybe?
Or we simply forbid all this and force the increment and decrement in the body:
for (int i=0,j=n; i<j; ;) {
...
i+=1;
j-=1;
}
This is throwing the baby out with the bathwater!
And finally, if the third argument is a statement, would a break or continue be accepted there as well? A nested for? Sure, these are examples of abusing the syntax, but so is using x and ++x in the same expression.
My conclusion is that some constructs are so powerful that they can both simplify our code and make it unreadable. The choice how to use them is ours.
- amavect 7mo agoThe author agrees with you. In the final paragraph, the author says that they allow ++ and -- when not using the value, as such use poses no harm. Also, you can use a comma with += and -=, for (int i=0, j=n; i<j; i+=1, j-=1) but keep in mind the decrement must usually happen before the loop body (post-increment reverses to pre-decrement). for (int i=0, j=n; i<j; i+=1) { j-=1; ... } for (int i=0, j=n; i<j; i++) { j--; ... }