r/coffeescript • • Jul 26 '13

Is CoffeeScript's Scoping Madness..? (x-post from /r/programming)

/r/programming/comments/1j1cw7/coffeescripts_scoping_is_madness/
6 Upvotes

4 comments sorted by

View all comments

Show parent comment

2

u/BlitzTech Jul 27 '13

I know it's a bit late, but I'll try to answer your questions anyway:

  1. CS developers are careful about their variable names and have adopted "shallow modules" as a best practice to reduce the number of variables a developer needs to worry about. It's not too significant at one or two levels deep, but three or more and very unexpected behavior can occur depending on your choice of names.
  2. It's not the intentional shadowing that's problematic; it's when you accidentally refer to e.g. config in your local function and actually that points to the config variable you use in your whole module and suddenly assigning a value to config.doSomething affects everything in the module and not just the current function.
  3. Javascript has the var keyword that forces a variable name to be local to the current function scope, regardless of where in that function the var expression is used (this is "hoisting"). Coffeescript intentionally did away with that because it's "ugly".

In short, these kinds of issues don't often bite you, but when they do they are particularly insidious because the parts you observe to be buggy may be unrelated to the changes that caused them to break just because you chose poorly when picking variable names. I personally find the mental overhead of dealing with this and several other disambiguation issues to be more trouble than the gains in readability are worth.