I'm sure you are aware of the bug in your own compiler introduced by this very issue, mentioned here.
Your motivations for eliminating shadowing are very reasonable. The problem is that you cannot even detect whether the programmer intended to shadow a variable, since there are no mandatory variable declarations. And it's easy to "accidentally introduce intentional shadowing" by adding a function or global variable with the same name as some local variable in the same file, as in the linked bug.
CoffeeScript "forbids shadowing", but in a very strange way: it does not (cannot) reject the program, but instead silently changes the meaning to something entirely different from what the programmer intended. Add mandatory variable declarations to CoffeeScript, and suddenly you can detect shadowing (as well as many other common errors). Then if you want to reject programs that shadow, you'll hear no complaints from me.
I'm certainly aware. Like everything else, eliminating shadowing in this particular way is a tradeoff. When you're introducing new variables at the top level of the file -- something that's pretty rare, that the patch did -- you certainly have the possibility for conflicts. But shadowing also has a possibility for conflicts -- by var'ing a variable in an intermediate scope, you can break an inner scope's access to an outer variable. I've had that problem before in JavaScript.
Incoming pull requests are often slightly broken in various ways, and need to be tweaked after merging. If you think of the tradeoff of changing that variable name, versus having to go through and add var or a special assignment operator to every introduction of a variable in the entire codebase -- it's a choice I'd always be happy to repeat.
Keeping variable names descriptive and singular within a lexical scope, and removing the need to even think about "has this variable been declared in the right place", is a huge boon, in my opinion.
On a more serious note, if you wanted to eliminate shadowing, wouldn't a better strategy be to have the compiler complain if the programmer tries to shadow a variable?
I'm certainly aware. Like everything else, eliminating shadowing in this particular way is a tradeoff. When you're introducing new variables at the top level of the file -- something that's pretty rare, that the patch did -- you certainly have the possibility for conflicts. But shadowing also has a possibility for conflicts -- by var'ing a variable in an intermediate scope, you can break an inner scope's access to an outer variable.
The difference is that a compiler can detect the latter, so the cost is that you spend 1 second compiling your program, get an error and rename the conflicting variable.
When you reuse a name in CoffeeScript, the cost is a bug that can go undetected for a long time.
You haven't gained this:
Keeping variable names descriptive and singular within a lexical scope, and removing the need to even think about "has this variable been declared in the right place", is a huge boon, in my opinion.
because you can get it in a language with explicit variable declarations too! All you've gained is not typing "var", at the cost of obscure bugs.
Implicit variable declarations are a choice I hope no language designer ever makes again.
View all comments
Show parent comment
8
u/rwbarton Jul 26 '13
I'm sure you are aware of the bug in your own compiler introduced by this very issue, mentioned here.
Your motivations for eliminating shadowing are very reasonable. The problem is that you cannot even detect whether the programmer intended to shadow a variable, since there are no mandatory variable declarations. And it's easy to "accidentally introduce intentional shadowing" by adding a function or global variable with the same name as some local variable in the same file, as in the linked bug.
CoffeeScript "forbids shadowing", but in a very strange way: it does not (cannot) reject the program, but instead silently changes the meaning to something entirely different from what the programmer intended. Add mandatory variable declarations to CoffeeScript, and suddenly you can detect shadowing (as well as many other common errors). Then if you want to reject programs that shadow, you'll hear no complaints from me.