Hacker News new | past | comments | ask | show | jobs | submit login

What's wrong with those compiled loops?

The only difference from a "hand-written" loop is that you would use `i` and `ln` instead of `_i` and `_len`, and something in context like `numbers` instead of `_results`.




There are a total of 9 references to the `_vars`. I think line noise in the compiled javascript is a legitimate counter argument to the claim that its strait forward and readable.


12 if you count the declaration. You'd have exactly the same doing it yourself, you can at most reduce it to 10 by declaring i and ln inside for().

You're also overlooking the fact that this can be considered an advantage: variable naming for these transient uses is very consistent. When writing JS you could have names all over, even a different one for every loop.


I've maintained the hoisted index variable and cached the length:

    var i, foods = ['toast', 'cheese', 'wine'], foods_length = foods.length;
    
    for (i = 0; i < foods_length; i++) {
      eat(food[i]);
    }
Prefixing variables with an underscore is not an advantage in readability because it lends nothing to the description of what the variable represents. Its another glyph that you have to visually parse before reaching possible meaning.


I didn't say the underscore is good - it's a necessary evil.

I said consistency is good. And you just proved my point.


The reason why all declarations happen at the top of the current function body, instead of inline with the first assignment is because in JavaScript assignment is a statement, not an expression. You can't do this:

    alert(var one = 2);
In CoffeeScript, (nearly) everything is an expression. So you need to be able to do this:

    alert one = 2
By pushing up all the var declarations, all assignments can be themselves assigned, returned, or passed as arguments to function calls directly.


Aren't local variables scoped to be visible within the entire JavaScript function? I thought the recommendation to declare local variables at the top the function was a reminder (or to avoid accidental importing of global variables of the same name)?




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: