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
32
u/rabidcow Dec 22 '11
Yeah, when I read in the language docs/intro that the solution to this "oh, you can accidentally break your code in a very hard to debug way" was "be careful", that's when I decided that I would not be using it. I'm sorry, but part of the job of a good programming language is to prevent me from unintentionally doing stupid things, especially things that would lead to a lot of time wasted debugging.
9
u/[deleted] Dec 23 '11
One of the thing that really miffs me about CoffeeScript is that it fails to appreciate the deep synergy between blocks and lexical scoping and the dominance property. Proper lexical scoping prevent all sorts of silliness (like using variables before defining it). It means that for every variable use there is a lexically apparent definition, and you can figure out which one it is just by scanning upward and looking at 'let' forms.
4
u/contantofaz Dec 22 '11
Dart does it differently in a number of ways, even though it also compiles down to JavaScript.
Having to declare variables with an introducing "var" statement is good for the most part. It's bad in that it makes using variables a little more verbose.
4
u/ef4 Dec 23 '11
Bound names are a scarce resource. Not because you'll run out of them, but because the reader can only keep a small number of them in mind at a time.
If you have enough names in scope that you're accidentally reusing them, you're already in trouble, whether the scoping rules help you compound the problem or not.
And the data flow in this function is way too byzantine for my taste
I'm not at all surprised that code like this leads to subtle bugs, no matter what scoping rules are in play.
17
u/jashkenas Dec 22 '11
For a bit of background on why this is the way it is, check out these two tickets:
https://github.com/jashkenas/coffee-script/issues/238
https://github.com/jashkenas/coffee-script/issues/712
To summarize, in brief:
By removing "var" from CoffeeScript, and having variables automatically be scoped to the closest function, we both remove a large amount of conceptual complexity (declaration vs. assignment, shadowing), and gain referential transparency -- where every place you see the variable "x" in a given lexical scope, you always know that it refers to the same thing.
The downside is what Armin says: You can't use the same name in the same lexical scope to refer to two different things.
I think it's very much a tradeoff worth making for CoffeeScript.
3
u/[deleted] Dec 23 '11
CoffeeScript furthers devolves into Perl. Now without use strict.
3
u/[deleted] Dec 22 '11
This is the reason I refuse to learn CoffeeScript.
2
u/inmatarian Dec 22 '11
It's worth mentioning that in Lua, everything is considered global by default, unless marked as a local variable. So, to extend the example:
Also, any executing code is considered to be a running function, you could issue a local declaration at the global level, and have it not stored in the global environment, but rather it remains local to the file.
My only gripe is that
localis a pain to type over and over. I would have preferred eithervarormy.1
u/showellshowell Dec 23 '11
I hope this gist helps folks to understand how CoffeeScript scoping actually works, even if you disagree with Jeremy's design decisions:
https://gist.github.com/1512100
1
u/tailcalled Dec 23 '11
Or you could just not have assignment.
1
u/crusoe Dec 22 '11
Uhm, python is the odd language out, requiring a special keyword to effectively enable a closure that captures a variable in a outside scope.
In languages with the notion of variable creation seperate from assignment, this isn't even an issue.
-2
u/dfltr Dec 23 '11
This is exactly the kind of situation I was worried about when I first heard about CoffeeScript.
"We think it's better for you to do it this way." That's great, good for you, but you are not the ECMAScript working group. Do not change the way the language works.
-4
u/shevegen Dec 23 '11
I actually think Python 3 is doing it wrong and CoffeeScript got that right.
Is it only me or is Python slowly entering a decline state on its own? It is still more readable than Perl 5, but with keyword crap like "nonlocal" my old adage that python is so readable is slowly beginning to fade. Perl 6 would then be as readable as python...