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
1
u/jashkenas Dec 23 '11
... perhaps different to the compiler, but not visibly different to the reader. When I write
x = 1, I'm saying that the value of x right now is 1, and everything in the current scope and below this point can access that value. This holds whether or not this instance is the first time thatxhas appeared in the program, or if it's already been used at a higher level.At the moment, yes. I have ambitions to make this a compile-time error ... even though I doubt it will go over well ;) That said, the unfortunate nature of shadowing parameters doesn't change the overall goal: Just because function parameters can shadow doesn't mean that they should. You're still rendering forever inaccessible a useful local variable from an outer scope, and you're still giving
xtwo different meanings within the same lexical scope. You should probably pick a better name for your parameter.Yes. By forbidding shadowing, CoffeeScript isn't optimizing for cut-and-paste programming. Significant whitespace in general doesn't optimize for cut-and-paste programming either. Patterns that do optimize for cut-and-paste programming tend to favor local isolation -- Instead, we're aiming for the holistic readability of the code.
I agree with that sentiment, but I'm not sure that the current approach is any less explicit. Different, sure -- implicit, not so much. Within a given file, all variable scopes are perfectly explicit: as you read, each variable is local to the scope where it was first introduced.