2011-07-11

CoffeeScript – overrated?

There has been an interesting thread on Reddit with the title “DAE [Does Anyone Else] think CoffeeScript is ridiculously overrated?” [via @k33g_org]. This post provides some perspective on that opinion.

CoffeeScript is a modern version of JavaScript: Check out its excellent website for a quick overview of what it looks like.

  • Completely new syntax: CoffeeScript comes with its own syntax that looks a lot like Python: it uses indentation instead of braces, parentheses and commas are often optional, etc.
  • Improves JavaScript: CoffeeScript helps with many of JavaScript’s more difficult parts – inheritance, shorter function syntax with optional binding of this, etc.
  • Compiled to JavaScript, statically. When you write CoffeeScript, there is an extra build step involved, before you can run your web application: you need to compile it to JavaScript. You will thus have to do your debugging in JavaScript, but extra emphasis has been placed on making the generated code as readable as possible. [2]

CoffeeScript is influential:

  • Will be part of Rails. CoffeeScript will be shipped with Ruby on Rails 3.1.
  • Influences ECMAScript.next. It can be seen as a field test of next-generation JavaScript features and informs the decisions made for ECMAScript.next. Especially Brendan Eich has mentioned it as a source of inspiration.
Caveats:
  • Syntax. While I think that there are syntaxes that are prettier than the Java-inspired JavaScript syntax (Smalltalk and Lisp come to mind), it is what we have and it is not so bad that one cannot live with it. I don’t agree with the design choice of choosing a completely different syntax for an advanced version of JavaScript, because you now have to know two syntaxes: CoffeeScript’s for programming and, occasionally, JavaScript’s, for debugging. Obviously, there are people who hate JavaScript’s syntax and/or don’t want to stray too far from their favorite programming language. For those people, this is a feature, not a bug. I would have preferred a more JavaScript-ish approach where upcoming ECMAScript features are compiled to current-day JavaScript. This is what Google Traceur [1] does. Note that if JavaScript had macros, one could retrofit older engines with both new library functionality and new language features.
  • Library versus language. Not all features have to be part of the core language. With JavaScript’s higher-order functions (functions/methods whose parameters are functions), libraries can do a lot. The following example shows that the language feature list comprehension is not much more beautiful than Array.prototype.map() plus shorter function syntax. This is CoffeeScript code that produces a list of square numbers:
        list = [1, 2, 3, 4, 5]
        squares = (Math.sqrt num for num in list)
    
    In JavaScript code (plus shorter function syntax), this looks as follows:
        var list = [1, 2, 3, 4, 5];
        var squares = list.map((num) -> Math.sqrt(num));
    
  • Not everything is an improvement. We are obviously entering the realm of taste, but I don’t think all of CoffeeScript’s features are an improvement over current-day JavaScript [3]. For example, I like having to declare a variable via the var operator (soon to be replaced by the improved let operator), because it makes it explicit where you start using it and helps with catching typos.
The future:
  • ECMAScript.next [4] will fix most of JavaScript’s shortcomings: modules (which are currently not in CoffeeScript), classes, shorter function syntax, etc.
  • Paren-free JavaScript. Brendan Eich has suggested a more compact syntax for JavaScript called paren-free JavaScript [3]. It provides some of CoffeeScript’s conciseness, but without radically changing the syntax. The key insight is that parentheses are not necessary if an expression is bracketed by a keyword (such as if) and a brace. Thus, blocks for if and loops would have to be mandatory to enable this syntax, but that is what most style guides suggest, anyway.
        if year > 2010 {
            syntax++
        }
        
        for i in iter {           // i is a fresh let binding!
            frob(i)
        }
        
        while lo <= hi {
            let mid = (lo + hi) / 2
            // binary search blah blah blah
        }
    
  • Compile CoffeeScript in your browser. There are two ways to run CoffeeScript directly in the browser:
    • Running CoffeeScript In-Browser
      One of the ways to run CoffeeScript (other than processing on the server or using the command line to pre-compile) is to run CoffeeScripts, directly, in-browser.

      This simply means that you can write coffeescript inside the HTML document. It has the disadvantage of being near impossible to debug as the running code will be encased in a very large javascript eval statement.

    • CoffeeMaker
      It runs on most modern LAMP servers (I use it on my localhost development server, XAMPP) and compiles CoffeeScript on the client but caches and stores it on the server.
  • Firefox SourceMap [5] allows you to debug an input language that is compiled to JavaScript in its source code.
Related posts:
  1. Google’s Traceur: compile ECMAScript.next to JavaScript on the fly
  2. Translating CoffeeScript classes to JavaScript [examines the JavaScript generated by CoffeeScript]
  3. CoffeeScript versus paren-free JavaScript [I compare the two approaches of syntactically improving JavaScript and rate CoffeeScript’s new language constructs]
  4. ECMAScript.next: the “TXJS” update by Eich
  5. SourceMap on Firefox: source debugging for languages compiled to JavaScript

