VARUNA JAYASIRI

@vpj

Should we switch from CoffeeScript?

September 28, 2016

A little background

At nearby.lk we moved our backend from python to node.js and then rewrote both the backend and UI code in CoffeeScript in late 2013. And we migrated all Javascript code to CoffeeScript at Forestpin in early 2014.

Then we replaced all HTML files and generated HTML content in CoffeeScript; initially with Coffeecup and then with Weya.coffee. So we only have Coffeescript and CSS in our projects.

Why we are considering to switch

There are two main reasons. First is that CoffeeScript is untyped. The second is the uncertainty of the future of CoffeeScript.

CoffeeScript is untyped and so is Javascript. This becomes a problem when you have a large code base. There can be hidden errors not covered by unit tests. Refactoring and making modifications is harder. Also, editors can give better code completion with typed languages. A typed language can introduce extra optimizations too, but I don't think this is done with likes of TypeScript.

The popularity of CoffeeScript is fading. There are a lot of posts about people moving away from CoffeeScript, and almost nothing about people adopting CoffeeScript. If CoffeeScript loses popularity there'll be fewer contributions to the CoffeeScript compiler project; it might not get updated along with developments of Javascript. However, CoffeeScript has so far adopted all new features of Javascript we found to be important (e.g. generators, modules).

What we will miss

We are certainly going to miss markup in CoffeeScript. Generating HTML text from Javascript is clean (with string concatenations and HTML tags).

 @div ".users", ->
  for user in users
   @div '.user', on: {click: editUser}, ->
    @span ".name", user.name
    @span ".phone", user.phone
    if v.image?
     @img src: user.image

The other thing we are going to miss is executable class bodies. This let us have mixins, bind this to event handlers, and even do things like check if all abstract methods are implemented. We use these quite often in our code.

What are we going to do

There was a new release of CoffeeScript, and there are discussions about CoffeeScript2 and about compiling CoffeeScript to ES6. So, hopefully, CoffeeScript will have a future.

Since the only immediate benefit of switching from CoffeeScript to TypeScript is type checking, we will stay with CoffeeScript for a while.

To keep our options open, I started a small side project in TypeScript. Apart from not having a nice way to generate HTML, the experience so far has been good. Typescript is less error prone and the editor code completion saves time. But the code is a lot longer (lines of code) and to me doesn't look as elegant as CoffeeScript - perhaps I need some more getting-used-to.

Coffeescript will stay untyped for a while - Type annotations are marked as "no action".

##A little background At <<http://nearby.lk(nearby.lk)>> we <<http://blog.varunajayasiri.com/app_engine.html(moved our backend from python to node.js)>> and then rewrote both the backend and UI code in CoffeeScript in late 2013. And we migrated all Javascript code to CoffeeScript at <<https://www.forestpin.com(Forestpin)>> in early 2014. Then we replaced all HTML files and generated HTML content in CoffeeScript; initially with <<http://coffeekup.org/(Coffeecup)>> and then with <<https://github.com/vpj/weya(Weya.coffee)>>. So we only have Coffeescript and CSS in our projects. ##Why we are considering to switch There are two main reasons. First is that CoffeeScript is untyped. The second is the uncertainty of the future of CoffeeScript. **CoffeeScript is untyped** and so is Javascript. This becomes a problem when you have a large code base. There can be hidden errors not covered by unit tests. Refactoring and making modifications is harder. Also, editors can give better code completion with typed languages. A typed language can introduce extra optimizations too, but I don't think this is done with likes of <<http://www.typescriptlang.org/(TypeScript)>>. >>> Coffeescript will stay untyped for a while - <<https://github.com/coffeescript6/discuss/blob/master/Features.md(Type annotations are marked as "no action")>>. **The popularity of CoffeeScript is fading.** There are a lot of posts about people moving away from CoffeeScript, and almost nothing about people adopting CoffeeScript. If CoffeeScript loses popularity there'll be fewer contributions to the CoffeeScript compiler project; it might not get updated along with developments of Javascript. However, CoffeeScript has so far adopted all new features of Javascript we found to be important (e.g. generators, modules). ##What we will miss We are certainly going to miss **markup in CoffeeScript**. Generating HTML text from Javascript is clean (with string concatenations and HTML tags). ```coffeescript @div ".users", -> for user in users @div '.user', on: {click: editUser}, -> @span ".name", user.name @span ".phone", user.phone if v.image? @img src: user.image The other thing we are going to miss is **executable class bodies**. This let us have mixins, bind ``this`` <<https://github.com/vpj/weya/blob/master/base.litcoffee(to event handlers)>>, and even do things like check if all abstract methods are implemented. We use these quite often in our code. ##What are we going to do There was a new release of CoffeeScript, and there are discussions about CoffeeScript2 and about compiling CoffeeScript to ES6. So, hopefully, CoffeeScript will have a future. Since the only immediate benefit of switching from CoffeeScript to TypeScript is type checking, we will stay with CoffeeScript for a while. To keep our options open, I started a small side project in TypeScript. Apart from not having a nice way to generate HTML, the experience so far has been good. Typescript is less error prone and the editor code completion saves time. But the code is a lot longer (lines of code) and to me doesn't look as elegant as CoffeeScript - perhaps I need some more getting-used-to.