9 ms·
CoffeeScript: The beautiful way to write JavaScript
- endtime 16y agoI love CS, but this article doesn't seem to have much useful content that isn't arleady in the CS docs: http://jashkenas.github.com/coffee-script/ http://jashkenas.github.com/coffee-script/
- derwiki 16y agoNope, but I read this article and then read the CS docs -- which I wouldn't have done if this article wasn't posted.
- MatthewPhillips 16y agoI've had trouble learning CoffeeScript due to the compiler not providing any help whatsoever when you make a mistake. I was writing a web server that just wouldn't compile, looked the code over several times and eventually gave up and did it in javascript instead. Later found out that Gedit was displaying a tab that vim showed as just being a space. Never tested if that was the only problem or not.
- TrevorBurnham 16y agoAgreed. The compiler definitely isn't as helpful as it could be in giving information about errors. On the other hand, a lot of the errors it hits at compile-time are things you wouldn't have noticed until run-time if you were writing JavaScript.
- jherdman 16y agoI can't shake the feeling that people that like CoffeeScript are those that don't "get" JavaScript (e.g. CoffeeScript's notion of a class, whereas JavaScript has none -- let go an embrace the prototypal object model). Further, most (if not all) of the examples of the problems with JavaScript are null and void. A language that combines paradigms? Most do. Sure, the name sucks, but that's hardly an issue. I understand that syntax may be more convenient for some, but this is indirection, and is a source of complexity that may impede collaboration from other devs more familiar with JavaScript.
- MatthewPhillips 16y agoCoffeeScript is javascript so prototypal objects still work as expected. They just went ahead and threw in classes if you want them, since there are already many javascript libraries that provide them for people who either are used to / want classes or to make it easier to port existing code.
- neimado 16y agocoffescript !== javascript, and it never will === javascript.
- jamesgeck0 16y agoPlease elaborate. It's easy to match generated Javascript to it's corresponding CoffeeScript source. There's a lot of syntactical sugar, but there isn't much that isn't just a handy shortcut. I used something kinda like CS's class system in JS before I even discovered CS.
- jashkenas 16y agoPerhaps this isn't the best forum for such a philosophical debate, but the notion that because you have prototypes in JavaScript you don't have classes is just plain wrong. Every single time you see JavaScript prototypes used effectively, you'll find that they follow a classical pattern: A constructor function serves as the class object from which new objects are instantiated. Syntax like instanceof ("object instanceof class") gives this away. So do all of the built-in JavaScript classes: String, Function, RegExp, Number... If it's an abstract object that defines common properties for instances, it's a class object, regardless of what you choose to call it. JS prototypes just expose the implementation mechanism, and CoffeeScript just provides you with a convenient way to manipulate the prototype chain.
- curtis 16y agoI'm personally of the opinion that prototype-based systems are superior to class-based systems in exactly the same way that gotos are superior to for loops. Add to this the fact that JavaScript's prototype system isn't actually very flexible, and you're almost always better off to roll a class-based system on top of it.
- dstein 16y agoI really wanted to like CoffeeScript but that it can't be metaprogrammed and serialized (easily) is a huge problem for a scripting language. I guess scripting was never the point of CoffeeScript, but a lot of what makes JavaScript so flexible is lost when you add a compile stage.
- mrkurt 16y agoI'm struggling to think of something I can do in Javascript, but not CoffeeScript. Can you give an example?
- dstein 16y agoBecause of whitespace and line break sensitivity you can't dynamically generate serialized CoffeeScript (unless delicately handled) because it won't compile.
- oscilloscope 16y agoI'm building a web-based IDE for Raphael in CS. My big pain point: when users define a function, JS Shell returns the JS version of the function. If the user types in a function name without calling it, they see the JS definition. Since one can't go from JS -> CS, I can't figure out a good way to keep JS away from the user. http://fleetinbeing.net/raffi/ http://fleetinbeing.net/raffi/ (type "spiral 30" to run a function defined in CS, then type "spiral" to see the definition in JS)
- barmstrong 16y agoI would love to see javascript turn into assembly language. Google has already done with with GWT with great improvements in productivity. One question I had while reading this. Was there a technical reason why he didn't make a gem or toolkit in pure Ruby or Python that would then spit out Javascript? I think this is closer to how GWT did it where you can write pure Java, and of course it then wouldn't require people to learn a new language.
- oscilloscope 16y agoThe issue is Ruby/Python/Java's semantics don't map perfectly to JavaScript. As it says in the documentation, the golden rule of CoffeeScript is "It's just JavaScript". For people who know JavaScript, it's not a new language-- just some syntactic sugar and a few bonus features.
- davepeck 16y agoOut of curiosity: What's with today's obsession over beautiful syntax? Shouldn't we strive for beautiful semantics, instead?
- skymt 16y agoIn this case, the semantics were already decided: CoffeeScript was intended to map transparently to JavaScript. Each change to semantics would add complexity to that mapping.
- davepeck 16y agoSyntactic sugar is all CoffeeScript can ever be. Is pretty syntax better than common-use syntax? (To be clear: I don't have a strong opinion. I'm curious what the community thinks the value proposition for CoffeeScript, HAML, etc is.)
- jacobolus 16y ago> Is pretty syntax better than common-use syntax? What do you mean?
- davepeck 16y agoJavascript is understood by most developers these days. CoffeeScript is pretty. (Edit: I don't think that "pretty" is a bad thing. I just... when is it the most important thing, or even high up on the priority list? Pretty isn't bad, but if it means learning an entirely new syntax and immature toolchain, its value for all but personal projects is unclear to me. I see that others value it differently, and I'm trying to understand why.)
- ramidarigaz 16y agoYou say "pretty" like it's a bad thing...
- JoelSutherland 16y agoI couldn't agree more. The biggest problem with CoffeeScript adoption is the administrative overhead required for novices to get it running. This means that most CoffeeScript projects are usually larger -- it just doesn't get used in small one-off things. My company's product is a hosted CMS and we just tackled this problem (http://www.gethifi.com/blog/hosted-cms-coffeescript-support http://www.gethifi.com/blog/hosted-cms-coffeescript-support) by completely eliminating the workflow issue. You can use Coffee or JS and the system takes care of the rest for you. I've found this to be a big help when working on smaller client sites. Often I just need to write a couple dozens lines of JS and it just isn't worth the hassle to do it in Coffee -- now it's gravy.
- munificent 16y agoI started toying with CoffeeScript a while back for my own entertainment and found the administrative overhead was much less than I anticipated. I went from a clean slate to being up and running in like five minutes. Once I had node and CoffeeScript installed, it was just a single command-line to start a watch on my .coffee file and then I was back to "refresh browser window to see my changes" mode just like regular JS.
- nexneo 16y agoThis is almost copy of CoffeeScript manual (plus some old jokes about Javascript) Read original here, http://jashkenas.github.com/coffee-script/ http://jashkenas.github.com/coffee-script/
- Almaviva 16y agoI don't find an overly naive use of recursion that leads to an exponential time algorithm to be beautiful.
- justrudd 16y agoI've done my fair share of web programming. But JavaScript (for me) has always been to add the flash, not to write the app. In the past couple of months, I dove into Backbone.js, Sammy.js, etc. and while my JavaScript worked, it wasn't "the JavaScript way". When I found CoffeeScript, I was very happy. But I've been moving back to more JavaScript recently because I've spent hours looking through the generated JavaScript and learned quite a bit from it. I did the same with my C compiler and x86 Assembly :) It's nice to read what the experts think the final product should look like. So write some CoffeeScript, compile it, and learn some JavaScript from it.
- astrodust 16y agoIf you can't debug in CoffeeScript, you can't take it seriously. Even meta-CSS frameworks suffer from this problem when there's errors you have to back-track through a whole layer.
- knv 16y agoI wonder if it's feasible to hack the javascript engines (v8, spidermonkey, ...) to support languages CoffeeScript? In case the answer is positive how come nobody's doing that?
- jergosh 16y agoaka "How not to implement Fibonacci sequence"
- larelli 16y agoY U no cache the results?
- btipling 16y agoUgh, there is nothing wrong with JavaScript, please let's all just forget that CoffeeScript even exists. Thank you.