Is it a bad habit to write reverse loops as:
for (i = N; i--;)
in order to access (N-1) to 0
If so, why? jsLint certainly doesn't like it.
Is it a bad habit to write reverse loops as:
for (i = N; i--;)
in order to access (N-1) to 0
If so, why? jsLint certainly doesn't like it.
Share Improve this question asked Feb 3, 2012 at 21:12 SinethetaSinetheta 9,4495 gold badges32 silver badges52 bronze badges 17-
Your code won't work. It should be
for (i = N; i > 0; i--);
. I don't know why you would do this, though.N
will still be its original value.i
will be 0 by the end. I'm not sure what you are trying to achieve – benekastah Commented Feb 3, 2012 at 21:16 - Readability is the only reason that immediately springs to mind. – josh3736 Commented Feb 3, 2012 at 21:16
-
Another more mon syntax for reverse loops is
while(i--)
– David Hellsing Commented Feb 3, 2012 at 21:17 -
5
@benekastah: It certainly does work. Remember that the second expression in a
for
statement determines whether or not the loop continues; wheni
reaches0
, the second expression will be falsy, so the loop stops. (Also, benekastah's ment inadvertently validates my ment that this isn't readable.) – josh3736 Commented Feb 3, 2012 at 21:19 - 1 @benekastah oh it certainly works jsfiddle/sU3HL it just does the decrement during the "condition" instead of using a final-expression. – Sinetheta Commented Feb 3, 2012 at 21:21
6 Answers
Reset to default 6There's no technical reason that this won't work. However, it clearly has readability issues since someone had an immediate "well that won't work!" reaction.
This is the kind of issue that the jQuery team struggles with – whether to use novel constructs that save bytes at the expense of clarity and maintainability. It really es down to whether it's worth 1 or 3 bytes of savings:
for(var i=9;i--;)
var i=9;while(i--)
for(var i=9;i>0;i--)
In this case, probably not.
It is less readable, without your explanation it would take me few seconds to understand what the loop does. Why not simply:
while(i-- > 0)
?
Did you consider readability? You may very well understand it yourself, but other developers might get confused since the parts of the for
"idiom" are usually named as:
for ([initialization]; [condition]; [final-expression])
While the condition can technically be any expression, your version does not conform to this idiom, since the "condition" part you use does more than just defining a condition - it sneakily decrements i
as well.
"Is it a bad habit to write reverse loops as:"
for (i = N; i--;)
That's a matter of opinion, but it's effectively a reverse while
with initialization, so in my opinion it's not a "bad habit". It's just a coding style.
The specification makes the parts of a for
optional to give the developer that flexibility.
"jsLint certainly doesn't like it."
Who cares. You're not bound to follow the opinions of jsLint.
The answers would be very subjective I guess. I don't think it is a bad habit but I do find it aesthetically unpleasing. This can be expressed more elegantly as:
for (i = N - 1; i >= 0; i--) {
// do something here.
}
// And if it is really important that i should be 0 here
// as it is in your original code.
i = 0
This code is easier on our brain while browsing a lot of code that happens to contain this.
Yes. There is a good reason. Readability.