CoffeeScript: Why I’m never writing Javascript again

Posted: February 14th, 2011 | Author: | Filed under: CoffeeScript, Javascript, programming | 23 Comments »

If you program in Javascript and haven’t heard of CoffeeScript yet, you are missing out. I’m not going to talk about installing it or the basic usage of it as the original documentation and a few blogs already have talked about it.

What I want to talk about is why it’s so amazing and why I can never write Javascript again. I’ve been programming Python for quite a while now and when  you do work on the web (like with Django) you will eventually have to deal with the browser and if you want to do anything beyond basic form processing you’ll need to deal with Javascript.

The maturation of libraries like jQuery has made life, infinitely easier but, Javascript itself is still a strange little language. It’s actually deceptively simple, it that any decent programmer can probably trial and error his/her way through a simple Javascript feature pretty quickly. This is actually one of the strongest features of the language, most people can write Javascript without actually knowing Javascript but, this lack of structure and ease of use is apart of it’s tricky nature. Once you begin to do complicated things the quirks of the language start to arise: undefined oddities, antiquated array manipulation, limited standard library, strange object models, and for me personally as a Python programmer the mess of braces and semi-colons peppering my code with distracting syntax.

CoffeeScript is essentially a combination of a Ruby-esque syntax and a healthy dose of just the good parts from Javascript. What you get is a fairly clean language that always “compiles” into very readable and workable Javascript. Which means anywhere you need to use Javascript: in the browser, in node.js, on the iPhone; you can use CoffeeScript.

I’m going to be writing a few posts playing with CoffeeScript in the future but, right now I’ll give you a few “out-of-the-box” examples of CoffeScript that isn’t just a basic example

Node.js chat server in CoffeeScript

Appcelerator iPhone Application in CoffeeScript


