Shifting responsibility like that is one of my biggest peeves in programming. Everyone wants things to be someone else's problem. Sometime that's good (library reuse means one really good wheel that doesn't get needlessly reinvented) but also means the people best positioned to address things don't have the understanding and/or desire to be bothered.
Yes, better style and testing lead to better code. BUT all three - style, testing and language features - would lead to even better code all around. I'm not saying there isn't good justification for not handling scoping better in cs, but this is not it.
The "use TDD" one really gets under my skin because it leans towards the road where any problems your project faces can be hand-waved away by for not being a true enough Agile Scotsman.
Because it's not a bug. It's not like he's stupid and can't figure out how to change it. It was intentional. He likes it that way. If you don't, use something else.
My biggest objection is your use of the word "fix". It's not a bug, since jashkenas has stated repeatedly that it's intentional and in his mind it prevents more bugs (where people shadowed a variable by accident) than it might cause (where people wanted to shadow a variable, and didn't, or accidentally would have shadowed a variable in JS, but didn't in CoffeeScript). Feel free to disagree, but unless you've got some statistically sound study, preferably peer-reviewed, then it's obnoxious to still be clamoring for him to "fix" it. Honestly, even if you phrased it as "change" instead of "fix", it would be slightly annoying, since it's well-documented by now that it's not going to happen, but at least you aren't implying the superiority of your design aesthetic over his when he's got a strong track record of creating technically impressive Open Source projects that thousands of developers find useful. One you almost certainly (unless you're secretly DHH or John Resig or something) can't claim to match.
NOTE: I probably wouldn't have done it the CoffeeScript way (I'd force people to actually declare their variables—shocking, I know—and scope it from declaration to the end of the closest enclosing block, but that's not really relevant to this discussion). That said: I admit he has a point. For the first class of potential bugs: shadowing variable names is just a bad, error-prone idea. For the second class of potential bugs: if your code is nested deeply enough you can't spot the problem, you need to re-organize your code, because you've probably got other, harder-to-see bugs.
Coco still has some of its own problems, and I think that exposing Javascript's == operator is a terrible idea.
Other CoffeeScript-like languages that may be better include LiveScript (which is itself based on Coco, but with a lot more features) and GorillaScript. If I start a new project involving a substantial amount of JS I'll probably end up using one of them (or at least try using one of them).
Using Livescript for over a year now, never going back. It uses shadow-by-default with := operator for explicitly assigning to outer scopes. Prevents accidental clobbering and encourages purity and functional thinking. Recommend.
It's interesting to read through the ticket. Obviously the maintainers are opinionated (and that's their right of course), but still reading it, it reminds me of some posts from lolphp. The php apologists in their bug tracker, etc, making crazy excuses for a broken language. "Doctor, it hurts when I do this," and the doc then says "Don't do that." Not that I think cs is anything like php. cs looks quite nice, but I'm also weary of YAL (yet another language).
And I guess this maybe shows an issue with using a language approach such as with coffeescript, which is essentially surface sugar for js. That you still need to maintain rigorous programming methodologies and approaches, not just to aid you in creating the program, but also to guard against the pitfalls of the language, which is really the most laborious and rotten part of browser side javascript today.
Or maybe cs removes the other 90% of pitfalls from js, and it just has this one, I still think I'll stick with straight js (15 years of it tends to get on your backbone), and if I had a choice to make, I'd probably go with typescript or dart.
To be fair, CoffeeScript most likely had an effect on the design of Dart, Typescript, and the upcoming versions of JS.
IIRC, C#'s lambda expression predate CS's by 2 years and they're almost identical (the biggest difference is that CS has two different "arrows" for capturing the current this or not). And Scala — which again has almost identical lambda syntax) predates C# 3.0 by ~4 more years.
And its syntax looks a lot (though it's not identical to) Haskell's, whose 1.0 definition predates Scala by 13 years.
CoffeeScript (2009): (x, y) -> expr(x, y) or (x, y) => expr(x, y) C# 3.0 (2007): (x, y) => expr(x, y) Scala (2003): (x, y) => expr(x, y) (also shorthand along the lines of expr(_, _), not verified) Haskell (1990): \x y -> expr x y
What definition of "scope" is being used here? It seems obvious that an exception should be thrown if a variable that is not in scope is read. And how is err not in scope?
My first impression is that they are departing a bit too much from CS syntax (e.g. making () -> invalid). But it does seem like an interesting project.
As someone who programs in Javascript for a living but has never written a single line of CoffeeScript, why is this a problem? I don't get what the actual complaint is.
Is the complaint that it is non-obvious that the closure around the test function includes the var y defined outside the function's scope?
I use outside scoped variables all the time to pass information down through async chains. I also don't use generic variable names like 'y'.
The "local" in coffeescript is a "do" function. For example:
x = 1
y = 2
g = ->
x + y
do (y = null) ->
h = ->
x + y
compiles to...
var g, x, y;
x = 1;
y = 2;
g = function() {
return x + y;
};
(function(y) {
var h;
return h = function() {
return x + y;
};
})(null);
If I had left out the y argument for the do block (or forgotten to assign it something), it would have used the global version. Take it or leave it, it is possible to handle scoping this way.
The most important thing is that all variable declarations are scoped to the file. There are no global scoped variables; if you want to export something, you need to attach it to the window/exports/whatever global object you have available. The idea is that if you have a file with multiple scopes using different variables with the same name, your file is too big and you should separate concerns. Do blocks should very rarely be necessary.
A similar argument comes up with coffeescript's use/non-use of parentheses, which can be confusing when trying to figure out whether you need them or not. The problem usually stems from people writing confusing one liners, which end up being hard to read. The solution is to write coffee in such a way that every single line is concise and simple. Other than chaining calls, I've never written a line where I absolutely needed parentheses. If I did, it made sense to split the line in two.
Basically, all of the issues with coffeescript are solved by writing short, concise module files with short, concise lines of code. Devs who want to write sprawling files with big one liners won't like coffeescript.
With the extremely important caveat that in Ruby standard variables can never cross method boundaries. You need a constant, a global or a class variable for that, which are all syntactically different and live in their own namespaces.
The CoffeeScript developers seem to have modeled all functions in JavaScript after blocks in Ruby. There was no need for additional syntactic distinctions.
Generally "at work" I avoid CoffeeScript, primarily for the reason I still work with a lot of folks that don't understand JavaScript... but in general, I find it's good to familiarize yourself with the JS output of CS, then just treat CS like shorthand for JS.
... in otherwords, when I'm using CS I'm still thinking in JS.
I don't at all recommend CoffeeScript to anyone that isn't already relatively skilled in JavaScript.
While I agree with the title of this post, in the process of writing ~20k lines of CoffeeScript it hasn't actually ever bitten me, unlike some other problems with the language. Avoiding deeply nested scopes (and having too many things in scope in general) makes it easy to avoid issues, and IMO that's a good idea even in languages with sane scoping anyway.
it bit me all the time before I figured out what was happening. also it's bad design because it makes it way too easy to smash globals like module and function names. you do learn to avoid it though, and otherwise, CoffeeScript is great. The function syntax is a must for a language like JS too.
Absolutely, but harmony was actually influenced by C# and coffeescript. It'll be great when it's widely supported. Definitely a big step in the right direction.
I'm not sure what you mean? Do you mean that in coffeescript the () after a function are optional? I find that it helps with readability with nested functions and callbacks, although it has bitten me before, but for me the readability outweighs the cons.
Also, javascript does have partial application with the bind method, or you can use function scoping to do it yourself:
var foo = function (x, y) {...}
var bar = function (y) {
return foo(5, y);
}
Limiting it to just things that have actually caused problems rather than merely irking me:
The syntax makes it too easy to forget the () on function calls. This is a problem I don't think I've ever had in any other language, but I've done it a few times in CS and seen others do it as well. Bare super being a function call probably contributes to this.
Trying to cram for and map into a single thing, as covered in that thread. Even primarily expression-based languages (Scheme being the one I have the most experience with) still generally keep them separated for a reason.
Deindenting a different distance than you indented is legal and "works". Pops up rarely (usually due to c&p), but it can have really confusing results. Thankfully coffeelint can check for this.
On the whole I'm quite a fan of CoffeeScript as it's one of the few languages I've used that feels like being pleasant to use was actually a goal of the language, but it still definitely frustrates me at times.
Why not build that into the compiler? The whole point of a compiled language, in my opinion, is the guarantee that, if it compiles, it's highly likely to be correct. Why do I need a separate tool to do basic checks like that?
You're right that the bare super keyword is a bit of an odd-duck ... it exists because the 90% use-case for super is to forward all of the arguments that your overridden function received. Currently, you just write:
myFunc: ->
super
... but if we didn't have that, you'd have to do something more like this all the time:
myFunc: ->
super.apply(this, arguments)
Having loops that function as loops when you use them as loops, and function as comprehensions when you try to use their value, is a pretty core feature. It's a nice conceptual simplification, and if we removed it, then the story would be: everything's-an-expression-except-for-loops, which would be a bit sad.
You're right about different-distance dedenting. I think the reason why it's valid is to support use-cases like this:
object.method a, b,
c, d
... but we could probably do better to detect cases that are obviously incorrect, and flag them as syntax errors. Feel free to open a ticket if you'd like to get this rolling.
I disagree. The rule that implicit parentheses (generally) follow, is that they extend to the end of the line, or to the end of the block that ends the line. To riff on your examples, it allows for uses like this:
print message or defaultMessage
bank.deposit currentBalance + incomingCheck
... and if you need tighter binding, you simply write the parens:
total = bank.lookup("checking") + bank.lookup("savings")
This is not possible now in CoffeeScript
Oh, but it is ;) If you don't mind the new line, block strings gobble-up all of the left-hand indentation for you: http://coffeescript.org/#strings
The thing I sometimes run into in CS is that defining a lambda as the first argument of a function when there are more than one argument does only work if you put the thing in brackets:
The rule that implicit parentheses (generally) follow, is that they extend to the end of the line, or to the end of the block that ends the line.
Hmm, good point. However, that encourages what I see as 'floating arguments'. Extending to the end of the line is a good idea if it doesn't meet an operator, like the example on the front page:
console.log sys.inspect object
Furthermore, I think it would be good to encourage wrapping a long argument with operators in parenthesis.
If you don't mind the new line
That's the problem :)
A common example is a long URL which are sensitive to new lines, extra spaces, etc. I think the current implementation of double quoted, multiline strings is very limited as it only works properly in the outermost scope, otherwise it doesn't respect indentation, making the code look out of place (and interfering with Vim's folding ;) ).
Is there a way to disable implicit parenthese entirely? I like a lot of what's in CoffeeScript but that class of behaviour has lead to bugs, usually subtle, in every language where I've encountered it.
... but if we didn't have that, you'd have to do something more like this all the time:
myFunc: -> super.apply(this, arguments)
That could be solved with more generalized sugar along the lines of Python's super(*arguments), which would eliminate the need for the unusual special-case and be applicable elsewhere.
I'm not sure that super is actually the cause of my issues here though. Probably a bigger factor is that I'm a fan of the following syntax, which makes me overly used to seeing function names with nothing after them for calls:
obj.foo
name: 'a'
className: 'btn'
Having loops that function as loops when you use them as loops, and function as comprehensions when you try to use their value, is a pretty core feature. It's a nice conceptual simplification, and if we removed it, then the story would be: everything's-an-expression-except-for-loops, which would be a bit sad.
I disagree about it actually being a simplification, since I see "loop over a list of items and trigger side effects" and "transform a list of values into a different list of values" as fundamentally different algorithms, and deciding which algorithm to use is outside the scope of what a compiler should be doing.
Probably not worth writing more words on this unless this is somehow a novel argument that you haven't seen.
You're right about different-distance dedenting. I think the reason why it's valid is to support use-cases like this:
object.method a, b, c, d
That merely requires variable-width indenting. The case that's problematic is the following:
a = ->
b
c: d
e
f
g
I can't really imagine any reason why one would want to do this intentionally, and a, f, and g all being treated as the same indentation level is really weird. Python solves this by requiring that dedents return to a previously used indentation level, which makes e an error without breaking the above sensible formatting.
everything's-an-expression-except-for-loops, which would be a bit sad.
It could still be an expression, just one that always evaluated to undefined. The return expression doesn't have a meaningful evaluated value either, for that matter. (Which is natural, of course, since there would be no way to capture it.)
I'm with you on this one; this seems like a big deal until you write some real coffeescript and realize that your scopes are so small most of the time you never have to worry about what's declared in a parent scope.
I don't think I've ever had a bug due to this in the ~12k lines of coffeescript in our app. In fact more often than not I end up with too narrow a scope given that coffeescript inserts closures quite aggressively.
Recently I wrote a server that totals about ~1k of code. I didn't get bit by this, largely because a) single author and b) I have very few things declared at the top level; most of my code is in classes.
OTOH, the number of bugs I wrote that would have never gotten past a typechecker was ridiculous.
IMHO that's the biggest challenge by far when doing large-scale CoffeeScript programming, and it's the same challenge you have in JavaScript too. In isolation, it might be infuriating that CoffeeScript has this one bugaboo that Javascript doesn't have, but if you step back and look at the big picture, there's so much shit you have to deal with in either language that in comparison this issue is pretty inconsequential indeed.
I love Typescript for adding typechecking (and some ES6 goodies) to Javascript. It regularly saves me from a lot of bugs. I'm considering moving some of my existing codebases to Typescript.
In CoffeeScript, 'for' is an expression that returns a value, not a statement. Also in CoffeeScript, the value of the last expression of a function is its return value (no need to explicitly say 'return').
Having a piece of code compile into dramatically different things depending on whether or not it's the last expression of the function is pretty crazy.
Things which individually are perfectly sensible combing into a pretty undesirable end result is a classic indicator of a language that's just a collection of features with no overarching design.
In fact, it's an integral part of the design. A lot of effort is present in the compiler to make is possible to turn things which would normally be statements in JavaScript, into expressions -- but in order to do so optimally, only when you actually use them as expressions.
If you haven't played around with it already, try this:
switch letter
when "A" then parseA()
when "B" then parseB()
Versus this:
result = switch letter
when "A" then parseA()
when "B" then parseB()
... there are many more examples. if/else, try/catch, first-time variable assignment ...
What you're saying is orthogonal to whether return is implicit. I think the point is that with your example, the intent is clear and is specified solely in that line of code. Where as with return, there's no syntactic difference in the line of code, it's done entirely by position. So it's not obvious which version of a language construct you're asking for. This is even worse when you consider that commenting out a line of code can change the meaning of a seemingly unrelated line of code (commenting out the last line of a function potentially changes the line before's meaning).
Variable scoping seems like a similar issue. With both things, you need to consider a larger scope of code to understand the meaning of something. Whereas other languages could encode that meaning in a more local way. What I find odd (and quite interesting) is that this complexity of understanding comes from the pursuit of simplicity in the syntax. I think it shows that simple syntax does not mean simple semantics, and the two may even be opposed in some situations.
Having a piece of code compile into dramatically different things depending on whether or not it's the last expression of the function is pretty crazy.
Having "-> 3" compile to "function() { return 3; }" rather than "function() { 3; }" is not "dramatically different".
If you disagree, then suppose you will find most functional languages to be "pretty crazy" according to that standard.
foo = ->
for i in [0..3]
doStuffWith i
bar = ->
for i in [0..3]
doStuffWith i
otherFunction()
compiles to
var bar, foo;
foo = function() {
var i, _i, _results;
_results = [];
for (i = _i = 0; _i <= 3; i = ++_i) {
_results.push(doStuffWith(i));
}
return _results;
};
bar = function() {
var i, _i;
for (i = _i = 0; _i <= 3; i = ++_i) {
doStuffWith(i);
}
return otherFunction();
};
The for loop here is compiling to things that differ in more ways than the presence of return based on whether or not it is the last expression of the function.
Yes, it's optimizing away the unused _results variable in the second example.
Hopefully you aren't denouncing every language that allows a compiler to perform dead-store optimization as being merely a jumble of features without an overarching design.
CoffeeScript is not marketed as an optimizing compiler. Quite the opposite, in fact: the homepage emphasizes that it's a very simple and straightforward translation to JS. I've had multiple people ask me questions along the lines of "why does this code get so much slower when I comment out the console.log call at the end of the function"[0] due to this, because the language supports treating for loops as if they were statements just well enough to be confusing. I think that implicit returns and expression-for-loops are individually both good things, but they combine poorly and a well-designed language would have found a way to avoid that.
[0] It's obvious enough when you look at the JS, but people new to CS often need a few nudges to get into the habit of looking at the JS whenever they have an issue.
it doesn't even make sense to me why a for loop would return a collection. It seems very bizarre that it would have those semantics (it's 'for', not 'map')
Ruby works the same, every statement is also an expression. I never saw the use with for and each() either though, it just causes problems and confusion.
I'm guessing because the cofeescript author(s) decided everything was going to be an expression even in situations where it's totally redundant or can bite you. Kind of like the example Plokryeran provided.
Now if you use coffeescript you always be aware of this behaviour. If you don't want your code to be doing needless work you have to end it with an empty return instead.
It doesn't have to be either/or. In scala, for is an expression too, but we can control its return type:
val x = Array(1,2,3)
for(i <- 0 to 2) {
x(i) = i * 2
} //returns (), which is Unit. This behaviour is analogous to C++ or java's for loops.
for(i <- 0 to 2) yield {
x(i) = i * 2
} //returns Vector((),(),())
I like rust's solution. The presence or absence of the semicolon after the last expression indicates weather the return value is the expression's result or nil. If the semicolon is left off the value is returned, otherwise the expression result is discarded.
but this quirk bit me so many times I've gone back to JS.
Agreed, this one problem is worse than all the problems that Javascript has. I felt weary from the beginning about lack of variable declarations, it biting me just showed one reason why.
The big problem is that this scoping bug is usually related to the order in which you call functions. You might already have made that mistake, you just have not seen it yet.
I was working in a 20k project, when I had it bite me twice in the same day. I started searching around the project and found at least two more broken edge cases that just hadn't been caught yet.
I have sympathy for this only because of the callback hell inherent with javascript libraries, but I still haven't run into an issue with it myself. If you are relying on this for large swaths of your code then I don't want to be on your team. If this is a serious issue then you have serious problems that aren't coffeescript.
This is really WTF. Not because of its practical significance, but because this is what I would expect of a language like bash or something. Have the creators of CoffeeScript heard of lexical scoping?
It's pretty sad, considering that ALGOL 60 is half a century old, that people are still inventing languages that play guessing games with scope. Just require every variable to be declared and these problems disappear. Is it really so hard to type "var"?
Technically (as many other languages do) CoffeeScript uses lexical scoping with implicit scope declaration. The issue is the latter, and the way it infers scope.
I don't think scope inference is a good idea to start with, but the way CoffeeScript does it is almost the worst possible way (the worst one being javascript's hey-you-did-not-declare-that-here-is-a-new-global-for-you).
Lexical scope IS shadowing. Inner scopes win over outer scopes, think of it as a scope stack. It's not shadowing, it's rather being at the top of the stack for that binding.
You know what this guarantees? When I refactor the code and I take out a var inside my JS function, I am guaranteed that I didn't break any other code. If I refactor an unknown CoffeeScript function, I have no idea. I could break your global value that you declare in another file somewhere.
I could break your global value that you declare in another file somewhere.
No. CoffeeScript compiles each file in isolation.
Lexical scope IS shadowing
I prefer not to conflate those two ideas. Besides, CoffeeScript allows shadowing if you do it in function arguments (or using do). They just discourage the practice, because a program is simpler to understand if you're not shadowing names all the time.
This problem is limited to a single file since each file compiles into an immediately invoked function. From what I've seen its not really a problem in practice.
That's nasty. I'm using CoffeScript for almost 3 years already and I didn't notice this issue as I never rely on global vars and my code is mostly stuffed in classes. Instead of global vars I'm using @myVar as a class attribute. I hope it makes sense...
And I'm amazed that anyone can compare the syntax of CoffeeScript, ECMAScript, and Python. and not see that CoffeeScript is clearly the worst, most error-prone, unreadable of the three.
While I understand it's due to prioritizing write-ability over everything else. It seems that everything unique in CoffeeScript is built specifically to be as error-prone, and arcane as possible.
This is why in Python, assignment is a declaration of a local variable by default, you have to mark it as "global" (or "nonlocal" in Python 3), if you want to refer to a global.
I actually kind of hate the python scoping. Since the lexical scoping is so limited ("nonlocal" is a verbose hack) everyone ends up using big classes and explicit self dictionaries instead. Automatic variable declaration also means that typos get turned into new variables instead of errors.
In most languages with closures (including JavaScript) the inner y and the outer y could and would be separate.
This is not really true. The whole point of a closure is that it closes over the surrounding scope. For example, in Ruby:
y = 0
test = lambda do |x|
y = 10
x + y
end
puts test y
puts test y
puts test y
This will print "10", "20" and "20". Exactly like the equivalent CoffeeScript. If you couldn't access the surrounding state, it wouldn't be a closure. In fact, this property of closures is used all the time in JavaScript (for example, to allow for "private" object members). It is not specific to CoffeeScript at all.
The OP's problem really has little to do with scoping. The fundamental "gotcha" here is that in CoffeeScript, there is no difference between defining a new variable and setting an existing variable.
Yes, I realize, I was just clarifying – after reading your post I though that it makes it seem like Ruby has the same issue as CoffeeScript here and that is luckily not true :)
Well, Ruby does behave exactly the same way — it just doesn't come up as often because of how closures are used in the two languages. To some degree, this can actually lead to more surprising behavior in Ruby. For example, if you define a method with define_method, it works like CoffeeScript, but not if you use def.
The OP's problem really has little to do with scoping. The fundamental "gotcha" here is that in CoffeeScript, there is no difference between defining a new variable and setting an existing variable.
I think it's both things really. Python, for instance, is much saner than Coffeescript yet doesn't distinguish between defining and setting a variable. It gets away with it because variables are function scoped (like Javascript). This creates other issues though such as not being able to write to outer-scoped variables without an additional construct. Distinguishing variable declarations from setting existing variables seems like the cleanest way to do it to me, and it supports arbitrary scoping. I also think it conveys the intent of the code better and allows for better error checking.
Despite the amount of spittle and feigned outrage that this feature generates among people who have never given the language an honest try, it's nice to hear a bit in this thread from the folks who have written tens of thousands of lines of CoffeeScript using this style of variable declaration, and have never had a problem. In short -- suspend disbelief for a moment, try something new, and you might like it...
But in long -- CoffeeScript's style of scoping is a deliberate change and (I'd argue) simplification from the usual dichotomy between variable declaration, initialization, and use. Let's reason about it a bit from first principles:
When you write a program, and use a variable to name a value at a certain point in the code, that variable should be a clear identifier for the value. In languages with first-class functions, this gets a little more involved, as closures and lexical scope means that any function defined within the scope where the variable is present is able to access it by name. This is what a closure is, and does -- allows access by the same name to variables in every lexical scope "above".
CoffeeScript tries to stop there. You have variables as names, and they're always expressions -- there's no statement required to declare them -- and they're always local and lexical to the file, and to the smallest scope in that file where they're used. If you want to make a global variable, you must explicitly make it global by attaching it to window, in the browser. The complexity of declaration-vs-initialization is gone.
And (the critical bit) the complexity of shadowing-by-default is gone. A chunk of nested code, where an inner variable shadows an outer variable of the same name, lexically -- is always a bit of a wart on a perhaps otherwise well-structured codebase. When you shadow, you're making it impossible to access the closed-over variable from anywhere further down in the lexical scope, and you're creating a region of the code where it's more difficult for the reader to know the identity of the shadowed variable. Is x the thing it was a moment ago? Oh wait, it's a new value now in this inner part, and as soon as this function ends, on the next line x will again stand for that other thing. If you choose a better name for the variable, instead of shadowing -- which is something that's always possible to do in a lexically-scoped language, your code will be easier to read.
So, CoffeeScript's variables are lexical-to-the-file, scoped as locally as you use them, and ideally, feel more transparent as identifiers, because when you see x, you know it's thex in the current lexical scope, and not an x with multiple identities.
nah, that's a hack. Do is a construct that immediately invokes the passed function, forwarding any arguments, and proper scoped bindings can be simulated here by passing some default values for those arguments.
I'm sure you are aware of the bug in your own compiler introduced by this very issue, mentioned here.
Your motivations for eliminating shadowing are very reasonable. The problem is that you cannot even detect whether the programmer intended to shadow a variable, since there are no mandatory variable declarations. And it's easy to "accidentally introduce intentional shadowing" by adding a function or global variable with the same name as some local variable in the same file, as in the linked bug.
CoffeeScript "forbids shadowing", but in a very strange way: it does not (cannot) reject the program, but instead silently changes the meaning to something entirely different from what the programmer intended. Add mandatory variable declarations to CoffeeScript, and suddenly you can detect shadowing (as well as many other common errors). Then if you want to reject programs that shadow, you'll hear no complaints from me.
I'm certainly aware. Like everything else, eliminating shadowing in this particular way is a tradeoff. When you're introducing new variables at the top level of the file -- something that's pretty rare, that the patch did -- you certainly have the possibility for conflicts. But shadowing also has a possibility for conflicts -- by var'ing a variable in an intermediate scope, you can break an inner scope's access to an outer variable. I've had that problem before in JavaScript.
Incoming pull requests are often slightly broken in various ways, and need to be tweaked after merging. If you think of the tradeoff of changing that variable name, versus having to go through and add var or a special assignment operator to every introduction of a variable in the entire codebase -- it's a choice I'd always be happy to repeat.
Keeping variable names descriptive and singular within a lexical scope, and removing the need to even think about "has this variable been declared in the right place", is a huge boon, in my opinion.
On a more serious note, if you wanted to eliminate shadowing, wouldn't a better strategy be to have the compiler complain if the programmer tries to shadow a variable?
I'm certainly aware. Like everything else, eliminating shadowing in this particular way is a tradeoff. When you're introducing new variables at the top level of the file -- something that's pretty rare, that the patch did -- you certainly have the possibility for conflicts. But shadowing also has a possibility for conflicts -- by var'ing a variable in an intermediate scope, you can break an inner scope's access to an outer variable.
The difference is that a compiler can detect the latter, so the cost is that you spend 1 second compiling your program, get an error and rename the conflicting variable.
When you reuse a name in CoffeeScript, the cost is a bug that can go undetected for a long time.
You haven't gained this:
Keeping variable names descriptive and singular within a lexical scope, and removing the need to even think about "has this variable been declared in the right place", is a huge boon, in my opinion.
because you can get it in a language with explicit variable declarations too! All you've gained is not typing "var", at the cost of obscure bugs.
Implicit variable declarations are a choice I hope no language designer ever makes again.
[...] people who have never given the language an honest try [...]
Guess why people just stay away from it in the first place?
from the folks who have written tens of thousands of lines of CoffeeScript using this style of variable declaration, and have never had a problem
Everyone of those who claims this just hasn't found his bugs yet. The nature of this language bug is that you never can't be sure that you haven't made a mistake by design.
it's nice to hear a bit in this thread from the folks who have written tens of thousands of lines of CoffeeScript using this style of variable declaration, and have never had a problem.
One paragraph in the CoffeeScript doc completely turned me off. I wonder how you deal with it? The paragraph is “Because you don’t have direct access to the var keyword, it’s impossible to shadow an outer variable on purpose, you may only refer to it. So be careful that you’re not reusing the name of an external variable accidentally, if you’re writing a deeply nested function.” What? Be careful? That is what scoping is for so I don’t have to know every name used outside my function. I saw in some comments on the developer’s site that he thought this is like Python. But he’s clearly wrong about that.
I guess it's important to keep reiterating this argument. However, I don't really see a value in the 3000th complaint blog about this. I don't really think this article is constructive in any way.
I haven't been programming for forever, but I don't honestly think that shadowing is as bad as everyone has tried to make it out to be. It can be extremely uncomfortable, and may cause you to have to include extra steps, but I certainly do not think this ruins coffeescript. I guess it depends on programming style, and the problem only grows about as big as you let it. I haven't written more than a thousand lines of coffeescript, however I would like to happily note that coffeescript hasn't yet kidnapped my dogs and held them for ransom. Everything's going ok.
This article helped me move CoffeeScript in my list of "WTF?!" languages. It was quite useful to me as I may have considered using it hadn't I read this.
Well, lots of people who don't know how scoping works.
This is lexical scope, and it's useful as hell to create closures, which are ESSENTIAL to a functional-style programming.
If what you want is to SHADOW a variable from a previous scope, you can do that with Coffeescript's "do".
Coffeescript is not my cup of cake, I hardly ever program with it, but this behavior is predictable. I would have the same behavior in any language that has lexical scope and mutation, that's why things like "let" or "do" exist.
For instance, in Common Lisp you wouldn't do this:
(let ((y 0))
(defun test (x)
(setf y 10)
(+ x y))
(prin1 (test 5))
<more code that uses "y">)
That would be insane. You'd simply:
(let ((y 0))
(defun test (x)
(let ((y 10))
(+ x y)))
(prin1 (test 5))
<more code that uses "y">)
For the same reason, in Coffeescript you could:
y = 0
test = (x) ->
do (y = 10) ->
x + y;
alert test 5
The difference is that in Lisp I can look at a function and a use of a variable in that function and know whether it is local to the function or defined in a wider scope (perhaps globally). Your first test is obviously using an outer y, since there is no (let ((y ...)) ...). Every variable has to be declared somewhere.
In CoffeeScript if I have a function
test = (x) ->
y = 10
x + y
I have no way to know whether y is local or global! It depends on whether there is a global variable y. But either way, test is valid.
It's quite true that you can use do to introduce a local variable. But the default is this crazy context-dependent behavior. do should be the only way to introduce a variable, then you would have the same sane scoping as in Lisp.
That's a common problem on languages that don't make a difference between assigment and binding. But you can always use a subset of the language that makes sense.
Quite in fact, Javascript has a similar problem, but you solve it in a weirder way (with self-calling functions).
Javascript has a similar problem, but you solve it in a weirder way (with self-calling functions).
Javascript has function scope variables (var) so the problem is limited to a function, not the entire scope chain. And "let" is coming in ES6 to give us block scope. And guess what "do" does, it takes a function and calls it in that weird way you're talking about.
Sure you can use a subset of the language, but THE COMPILER DOES NOT CHECK THAT YOU ARE USING A SUBSET OF THE LANGUAGE. Your solution is fine for perfect programmers who write small programs and never make mistakes. But the whole point of using a compiled language is so that the compiler can check for as many possible mistakes as possible. If your solution to a problem with Coffeescript is to introduce a problem that Coffeescript was designed to solve (that the programmer must be perfect and the compiler ignores possible errors), then Coffeescript has failed at its design goal, and you're just making excuses for bad design, which is stupid.
Sure you can use a subset of the language, but THE COMPILER DOES NOT CHECK THAT YOU ARE USING A SUBSET OF THE LANGUAGE.
So? Every language has its own idiosyncrasies that are avoidable, compiled or not. Knowing what can go wrong is important for every developer, ever. And that includes CoffeeScript programmers.
The scope problem has a solution. That's it, why are we still fighting?
“I liken starting one’s computing career with Unix, say as an undergraduate, to being born in East Africa. It is intolerably hot, your body is covered with lice and flies, you are malnourished and you suffer from numerous curable diseases. But, as far as young East Africans can tell, this is simply the natural condition and they live within it. By the time they find out differently, it is too late. They already think that the writing of shell scripts is a natural act.” — Ken Pier, Xerox PARC
There is an expression in Portuguese that I don't think it makes sense in English, which says:
O que tem a ver o cu com as calças?
Translating literally, it means "what does the asshole has to do with the pants?", which is a way of saying that what you're saying is completely out of scope.
If you can tell me a language that you can program without worrying about a subset, I may agree. But every language has problems. Even haskell, common lisp or scheme, which are well regarded.
This is only a problem if you have such a monolithic file that you can't keep track of what variables have been declared, and if you're using global-ish variables, both of which are generally bad ideas. I say it's not a problem with Coffeescript, it's a problem with you. Shadowing is a bad idea and I'm glad Coffeescript prevents it.
In any case, I daresay JavaScript developers make more mistakes from missing var's than Coffeescript developers do from this situation.
This problem is mostly nullified if you don't compile --bare Coffeescript. Without bare you cannot accidentally create or clobber globals. This also means that all variables you could possibly be accessing are in the same file. This helps with visibility. If you suspect you have a shared variable, you only have to look as far as the file in which that variable is used.
Though options like adding "local" or ":=" might seem nice, I'm not sure they help. The main problem is accidental shadowing (or not shadowing in this case). You have to know to use local or := in the first place, so that doesn't solve the problem. If you know you need to use local or :=, you could just use a new variable name instead to the same effect.
I agree that was probably the original intent. However, IMO, variable declaration doesn't fit the spirit of dynamically typed languages where brevity is typically valued over explicit declaration.
It does makes sense in a statically typed language, though . If variable declaration is desired, then languages like Dart (http://www.dartlang.org/) make more sense than CoffeeScript.
How could private methods be easily added? Keep in mind that Coffeescript is meant to just be a thin syntax layer over ordinary Javascript. One of the goals is that Javascript code should be able to seamlessly use classes written in Coffeescript.
And? In coffeescript a.__method will not be changed to something like a.__A__method like in python, which is the entire point of putting underscores in the front of method names in python
class MyClass
test: ()->
@._private_method()
_private_method: () ->
console.log "this is a private method"
Right now this compiles to this javascript:
var MyClass;
MyClass = (function() {
function MyClass() {}
MyClass.prototype.test = function() {
return this._private_method();
};
MyClass.prototype._private_method = function() {
return console.log("this is a private method");
};
return MyClass;
})();
with very simple changes to the compiler it could be changed to this:
var MyClass;
MyClass = (function() {
function MyClass() {}
MyClass.prototype.test = function() {
return _private_method();
};
_private_method = function() {
return console.log("this is a private method");
};
return MyClass;
})();
This makes it so that it scoped within the outter closure so that it is not in the global scope and only the class methods of MyClass can access the private method. Very simple addition to coffeescript yet the author of cs refuses to even consider it.
Ah! So it does, I just tried it out and it compiled just fine. Why have I not seen this specifie before because that would be a perfect definition of a private method then rather than all these other suggestions I am getting.
Private functions are generally not implemented in dynamic languages. Usually if you prefix a function name with an underscore or two, it's enough to signal to other developers not to mess with it.
E.g. __initializer = -> secretStuff()
That same pattern is used in Python. I think since the languages are not compiled there's really no practical way to simply disallow calling a specific method. It might over-complicate the interpreter implementation for little gain or etc. I'm not aware of the specific technical reasons why dynamic languages tend to not have private access levels.
Private functions are generally not implemented in dynamic languages.
Dart has them. Privacy is a module-level concept, not class-level, and I find it works pretty well. I find myself missing protected sometimes, but that's much harder to do in a dynamic language. I think Dart's solution here is a nice compromise between simplicity and control.
Privacy is a module-level concept, not class-level
This is a big difference though. Module level privacy is already kind of possible in JS (with local varables inside an IIFE) but class based (like he was trying to say) is really though because JS objects only have public keys and the instance variables and methods are all mixed together.
Module level privacy is already kind of possible in JS (with local varables inside an IIFE)
That's a bit different. That gives you module-private variables but not module-private methods. Dart has both. JS won't have the latter until private names (or is it "symbols" now?) are in, and those are a good bit more cumbersome to work with.
In Dart, you can define a class like:
class Foo {
_somePrivateMethod() { ... }
}
Code in that library can call _somePrivateMethod() when they have a Foo. Code outside of the library cannot, even if it gets an instance of Foo.
In JS you can always stick a function in your private variables. Of course, you need to use a different syntax so its very annoying if something get converted from public to private or vice versa
self.foo(x)
vs
foo(self, x)
foo.call(this, x)
Its also quite hard to have protected methods or variables (dunno if Dart has those but protected shouldn't be a problem as long as you keep your class hierarchies separate from raw JS)
That said, I'm pretty sure you already know this. I was just pointing out that lots of people tend to mix classes/objects and modules into a single concept even though they are actually very orthogonal.
In JS you can always stick a function in your private variables.
Yup, but you lose dynamic dispatch in the process. For example:
abstract class Tool {
void _click();
}
class Paintbrush extends Tool {
void _click() { ... }
}
class Eraser extends Tool {
void _click() { ... }
}
Here we've got a couple of derived classes that override a private method. In Dart, within the library where these are defined, you can call _click() and it will dynamically dispatch to the right method if you have a Paintbrush or an Eraser.
Thats what I was talking about when I mentioned the problem of protected methods. This is OK for Dart but its though for Coffeescript because in that case you want to be able to have vanilla JS classes inheriting from your CS class and then its tricky to have a property be visible by the child class without also making it fully public.
CoffeeScript on the other hand offers you no method to scope a variable. Scoping is entirely automatic, and the lowest level use of a variable name is the single instance of it.
In other words: every variable in Coffeescript (apart from function parameters) is a global.
Well you have two functions in the same scope, they can both have their own x, but if you define x above the functions, thay're magically both using that x instead.
"if a name has been used as a global or in an outer scope, then references to that name will always refer to the global/outer variable"
so if I use a variable globally I'm already using somewhere else, it magically becomes global. Am I not correct? Which is almost the exact same behavior as it having been global to begin with.
I can use a variable globally I initially defined in a function. It was scoped, but when I use it outside its not. Might as well have not been scoped to begin with, because it gets none of the benefits.
Might as well have not been scoped to begin with, because it gets none of the benefits.
Not exactly, you can have two functions f and g which both have a local variable named x, and if f calls g, g won't clobber f's x. If x was truly global, it would get clobbered.
All variables are scoped as usual, the only difference is that there is no shadowing, so if you refer to a variable with the same name as a global variable inside a function, you will access the global one, not shadow it.
While I agree that the scoping rules are odd, the problem can be vastly reduced with a consistent naming scheme. For example all variables start with a lower case letter, globals are prefixed with "g_", functions start with an upper case letter etc. Not saying you should use that one specifically, just that you should use one so you know the type and scope of the variable just from the name.
JavaScript does have a bunch of technical issues and CoffeeScript insulates you from most. Here's some of it.
Too many different interpreters. Increasingly a historical argument, thankfully, but cross-browser compatibility has been a pain. Again, CoffeeScript insulates you, at least to a degree.
It just doesn't feel comfortable. I've seen too many cases of semantic trickery and nested function(){}s that render code ineligible. I feel better writing Pythonesque languages. That's not a technical argument, just personal preference. Just like I use HAML and such, I'd easily use a translator from a style I like to JS, even if it did nothing else.
I have become quite attracted to CoffeScript lately and I can see how this blog post could discourage some to avoid it, but actually it just made me like it more..!
Seeing as this is a deliberate design choice from the creator, there must be a reason why it's made this way and why so many people use it.. So one little blog post isn't going to scare me that easily..
The more I think about it and read the comments in this thread, the more it makes me understand what makes CS so special.. how it encourages simple and beautiful code by thinking about how you name things.. and not just be some punch card like stream of verbose mechanical instructions..
An easy and natural way of avoiding accidental shadowing is to group and structure all your code and variables into objects and classes; make your program modular.. that way the global scope won't be cluttered with all kinds of confusing names.. you would only have a limited number of modules who can access each other through each respective namespace.. clear and simple..
Every good developer knows that each tool/language serves different use cases and different people.. hopefully this thread will help you decide better whether CS is something for you.. if not, there are plenty of other languages to go around.. :)
trust your technolust.. :D
EDIT: I guess what I'm trying to say is that I understand how this scoping would fit well with CoffeeScript's personality, but I also understand why the blogger consideres this dangerous behavior, because it really is.. But that's CoffeeScript for you.. take it or leave it..
Seeing as this is a deliberate design choice from the creator, there must be a reason why it's made this way and why so many people use it.. So one little blog post isn't going to scare me that easily..
You could just push all your global variables to the bottom of your script.. variable names become declared in the order they first appear in the script..
EDIT: not that I would generally recommend that.. just a suggestion to those who like to pollute their global scope with all sorts of junk..
The worst offense of CoffeeScript is the syntax. I know they were going for concise, but replacing words with hieroglyphics is never the answer. Didn't everyone reject Perl for that reason? Replace function with def or sub and you're concise. Replace it with -> and you've gone right past concise to obtuse.
I wrote Perl as my primary language for a good 4-5 years, so no I don't have a problem with it. I just don't understand why we would make a conscious decision to move toward it in 2013. I have the same problem with Scala. It's practically the Holy Grail of features, but it reads like a mess of punctuation marks.
14
u/cashto Jul 25 '13
For more discussion, see the ticket on github.
There exists a fork of CoffeeScript (named Coco) in response to this and a handful of other issues. I haven't used it though.
3
u/kungfufrog Jul 26 '13 edited Jul 26 '13
As someone who programs in Javascript for a living but has never written a single line of CoffeeScript, why is this a problem? I don't get what the actual complaint is.
Is the complaint that it is non-obvious that the closure around the test function includes the var y defined outside the function's scope?
I use outside scoped variables all the time to pass information down through async chains. I also don't use generic variable names like 'y'.
29
u/brandjon Jul 25 '13
So it's basically unannotated declarations like Python, but everything is nonlocal or global by default, with no "local" keyword to override?
Yeah, that sounds like madness to me.
10
u/[deleted] Jul 26 '13
Generally "at work" I avoid CoffeeScript, primarily for the reason I still work with a lot of folks that don't understand JavaScript... but in general, I find it's good to familiarize yourself with the JS output of CS, then just treat CS like shorthand for JS.
... in otherwords, when I'm using CS I'm still thinking in JS.
I don't at all recommend CoffeeScript to anyone that isn't already relatively skilled in JavaScript.
56
u/Plorkyeran Jul 25 '13
While I agree with the title of this post, in the process of writing ~20k lines of CoffeeScript it hasn't actually ever bitten me, unlike some other problems with the language. Avoiding deeply nested scopes (and having too many things in scope in general) makes it easy to avoid issues, and IMO that's a good idea even in languages with sane scoping anyway.
24
u/iopq Jul 25 '13
This is really WTF. Not because of its practical significance, but because this is what I would expect of a language like bash or something. Have the creators of CoffeeScript heard of lexical scoping?
5
u/doomhz Jul 26 '13
That's nasty. I'm using CoffeScript for almost 3 years already and I didn't notice this issue as I never rely on global vars and my code is mostly stuffed in classes. Instead of global vars I'm using @myVar as a class attribute. I hope it makes sense...
25
u/AreaOfEffect Jul 25 '13
IMO, CoffeeScript has a limited life span. ECMAScript 6 has a vastly improved syntax that rivals python.
12
u/dons Jul 25 '13
Fucking variable bindings, how do they work?
2
u/[deleted] Jul 26 '13
This is why in Python, assignment is a declaration of a local variable by default, you have to mark it as "global" (or "nonlocal" in Python 3), if you want to refer to a global.
5
u/PaintItPurple Jul 25 '13
This is not really true. The whole point of a closure is that it closes over the surrounding scope. For example, in Ruby:
This will print "10", "20" and "20". Exactly like the equivalent CoffeeScript. If you couldn't access the surrounding state, it wouldn't be a closure. In fact, this property of closures is used all the time in JavaScript (for example, to allow for "private" object members). It is not specific to CoffeeScript at all.
The OP's problem really has little to do with scoping. The fundamental "gotcha" here is that in CoffeeScript, there is no difference between defining a new variable and setting an existing variable.
3
u/Newtonip Jul 26 '13
Madness?
THIS IS COFFEESCRIIIIIIIIIPT!!!!
7
u/jashkenas Jul 25 '13 edited Jul 25 '13
Despite the amount of spittle and feigned outrage that this feature generates among people who have never given the language an honest try, it's nice to hear a bit in this thread from the folks who have written tens of thousands of lines of CoffeeScript using this style of variable declaration, and have never had a problem. In short -- suspend disbelief for a moment, try something new, and you might like it...
But in long -- CoffeeScript's style of scoping is a deliberate change and (I'd argue) simplification from the usual dichotomy between variable declaration, initialization, and use. Let's reason about it a bit from first principles:
When you write a program, and use a variable to name a value at a certain point in the code, that variable should be a clear identifier for the value. In languages with first-class functions, this gets a little more involved, as closures and lexical scope means that any function defined within the scope where the variable is present is able to access it by name. This is what a closure is, and does -- allows access by the same name to variables in every lexical scope "above".
CoffeeScript tries to stop there. You have variables as names, and they're always expressions -- there's no statement required to declare them -- and they're always local and lexical to the file, and to the smallest scope in that file where they're used. If you want to make a global variable, you must explicitly make it global by attaching it to
window, in the browser. The complexity of declaration-vs-initialization is gone.And (the critical bit) the complexity of shadowing-by-default is gone. A chunk of nested code, where an inner variable shadows an outer variable of the same name, lexically -- is always a bit of a wart on a perhaps otherwise well-structured codebase. When you shadow, you're making it impossible to access the closed-over variable from anywhere further down in the lexical scope, and you're creating a region of the code where it's more difficult for the reader to know the identity of the shadowed variable. Is
xthe thing it was a moment ago? Oh wait, it's a new value now in this inner part, and as soon as this function ends, on the next linexwill again stand for that other thing. If you choose a better name for the variable, instead of shadowing -- which is something that'salwayspossible to do in a lexically-scoped language, your code will be easier to read.So, CoffeeScript's variables are lexical-to-the-file, scoped as locally as you use them, and ideally, feel more transparent as identifiers, because when you see
x, you know it's thexin the current lexical scope, and not anxwith multiple identities.That's the rationale.
2
u/agumonkey Jul 25 '13
Isn't it only more painfull because it's the only big problem of coffeescript ? like a 79/21 ..
2
u/howsyourweird Jul 26 '13
Comment #1 on this page and the preceding thread pointed out this shadowing 2+ years ago.
2
u/scandium Jul 25 '13
:o
2
u/notorious1212 Jul 25 '13
I guess it's important to keep reiterating this argument. However, I don't really see a value in the 3000th complaint blog about this. I don't really think this article is constructive in any way.
I haven't been programming for forever, but I don't honestly think that shadowing is as bad as everyone has tried to make it out to be. It can be extremely uncomfortable, and may cause you to have to include extra steps, but I certainly do not think this ruins coffeescript. I guess it depends on programming style, and the problem only grows about as big as you let it. I haven't written more than a thousand lines of coffeescript, however I would like to happily note that coffeescript hasn't yet kidnapped my dogs and held them for ransom. Everything's going ok.
1
u/gargantuan Jul 28 '13
Fuck it! Dart it is then!
1
u/Denommus Jul 26 '13
Well, lots of people who don't know how scoping works.
This is lexical scope, and it's useful as hell to create closures, which are ESSENTIAL to a functional-style programming.
If what you want is to SHADOW a variable from a previous scope, you can do that with Coffeescript's "do".
Coffeescript is not my cup of cake, I hardly ever program with it, but this behavior is predictable. I would have the same behavior in any language that has lexical scope and mutation, that's why things like "let" or "do" exist.
For instance, in Common Lisp you wouldn't do this:
That would be insane. You'd simply:
For the same reason, in Coffeescript you could:
If you want a local binding, "do" is there.
0
u/LukeGT Jul 26 '13
This is only a problem if you have such a monolithic file that you can't keep track of what variables have been declared, and if you're using global-ish variables, both of which are generally bad ideas. I say it's not a problem with Coffeescript, it's a problem with you. Shadowing is a bad idea and I'm glad Coffeescript prevents it.
In any case, I daresay JavaScript developers make more mistakes from missing var's than Coffeescript developers do from this situation.
1
u/Shanebdavis Jul 26 '13
This problem is mostly nullified if you don't compile --bare Coffeescript. Without bare you cannot accidentally create or clobber globals. This also means that all variables you could possibly be accessing are in the same file. This helps with visibility. If you suspect you have a shared variable, you only have to look as far as the file in which that variable is used.
Though options like adding "local" or ":=" might seem nice, I'm not sure they help. The main problem is accidental shadowing (or not shadowing in this case). You have to know to use local or := in the first place, so that doesn't solve the problem. If you know you need to use local or :=, you could just use a new variable name instead to the same effect.
1
u/drunken_thor Jul 25 '13
This is not a complaint for me, does no one else have a problem with lack of private methods and how easily those could be included in the language?
-2
u/bart2019 Jul 25 '13
In other words: every variable in Coffeescript (apart from function parameters) is a global.
-3
u/Y_Less Jul 25 '13
While I agree that the scoping rules are odd, the problem can be vastly reduced with a consistent naming scheme. For example all variables start with a lower case letter, globals are prefixed with "g_", functions start with an upper case letter etc. Not saying you should use that one specifically, just that you should use one so you know the type and scope of the variable just from the name.
-4
u/[deleted] Jul 25 '13
It still beats writing JS! If Coco picks up steam, I would switch over for future projects.
-1
u/[deleted] Jul 26 '13
"Look! When I define a variable in an outer scope, for some reason it gets defined in the outer scope! Madness!"
-5
u/[deleted] Jul 26 '13 edited Jul 26 '13
I have become quite attracted to CoffeScript lately and I can see how this blog post could discourage some to avoid it, but actually it just made me like it more..!
Seeing as this is a deliberate design choice from the creator, there must be a reason why it's made this way and why so many people use it.. So one little blog post isn't going to scare me that easily..
The more I think about it and read the comments in this thread, the more it makes me understand what makes CS so special.. how it encourages simple and beautiful code by thinking about how you name things.. and not just be some punch card like stream of verbose mechanical instructions..
An easy and natural way of avoiding accidental shadowing is to group and structure all your code and variables into objects and classes; make your program modular.. that way the global scope won't be cluttered with all kinds of confusing names.. you would only have a limited number of modules who can access each other through each respective namespace.. clear and simple..
Every good developer knows that each tool/language serves different use cases and different people.. hopefully this thread will help you decide better whether CS is something for you.. if not, there are plenty of other languages to go around.. :)
trust your technolust.. :D
EDIT: I guess what I'm trying to say is that I understand how this scoping would fit well with CoffeeScript's personality, but I also understand why the blogger consideres this dangerous behavior, because it really is.. But that's CoffeeScript for you.. take it or leave it..
0
u/[deleted] Jul 26 '13 edited Jul 26 '13
You could just push all your global variables to the bottom of your script.. variable names become declared in the order they first appear in the script..
EDIT: not that I would generally recommend that.. just a suggestion to those who like to pollute their global scope with all sorts of junk..
-8
u/[deleted] Jul 25 '13
The worst offense of CoffeeScript is the syntax. I know they were going for concise, but replacing words with hieroglyphics is never the answer. Didn't everyone reject Perl for that reason? Replace
functionwithdeforsuband you're concise. Replace it with->and you've gone right past concise to obtuse.-8
u/dmitry_sychov Jul 25 '13
But millions of web monkeys who have hard time separating assignment and declaration feel comfortable with CoffeeScript.