r/programming • u/zumpiez • Dec 22 '11
The Problem with Implicit Scoping in CoffeeScript
http://lucumr.pocoo.org/2011/12/22/implicit-scoping-in-coffeescript/
83
Upvotes
r/programming • u/zumpiez • Dec 22 '11
View all comments
Show parent comment
14
u/LaurieCheers Dec 23 '11
As OP (and apparently many other people) have eloquently shown you, code becomes very brittle if its meaning depends on the surrounding context. This IS a problem. It IS breaking people's CoffeeScript programs in ways that are hard to detect.
Also: you have not removed the conceptual complexity of declaration vs assignment, at all. You've just made it subtler, and harder to see. The only way to completely remove it would be to make all variables global. (*)
(*: Do not make all variables global.)
Don't get me wrong, I can see the benefits of the "no shadowing" rule - but this solution is worse than the disease. There are better ways to achieve it - in particular, Python's nonlocal keyword.
My solution would be to require the keyword "nonlocal" before allowing a closure to assign a nonlocal variable:
Without that keyword, the assignment will not create a local variable - it will cause an error.
Look ma, no shadowing! And yes, declaring a new variable can still change the meaning of surrounding code... but now the code will crash, instead of silently doing the wrong thing. Much better.