13 comments:

Pablo Vazquez Vidal said...

"var squares = list.map((num) -> Math.sqrt(num));"

lol, you just wrote "->" from coffeescript in the middle of the javascript example? :) Coffeescript is not only great, it produces very optimized and idiomatic Javascript, and by the way, it took me aproximatelly half a day to learn the syntax, in the end Coffeescript is just a really nice idiom of Javascript that increased my productivity (and that means that now I have more free time!) in several scale orders.

My 2 cents :)

Axel Rauschmayer said...

"lol, you just wrote '->' from coffeescript in the middle of the javascript example?"
That was intentional (look at the sentence above the code example). This notation might make it into ECMAScript.next. The point was that, if you have shorter function syntax, you can use map() (=library) instead of list comprehensions (=language feature).

rs31 said...

It's great to hear that CoffeeScript is helping amateur devs and bikeshedders sleep at night.

Davorin said...

but calling a function is slower, isn't it?

Axel Rauschmayer said...

Not necessarily. Modern JITs do all kinds of clever stuff, including inlining functions.

Yourmama said...

CoffeeScript sucks.

Who the fuck want to read Code intented by WHITESPACES?

Thats the worst point on Python, also.

Commonbullet said...

Crap, I'm missing the point. I don't think Coffeescript syntax is better than Javascript, it's confusing. I don't want to learn a script language in order to compile into another script language I already know. I can understand these syntax "layers" languages making it simpler for some people (a matter of taste?), but why so many people are enthusiastic about it? I confess I must be missing something.

claudio said...

I use to think like you about Python...
but then I grow up ;)
CoffeScript syntax is clearer for me and I loved that most of the things are expressions with a value you can use...

yoazt said...

Agree... The thing I don't get is that people consider CoffeeScript a 'new language'. It's not - it gets transformed into JavaScript, meaning that it's limited by exactly the same limitations as JavaScript itself. It just tries to recreate a syntax that Ruby-developers (including myself) love. It adds nothing.

It's as if I'd speak English, but use Dutch idiomatic phrases everywhere. No English-speaking person 'sits here-on to wait' (see? it means 'will find this useful' but it makes no sense). If you want to use JavaScript, learn it, and learn it well, otherwise your app will not work nicely. It just won't, no matter how nice your arrows and lambda syntaxes look to you.

Foljs said...

Well, you can indent it with TABS, as many do.

The question is "who the fuck wouldn't want to read code indented either with tabs or spaces"?

Because, once you understand that indentation is necessary, braces become less so. 

pelosi said...

I'm glad to hear I'm not the only one that doesn't understand what's so am-ah-zing about CoffeeScript. When I first learned about SASS, I was immediately excited. It gave me tools that didn't previously exist in CSS.

Then I learned HAML and while I understand why some people like it, it doesn't actually add any value to the process. If I have to debug someone else's code (or code that I had long since jettisoned from my brain), ERB is far less cryptic to me. 

And now CoffeeScript. It doesn't feel like Ruby to me and it doesn't feel like JavaScript. It's just one more layer of obfuscation with syntax that seems arbitrary. It's not as if JavaScript is so hard to learn. I don't understand why everyone jumped on the CoffeeScript bandwagon like JavaScript was some arcane and indecipherable language. 

Peter Hanley said...

I originally thought it was stupid, but I've found that meaningful indentation helps me think about my structure better. Or maybe I just drank the kool aid.

Ezekiel Victor said...

"It adds nothing" is a rather misguided statement which clearly indicates you haven't taken the 5 minutes to so much as scan the CS home page.

CS, referring to itself as "just JavaScript" and a "little language" (claims not nearly as grandiose as you're imagining) has plenty of features that definitely "add something", notably classical inheritance (which isn't one of the 500 different kludgy JS solutions no one can seem to agree on), for comprehensions, string interpolation, and existential accessor -- tools that any modern JS developer can immediately benefit from.

I don't think I've ever heard anybody suggest learning it as an alternative to JS. Ironically, if you don't understand the fundamentals of JS, you're more likely to hurt yourself *not* using CoffeeScript because you'll end up shadowing globals or writing weak comparisons that result in unexpected bugs. (Bear in mind those design decisions in CS were made _not_ to shield noobs from JS pitfalls, but rather to strip away oft unnecessary language "features," and perhaps help humans who know JS plenty well but may once in a while make a simple mistake -- maybe that doesn't apply to you. :)

It sounds like you and pelosi haven't entertained the notions of functional programming and/or object-oriented JS; if you had, you'd surely recognize the CS features inspired therefrom, or in pelosi's words: "what's so am-ah-zing."

Web Analytics