5 ms·
The goal of this diatribe appears to be to justify CoffeeScript's design, but to me it counts against it. To be fair, I have never thought CoffeeScript was wort
by ynniv 14y ago
The goal of this diatribe appears to be to justify CoffeeScript's design, but to me it counts against it. To be fair, I have never thought CoffeeScript was worth using, but here's the crux:
The pitch: "CoffeeScript is a better syntax for writing JavaScript."
The reality: "CoffeeScript does not behave like JavaScript in subtle, non-syntactical ways."
It is often suggested that people use CoffeeScript instead of JavaScript. But this understanding of CoffeeScript is inaccurate. CoffeeScript has different semantics from JavaScript, and the obvious expansion of CoffeeScript back into JavaScript is a wrong expansion. A knowledgeable JavaScript developer will make mistakes when making simple changes to CoffeeScript code that they did not write. They will have difficulty fixing bugs in CoffeeScript code.
If you like, you can be a CoffeeScript shop. Find senior developers who use CoffeeScript and teach junior developers to use it. Code will be a little easier to write, and, like nerf guns and foosball, you might seem like a superficially cool place to work. Like any new technique, you'll be out on your own. You'll run into bugs, you'll deal with toolchain support, you'll find optimization issues.
There are reasons to translate other languages into JavaScript. Macros, static typing, execution in other contexts. CoffeeScript gives you ruby-like syntax... To me, that just doesn't seem worth it.
- raganwald 14y agoCoffeeScript has different semantics from JavaScript, and the obvious expansion of CoffeeScript back into JavaScript is a wrong expansion Nothing in the post supports this assertion. You may have your own reasons for feeling this way, but the difference between JavaScript and CoffeeScript with respect to the "var" keyword is neither subtle nor obvious, given that CoffeeScript does not have a var keyword. The only possible source of literal misinterpretation would be to look at a CoffeeScript file and think that every variable that wan't a parameter was a global variable. This is extremely unlikely in practice. I read you as being biased against using it. FIne with me, Chief, go your way in peace. But don't try to twist my example into support for a decision you have already made.
- shaunxcode 14y agoThe thing I used to hate about smalltalk was having to define the temporary vars at the top of the method/block. Now it just seems like a great idea which clarifies any sort of ambiguity like this.
- ynniv 14y agothe difference between JavaScript and CoffeeScript with respect to the "var" keyword is neither subtle nor obvious, given that CoffeeScript does not have a var keyword. Great, then every variable in CoffeeScript must be global, because that's the semantics of a non-parameter not be declared a "var" in JavaScript. Except that this is not an accurate translation, as the entire post goes on to explain scoping in CoffeeScript. The only possible source of literal misinterpretation would be to look at a CoffeeScript file and think that every variable that wan't a parameter was a global variable. This is extremely unlikely in practice. Screwing up scoping is an extremely common JavaScript mistake. If you showed me JavaScript-like code and told me there was a bug, the first thing I would point out is that you declared everything globally. Oh, that isn't the case in CoffeeScript? Huh, I guess you're on your own then, because I didn't learn your goofy alternative JavaScript syntax. Maybe you think the junior kid who has a bug in his code is going to properly explain the subtlety of CoffeeScript scoping. FIne with me, Chief, go your way in peace. But don't try to twist my example into support for a decision you have already made. It's offensive of you to dismiss my criticism in this forum. I am not prejudiced against new things, and while CoffeeScript doesn't seem like a worthwhile idea to me, it's community is far more offensive. CoffeeScript's demagoguery of JavaScript might impress some folks, but outside ruby-like syntax, CoffeeScript doesn't solve any fundamental issues. So, "go your way in peace", and leave me to my thoughts, please.
- raganwald 14y agoAs every debate ought to say, we're each entitled to our own opinions, but not our own facts. My opinion--which I believe I have demonstrated to be a fact--is that it is possible to write sane CoffeeScript that is lexically scoped and not subject to misinterpretation. It is a fact that it is possible to write CoffeeScript code that requires careful examination of the file to understand what it does, code which can also change if you alter something somewhere else in the file. My opinion--which I also believe I have demonstrated to be a fact--is that it is possible to write sane JavaScript that is lexically scoped and not subject to misinterpretation. It is clearly possible to write JavaScript code that requires careful examination of the file to understand what it does, acode which can also change if you alter something somewhere else in the file. I have zero quibble with you or anyone else feeling CoffeeScript doesn't solve a problem for them. I have zero quibble with you or anyone else saying "Notwithstanding the fact that CoffeeScript is a lexically scoped language, my opinion is that I hate it." My only quibble--and it is minor--is that it is not possible to say that CoffeeScript function-local variables don't work like JavaScript function-local variables and that you can confuse the two, since without the var keyword nobody can look at a CoffeeScript file and think they are looking at function-local variables. We both agree on the fact that a file with variables that are not parameters in CoffeeScript has different semantics from a file with variables that are neither parameters nor function-local in JavaScript. If your opinion is that this is a fatal flaw, well, who am I to argue with the fact that this is your opinion? I have a different opinion, and we are both entitled to same provided neither of us claim it to be a fact.
- lmm 14y ago>the difference between JavaScript and CoffeeScript with respect to the "var" keyword is neither subtle nor obvious, given that CoffeeScript does not have a var keyword The thing that is subtle and nonobvious as a javascript programmer is the way CoffeeScript variables are scoped. They are not scoped like "var" variables in javascript. They are not scoped like "non-var" variables in javascript either. They have their own kind of scope that is unique to CoffeeScript. Now you can argue that the way javascript scopes variables is wrong, and I'd agree with you. You can argue that the CoffeeScript way is better, and that's fine. But it makes it a different language from JavaScript, and it has all the baggage that goes along with that. You can't just treat CoffeeScript as "javascript with a nicer syntax"; you need to approach it the same way you'd approach learning any new language.
- mistercow 14y ago>CoffeeScript gives you ruby-like syntax The implied "only" in that statement seems preposterous to me. For one thing, it's both an over- and understatement of CoffeeScript's featureset, and for another, it's sort of like saying "The printing press just let people print books faster ∗yawn∗." (Well, OK, so CoffeeScript isn't quite the leap that the printing press was, but you get the point.)
- jlgreco 14y agoI haven't looked at CoffeeScript yet (I avoid Javascript work as it is already). What else does CoffeeScript give you?
- lowboy 14y agoFrom a comment on the other HN thread (http://news.ycombinator.com/item?id=4534863 http://news.ycombinator.com/item?id=4534863) string interpolation multi-line strings comprehensions guarded loops (hasOwn...) easy constructor/prototype definition (class) avoiding global leaks/shadowing function binding splats, defaults safe usage of reserved names existential operator chained comparisons READABLE regular expressions
- nostrademons 14y agoDumb question: why would I use CoffeeScript over something like PyJS (pyjs.org) or even (shudder) GWT? I've worked on enough projects that I've come to view syntax as a cost and not a feature. It can sometimes be worth paying that cost if it gets you lots of features - but all the features you list already exist in Python, and presumably can be translated just fine by PyJS. Why an entirely new language syntax instead of something that you're likely using on the server as well? Or is CoffeeScript basically Javascript with Ruby syntax?
- lowboy 14y agoCS is more than just syntactic sugar, but speaking in terms of syntax it has a more minimal mindset like Python or Ruby. The difference between source and compiled code is much much closer. CS has been designed as a pure abstraction of JS, whereas those other projects take full-fledged languages with all of their idoms and features and make JS out of it. I agree that new syntax is a cost, and in general that we should fear abstractions. PyJS seems like it's more of a web framework (as is GWT) rather than a pure language compiler. Python is a beautiful language, but just code in JS if you're working on the front end and are wary of CS. I'm a capable JS developer and it took me almost no time to get started with CS. Sure, you hit snags here and there with oddball situations and sometimes you have to check the output, but the benefits definitely outweigh the learning costs.