On CoffeeScript and ES6
Last updated on Nov 17, 2014

The ECMAScript 6 specification is approaching its approval, and with it are a number of great features. Should we stop writing CoffeeScript altogether and start using the upcoming ES6?

It appears that it is still too early to put CoffeeScript in its grave. There is still something that makes CoffeeScript more attractive than vanilla JS for everyday use: its syntax.

Many great features of CoffeeScript have already been implemented in ES6, but CoffeeScript remains full of those little things that make everyday jobs easier:

  • You don't have to think about hoisting, vars, lets or returns.
  • Syntactic sugar such as not or unless keywords or one line conditionals
  • Code is cleaner without function, {}, and ;

There are some more awesome features.

Splat arguments:

race = (winner, runners...) -> print winner, runners

Existential operator:

alert "I knew it!" if elvis?

Conditional assignments:

date = if friday then sue else Jill

Chained comparisons:

healthy = 200 > cholesterol > 60

Even with all these great features, many reasons to stick with plain JavaScript over CoffeeScript have been made. Let’s take a look at them:

Curly braces are fine

Most JS programmers don't understand what Coffee programmers have against curly braces or semicolons. The C-like syntax is just fine. How can a couple of extra characters be an argument against language quality?

In practice, less characters means less code, which could result in less bugs. It's easier to catch errors when you have less visual noise. Navigating a file is easier with 20 lines of code rather than 50. Also CoffeeScript won't let you write un-indented code, no matter how lazy you are.

Of course, it is mostly a matter of choice. Subjectively, I enjoy my code when it's more human readable. Some may believe that expressiveness and aesthetics of code, especially with syntactic sugar libraries like SugarJS, are not really rational arguments in a JavaScript vs. CoffeeScript discussion.

CoffeeScript is harder to understand than plain JS

It is largely a matter of habit. CoffeeScript does take some learning, but so will the new EcmaScript 6 syntax. It is sometimes hard to see the benefits of it before you start using it for real.

One of the arguments against CoffeeScript syntax was that it is too obscure for JS programmers and only understandable by people with backgrounds in languages such as Ruby. Now, as some of the CS syntax is part of the future of JS itself, I believe that this argument has been put to rest.

It doesn't support ES6 features

Yet. There is no logical reason to believe that good ES6 features won't be also implemented in CS once they become widely supported. Until then, there is no reason to make a language that compiles into another compiled language.

You need a pre-compiler

One of the strongest argument against CoffeeScript is that you will need a compiler for it forever, while the need for the ES6 compiler will be eventually eliminated.

If you use SCSS or Autoprefixer with Grunt or, even better, Gulp, to handle compilation (and you probably should) then using CoffeeScript is as simple as writing a few lines of code and you are ready to go. And with sourcemaps, you can also debug it easily in browser.

By the way, if you look at the ES6 Compatibility Table, you will see that there are still too many red cells to say that you won't need a compiler in the near future. It may take months if not years before all browsers will support all features of the next ECMAScript spec.

Most of the world uses JavaScript

This is just as true as the fact that most of the world also uses PHP and .net. But the lower entry level of Javascript also comes with the price of too many inexperienced programmers who write thousands of lines of bad code and then release them as unusable, buggy bower components.

The truth is, there are people out there using CoffeeScript to write awesome products like Atom, Discourse, Github, Dropbox, Hubot, Pow, or Koding IDE.

The bottom line

Both sides of this discussion have reasonable arguments, and choosing between CoffeeScript and plain JS still remains a matter of opinionated choice. Personally, as a Ruby programmer, I would be happy to write Ruby, be it Opal for web development or mruby for Arduino. But we live in a world where JavaScript has taken the leadership.

You will have to know JavaScript anyway. At the end, it's just what CoffeeScript is. But CS can save you a lot of nerves and time. If you already use Coffee, the bottom line is don't rush to rewrite everything with the cutting-edge ES6. The odds are that future versions will support ES6 features. And if you don't yet code in CoffeeScript, learning it may not be a bad idea.

Back to all articles
We are leading front-end consultancy specializing in React, React Native, and Angular. We build web and mobile apps, train teams and consult on complex projects.

Tel AvivIsrael

New York CityUSA

© 500Tech. Building high-quality software since 2012.