A CoffeeScript Intervention
pragprog.com
pragprog.com
You can argue whether this is good or bad, but it's not giving people a better understanding of anything, it's just making them follow best practices unknowingly.
JavaScript can be pretty overwhelming to newcomers. You mention scope, which is a good example of where CoffeeScript actually clarifies rather than hides the underlying semantics.
The most important thing to understand about scope in JS is that only functions create scope. This is very different from C and Java, and since JavaScript looks like C and Java, that difference is surprising (see e.g. http://www.adequatelygood.com/2010/2/JavaScript-Scoping-and-...). When you learn the same rule in CoffeeScript, it feels a lot simpler. And that understanding can carry over to JS.
Granted, the compiler is generating the `var` keyword for you, but the mapping is trivial. What's important is that scoped variables have the same behavior in both languages.
That being said I prefer not to have to write "return" but work plenty with languages outside of ruby and have no issues writing return when necessary.
Sure, and functions by definition return a value. I know programming isn't math, but the notion of a "void function" is somewhat of an oxymoron.
var scope = "global";
function f() {
console.log(scope); // Prints "undefined", not "global"
var scope = "local"; // Variable initialized here, but defined everywhere
console.log(scope); // Prints "local"
}
I'm someone who uses Javascript only occasionally, and I'm trying to learn it better. But I thought I understood scope just fine: no block scope, function scope, lexical scoping within functions and nested functions. I may have read about "hoisting" before, but honestly it never sunk in until today.So I'm assuming that Coffeescript always compiles down to Javascript where variables are explicitly hoisted upwards in declaration? (Answer appears to be yes from a few quick tests here...)
Using jslint eliminates item 1.
Item 2 is a stupid language quirk. Guess what, Coffeescript is a whole new layer of quirks.
Aside from item 4, the rest are based on a mistaken idea that reducing typing somehow is a huge advantage. (I don't know, it may be for some people but I'm either too fast a typist or too dumb a programmer.)
CoffeeScript is probably very appealing to some people because it imports coding conventions from some other language (Ruby I guess) that they like. I happen to like JavaScript and running code through jslint once in a while is a heckuva lot less annoying than having to compile code and deal with debugging "assembler".
Unfortunately, it's a feature only available in Mozilla's JS engine at the moment.
As an example, the whole point of setTimeout is to be able to do the sort of thing shown in Example 4. Defeating it just means you now have to search for another way to do the things that setTimeout was designed for.
Same with variable scope. If you can't declare a global without actually standing in the global scope, that makes things harder, not easier.
Beyond that, I glazed over at seeing various little ascii arrows. I'm sure they're there for a reason, but by then I was already halfway out the door.
1) You can trivially use setTimeout just as you did before. CoffeeScript just gives you a couple extra tools for simplifying things when you have use cases like the one presented in the article.
2) As for the global namespace: if you want to attach something to the window object it’s much more readable to just write "window.foo = bar" or whatever. In general though, I defy you to come up with an example where you want to define global variables from some other scope where your code wouldn’t end up just as clear but more robust with cross-scope variables defined in some high-level scope. Pretty much everyone agrees that JavaScript’s "pollute the global namespace by default" feature was a mistake.
The ascii arrows are by far the best part. JavaScript turns out to be a very nice language (semantically) for writing functional-style code. Unfortunately, the regular JavaScript syntax makes defining functions so unpleasant that I go out of my way to avoid extra ones. CoffeeScript makes defining functions so easy that I go looking for ways to use more, instead.
The article states that
> It’s about more agile code
but I find this claim dubious when the best JS tools are for JS, and not CoffeeScript. I believe that the features provided by the Closure compiler are more beneficial for stable JavaScript development (annotations, etc), and carry a significant performance incentive.
That seems to be trivially true, but in context it only makes sense if you're implying a bi-conditional, namely that the only reason to use a wrapper is if you don't understand var and this.
I believe that to be false. There are many reasons to use syntactic or semantic sugar even when you understand the underlying language very well. As a demonstration of this, I give you that most tools of this type are written by people who understand the underlying languages incredibly well and design such tools for their own use as relative expert users.
Also, I don't think CS is a tool to abstract away these problems from DEVELOPERS...that is, it's not so developers don't have to understand these issues...it's a tool to abstract away these problems from your CODE...so that you don't have to write all sorts of repetitive, dense, and gnarly code to work around all this stuff
. A lot of times when Coffeescript comes up, people think that it's purely for people that don't "understand" Javascript, but this is not the case at all. I know some supremely knowledgeable Javascript developers who have adopted it.
Tooling is, imho, one of the biggest drawbacks of Coffeescript at this point; however it's making progress. Coffeescript produces fairly clean and readable code (though it is very verbose), and it also has pass-through syntax for both code and comments...so tools like Closure Compiler could be used on the generated Javascript without a whole lot of trouble.
Some tools can be adapted by the user to use CS, and in other cases the creators are working to support Coffeescript (there are some at Mozilla who want to add debugging, etc support for CS). In cases where all else here fails, the community is working on providing Coffeescript versions.
Some people may not want to use it, or may be unable to due to some tool or workflow not being supported; but I've found most JS devs I know able to pick it up and use it quite easily with good returns.
I wouldn't call those "finer" points.
What exactly do you mean by this? What is there that you can do in Javascript that you can't do in Coffeescript?
In other languages I can put up with a reasonable amount of pain but "this" and dealing with loops in JS puts me over the edge.
As it was a 1-1 conversion it was fun to see not only how much syntactical cruft the CS syntax removes but also how a few sloppy lines got tidied up, (mainly existence checks). In that sense, it adds value. It'll be interesting to follow this up with how easy it is to churn out new code ...