23 Comments on “CoffeeScript: Why I’m never writing Javascript again”

  1. 1 Gary Bishop said at 4:08 pm on February 15th, 2011:

    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.

  2. 2 Tweets that mention DeGizmo » Blog Archive » CoffeeScript: Why I’m never writing Javascript again -- Topsy.com said at 7:02 pm on February 15th, 2011:

    [...] This post was mentioned on Twitter by azu, DeGizmo. DeGizmo said: #CoffeeScript : Why I’m never writing #Javascript again – http://bit.ly/e1OVuD [...]

  3. 3 alex dante said at 7:30 pm on February 15th, 2011:

    It took about a week to convince my co-workers of the value of Coffeescript. I’d only managed to get some of them interested in SASS/SCSS but everyone got on board with this.

    Cleaner, clearer code that compiles down to reviewable JS, it’s an essential tool for me now.

  4. 4 Adam said at 11:04 pm on February 15th, 2011:

    @Gary – You lose some control over variable scoping due to the var global by nature of JS which itself is actually quite a problem in large JS applications. I’d rather limit my globally scoped variables as much as possible anyhow

    If you do bump into global vs. scoped variable issues you can always explicitly declare an interior variable by appending a “this” to it via the CoffeeScript “@”

    Shown in this gist: https://gist.github.com/828841

    CoffeeScript isn’t perfect but, what it does do it make JavaScript easier to write in large quantity. Callback’s, nested {}, wrestling with undefined’s propagating around, etc… I’d rather wrestle the warts of Coffee vs. the holes of JS when writing a large single page application.

  5. 5 ilan berci said at 8:48 am on February 16th, 2011:

    @Gary,
    Ironically, what turned you off of CoffeeScript nailed it for me. You mentioned javascript scoping which is itself a mess. Javascript only supports functional scoping and not ‘block’ scoping so you always run into little surprises where some var you declared in a deeply nested if condition bites you in the a$$ when you scope out. Coffeescript negates this hard to find bug in large code bases and is a god send.

    Lastly, in JS it’s very easy to pollute the global namespace if you omit the ‘var’ decleration. Here again CoffeeScript comes to our rescue.

    I am very impressed with how CoffeeScript handled the scoping and it’s miles above JS in my opinion.

  6. 6 Doug Holton said at 10:33 am on February 16th, 2011:

    Here are some other languages that compile to javascript, too:

    https://github.com/jashkenas/coffee-script/wiki/List-of-languages-that-compile-to-JS

    A couple to check out include coco, which is way more radical than coffeescript (probably too much so for some though), and mobl, which is a statically typed language with a lot of nice features, like transparently converting synchronous code to asynchronous, mobile-friendly framework, and reactive programming (binding): http://www.mobl-lang.org/ It also integrates with Eclipse, supporting intellisense / code completion and so forth, although the most recent version works standalone as well.

  7. 7 Gary Bishop said at 2:26 pm on February 16th, 2011:

    @ilan,
    Help me understand how CoffeeScript “negates this hard to find bug in large code bases”, it seems to me it makes the problem worse because I have to “be careful”.

    I don’t see any advantage of eliminating the “var” keyword. It seems to me that it provides valuable redundancy and a clear indication of scoping (given that you understand it is function scope). Without var I can change the meaning of inner code by introducing a variable outside. That makes no sense to me.

  8. 8 Gary Bishop said at 2:28 pm on February 16th, 2011:

    @Adam,

    That is a neat trick with the @. I’m not sure I approve of abusing this that way but I can’t think of why right now.

    Thanks

  9. 9 leptons said at 5:57 pm on February 16th, 2011:

    Coffeescript is snake oil. It is not a net-positive in terms of improving a web development workflow. Consider all the time you spend fighting against the things coffeescript prevents you from doing and you will find that it causes more friction rather than speeding up your workflow. any serious developer investing in coffeescript needs to have their head examined.

  10. 10 Tom said at 9:07 am on February 17th, 2011:

    I’d be sold if I could debug in it; but the result is surely that you have to debug Javascript code that you didn’t directly write, right? That doesn’t sound like a great experience but I’m willing to be proved wrong by someone with experience of it.

  11. 11 Adam said at 10:46 am on February 17th, 2011:

    @Tom; Nope you are right. There’s no way to directly debug Coffee as it’s just a translation layer to JS. There is syntax checking and compile erros but, the resulting code is pretty readable and traceable JS so, I haven’t found debugging to be that hard at all.

  12. 12 Klawztrw said at 10:50 am on February 17th, 2011:

    In my opinion, the only problem with javascript is the lack of a standard library, and some more firepower to the arrays and objects. The javascript can provide aspects of functional programming and lambda functions and closures for those who want to deepen it.

  13. 13 Sterex said at 11:16 am on February 17th, 2011:

    This is the first time I’m hearing of CoffeeScript though. I’m surely less informed. However, I have to agree with @Tom.

    Debugging code that was written by someone else is a pain. Debugging a machine generated code is worse!

  14. 14 jm said at 3:25 am on February 18th, 2011:

    i’m afraid it’s a thumbs down from me too. the lack of debugging on it’s own would be enough, however, the fact that it doesn’t bring anything extra to the party kinda negates it’s usefulness for me….

    sorry.. :(

  15. 15 Oisín said at 5:00 pm on February 18th, 2011:

    @leptons: Could you give us any particular examples of “fighting against the things coffeescript prevents you from doing” and the friction it causes? Without some concrete examples, your post comes across as FUD somewhat.

    @Sterex: True, debugging machine-generated code can be tedious, but the (admittedly not very many) examples I’ve seen of CS->JS translation have been very clear and readable. It’s worth looking at some Coffeescript programs side-by-side with their compiled JS counterparts to see whether it will be too obtuse.

  16. 16 Rob said at 4:35 am on February 21st, 2011:

    I used coffee-script for a project a few months ago and ended up ditching it after a while for plain javascript.

    Don’t get me wrong, I love the syntax and extra features it gives you. But what really killed it for me was the way it breaks when you make a tiny syntax or indentation error. Tracebacks and inscrutable errors, usually lacking line numbers.

    If the error messages have gotten better and the code is more stable, I might consider using it again. I still think it’s a good idea for some projects – it’s the quality of the implementation I take issue with.

  17. 17 Weiting javascript | Filworld said at 10:24 pm on July 31st, 2011:

    [...] DeGizmo » Blog Archive » CoffeeScript: Why I’m never writing …Feb 14, 2011 … 2 Tweets that mention DeGizmo » Blog Archive » CoffeeScript: Why I’m never writing Javascript again — Topsy.com said at 7:02 pm on February … [...]

  18. 18 Daniel Sokolowski said at 5:23 pm on December 14th, 2012:

    I too am not sold on the CoffeeScript approach, it ads another layer of complexity – I rather deal with JS directly. I’ve also noticed the lack of `var` on the home page example and do share @Gary Bishop concerns.

    I will stick to a solid library like Google Closure instead. Interestingly GitHub writes their JS in it https://github.com/styleguide/javascript

  19. 19 Pierre Merlin said at 4:19 am on March 6th, 2013:

    Just to update your post, the coffee command do not accepts this flags order any more.
    You have to type `coffeee –watch –output js/ –compile coffees/`
    Cheers

    Pierre

  20. 20 Pierre Merlin said at 4:23 am on March 6th, 2013:

    Oups sorry, wrong blog post… where we experiment the benefit to place comments like a stack and not a queue.
    My 2cts.
    By the way I love Coffescript.

  21. 21 Sukima said at 10:39 am on July 15th, 2013:

    @Gary: First the quoted text refers to a problem even JavaScript has. If you reference a variable associated at a higher scope that is outside the current file (aka scope) then there is no way for any file based transpiler to know about the outer scope. You see this with files that depend on say jQuery. Some how, some where, $ is defined. There are ways around this of course (IIFE, do ($) ->, etc.) but in reality it isn’t needed. The point of the worrying is that if in the file your transpiling you use a variable that was not used prior it scopes locally as to be expected. Use IIFE like above if required. This is an edge case.

    For everyone else. Take care your development community embraces CoffeeScript. I found that when I wrote in coffeescript, transpiled, then submitted the generated output other JavaScript developers developed boils. The output is human readable but is clearly not a substitute for those who prefer JavaScript. If the team write CoffeeScript then the generated output is always hidden. If not they see it and will lynch you. Fair? No, the output is actually better JS then by hand but I think the output offends many on a core emotional level.

  22. 22 Trialasmins said at 5:18 pm on October 26th, 2013:

    Enter comment here.

  23. 23 rattan bo staff said at 11:27 pm on September 2nd, 2014:

    If you are asking that, let me answer promptly by saying Yes,
    you can get your ex boyfriend back in your life. As an experiment,
    maybe the next time you do a form, try modifying it using something else
    in your house. Kick-Boxing like sports, MMA, Judo,
    Tae Kwon Do, Kendo, and the like all need a ref.

    my homepage: rattan bo staff


Leave a Reply