Software often requires two or three iterations before you get it right. In our case this led to starting a rewrite of our hybrid mobile application. It had been developed over several years by a diverse group of people with varying coding practices, and it was deemed easier to rewrite it than to refactor it into a maintainable state.
Nowadays there are a multitude of language alternatives for JavaScript development. These range from simple syntax enhancements to full-blown language alternatives. After a bit of searching, we decided to evaluate three JavaScript alternatives for our rewrite: CoffeeScript, TypeScript and Dart.
Below is the results of our comparison between the three. This is not an extensive functional evaluation (a luxury not available for startups), but instead based primarily on reading the following books:
- The Little Book on CoffeeScript, by Alex MacCaw
- TypeScript Revealed, by Dan Maharry
- Dart: Up and Running, by Kathy Walrath & Seth Ladd
I can recommend these as good introductions to each language, as each one can be read in a few hours. You may also be interested in Anton Ivanov’s blog post comparing the three with code examples.
CoffeeScript – a fresh coat of paint
CoffeeScript is one of the oldest altJS languages and probably the most popular one. The slogan of the language is “It’s just JavaScript”. It is essentially an alternative syntax for writing JavaScript, and compiles one-to-one into equivalent JavaScript.
The goal of CoffeeScript is to expose the good parts of JavaScript and clean out the bad. For example, one thing that bites many inexperienced JavaScript developers is the type coercion when using ==. CoffeeScript removes this complexity by always converting == into ===. There is no possibility of accidentally using type coercion, you have convert types explicitly.
The syntax takes a large step away from JavaScript, most notably by the -> operator and using indention scoping. People with a background in C/Java-like languages may find this disturbing at first, Python coders will find it natural. While the -> lambda operator may take some getting used to, it reduces a lot of boilerplate code related to nested function definitions, common in event-driven languages:
$(document).ready -> $.get '/', (data) -> $('body').append "Successfully got the page."
The language has many other influences from scripting languages like Python, Perl and Ruby, such as post-conditionals, operator aliases and string interpolation to name a few. It heavily promotes brevity of code, with the risk of it becoming cryptic to non-experts.
CoffeeScript also provides a framework for classes and inheritance. One of the greatest pitfalls of JavaScript is having too many ways of doing things, and class definitions is one of the worst. CoffeeScript’s class framework allows working with classes in a consistent and easy manner.
Since CoffeeScript compiles directly into corresponding JavaScript, any external libraries can be used without any problems.
Overall, CoffeeScript provides a new syntax for JavaScript, promoting brevity and hiding many of the ugly bits of JavaScript. It is also one of the most common altJS languages, so documentation and support should not be hard to find.
Nonetheless, as a direct language conversion it cannot fix all of JavaScript’s faults. The syntax may appear rather geeky, excess cleverness using the syntax may backfire, and it can take some getting used to for people coming from Java-like languages.
TypeScript – the JavaScript of tomorrow
TypeScript is Microsoft’s take on altJS. It implements proposals of future ECMAScript standards today, allowing the compiled code to run on existing JavaScript interpreters.
The primary new language features provided are (optional) static typing and a class framework. The type system allows catching a wide variety of problems during compile time, instead of runtime. The class and module framework allows easier modularization and writing of complex applications.
Microsoft has also developed comprehensive IDE support for TypeScript for Visual Studio. The static typing allows it to perform many advanced refactoring operations, such as global renaming, reference searching and code completion, enough to make any scripting language developer envious.
TypeScript is the only one of the three that is a strict superset of JavaScript. This means that any valid JavaScript code is also valid TypeScript, and compatibility with existing libraries is a no-brainer. Also, if the proposals to ECMAScript 6 are accepted, your code will be valid (or almost valid) ECMAScript 6 code when it comes out.
This compatibility, however, is also the crux. As a superset, TypeScript cannot hide any of JavaScript’s ugly sides. All of the pitfalls of JavaScript are there lurking, waiting for you to step in. The static typing and Visual Studio plugin do a lot to alleviate the problems, though.
TypeScript is essentially a look at what JavaScript may be in a few year’s time. The addition of a type system, classes and modules, and an IDE that takes full use of these features makes it a tempting option to plain JavaScript.
Dart – a fresh look at the web
Dart is Google’s proposal for the future of the web. Dart takes the separation from JavaScript even further, and is a whole new language, designed from the ground up. It’s what JavaScript might look like if it were designed today.
The Dart language is not specifically designed for a browser, but more as a general usage language. Browser-specific functions, such as DOM manipulation, don’t rely on magic global variables, but are provided as libraries. The language resembles Java in many ways, and most programmers should be comfortable with it in a a day or two.
The fact that Dart compiles into JavaScript is about the only link between the two languages. In fact, the long-term goal is to have browsers natively implement Dart support – a version of Chromium with a Dart VM already exists.
While a clear distinction from JavaScript allows creating a sleek, modern language, it does come with a cost. Utilizing existing JavaScript code and libraries is more difficult, you need to access them through an interoperability library. You’re also further away from the code the browser sees. If there’s a problem on some obscure browser, it may be difficult to track it down and fix. Experienced JavaScript developers might also feel frustrated with the added layer between them and the browser.
Which one to choose?
So, which one is the best? As always, that depends.
If we were using Microsoft tools for development, TypeScript would be a very tempting choice. As it is, we’re doing the majority of our development on Linux and Macs using Eclipse. A major part of the benefit of TypeScript comes from the tooling within Visual Studio. As support is developed for more IDEs this may change – for example WebStorm 6 from JetBrain already includes TypeScript support.
If I was starting a new desktop web app, I would definitely consider Dart. It’s a very clean, modern language with extensive libraries out-of-the-box. However, our application must also run on rather old mobile browsers, and interface properly with our native code. If we later find out that Dart code doesn’t run properly or fast enough on some devices, we may be at a dead end. This is a risk we didn’t want to take.
For these reasons we ended up using CoffeeScript. While I don’t think it’s an ideal language, it does clean up a lot of JavaScript’s mistakes, reduces unnecessary code and provides structure to the code. Since it is close to regular JavaScript, we can be sure it won’t cause interoperability or performance issues.
An added benefit is that CoffeeScript is probably the most widely adopted altJS language, and there’s a considerable amount of manuals and explanations for it, and more developers in the community to answer questions. Without any clear numbers, Google Trends provides some idea on the relative interest in each language.
In short, CoffeeScript was a safe choice that is nonetheless considerably better than plain JavaScript.
Which language have you chosen for your projects?
