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
0
u/showellshowell Dec 23 '11 edited Dec 23 '11
Having one function per .coffee file is not the strategy I'm proposing.
First, you can use classes to greatly reduce the number of functions at top level scope. Now, sure, the class name itself will be at top level scope, but if you follow the convention of capitalizing the first letter of the class, then it won't collide with lowercase variables within functions.
If you do have multiple functions at top level scope, then you can largely mitigate naming collisions by giving them descriptive verb-like names, such as compile_source.
In cases where you want brevity in top-level-scoped variables, such as "user", I've already addressed the question in my responses to @bobindashadows. Long story short, use the search feature in your editor.
I've also addressed mitsuhiko's particular bug in this discussion. He ignored a best practice in CoffeeScript (and many other languages), which is to avoid leaking short names like "log" and "tan" into your top level scope. If we had simply used Math.log, we wouldn't even be having this discussion.
Finally, you make it sounds likes impossible to avoid the problem of accidental name collisions, when in practice people manage to write working code all the time.