Rethinking JavaScript for-loops
javascriptweblog.wordpress.com
javascriptweblog.wordpress.com
But the point of the article was: now that for-loops are gaining obsolescence as conventional array iterators, what can we use them for (and how do we have fun with them). A bit like a "what to do with those old LPs" discussion - except I still love listening to LPs :-)
For-loops are used in really limited ways, but they should be used much more often. Programmer should be better versed in how for loops work, the fact that you can add multiple expressions between each semi-color (;), etc. The reason is, for-loops are the most readable constructs for looping. Here's why:
For-loops are the one loop construct which give you all the information about the looping at once. Used smartly, you can read the first line of the for-loop, and discover everything that the programmer would like to do, that has to do with the looping.
For example, adding two variables to be incremented in a for-loop is trivial: for (int i=0, int b=10; i==10; i++,b++)
This can also be done outside of the for-loop declaration, e.g. as the first line inside the for-loop. However, for smart programmers, this signals, to me, that "b" is another variable having to do with the loop that I would like incremented each time. In other words, it gives me more info.
Other good uses can include checking two conditions, checking conditions with side-effects (e.g. instead of i++, use word=read_word_from_file()), etc. Lots of people are uncomfortable with these in a for-loop, but I maintain that's the best construct for these kinds of things, since it gives you all the info you need to understand the loop at one point only.
Especially in JavaScript. Please stop using for loops in favour of asynchronous recursion (or similar). Thank you.
This is what I've been using lately, pretty neat: http://github.com/caolan/async
It wasn't supposed to be taken too seriously.
BTW for-loops are not exactly the same in C, because JavaScript has no block scope.
should the next programmer (usually =you+T) see the loop and think "oh, a loop" or "oh, a loop. and it uses the assignment of the each var to the conditional block... great, now i will have to go through the data we receive to make sure no element get's a value of zero"
Just fell like it's important to state that one shouldn't use "fun and learning" techniques in a production environment.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." – Brian W. Kernighan
It makes a difference in the IEs. I don't remember it making much of a difference one way or the other with the JITted engines, though if the ES harmony proxies become widespread, there's a reasonable chance it'll become best practice again.
I remember seeing a test that noted a visible difference between the two. Its arguable, however, if it makes a significant difference in the grand scheme of things...
var ids = [story.id for (story in stories)]$.each(new Array(3), function(i){ alert(i); });