/* jquery */ /* jquery accordion style*/ /* jquery init */

26 July 2013

TypeScript & CoffeeScript: A Better JavaScript

JavaScript is a hugely popular language. Its scope extends well beyond the classic web development scenario. Today developers are able to create cross-platform mobile apps and full-sized applications in pure JavaScript.

Google, Appcelerator, PhoneGap and many others have active, well resourced JavaScript-based projects and compilers are springing up all over the place.

Nevertheless JavaScript coding can be a challenge, even for the professional developer. As code listings grow in size and complexity the language's many shortfalls induce to a steep increase in bug count and raise developer frustration.

Using a quality IDE will certainly help. But, how about a new language with a cleaner, modern-looking syntax? One that requires less lines of code for the same functionality, yet is still 100% JavaScript compatible.

Here I'll take a brief look at two, TypeScript and CoffeeScript.

TypeScript version 0.9 was unwrapped in front of Microsoft's developer community during Microsoft's Build 2013 event by Anders Hejlsberg.

In common with many recent Microsoft initiatives there's a strong lean towards openness, exemplified by the Apache 2 licence and CodePlex-hosted source code. In fact, TypeScript is a preview of what to expect in the upcoming ECMAScript 6 standard.

The open source, open specification, cross-platform compiler is itself written in TypeScript. Type declarations are removed by the compiler and the ECMA standard JavaScript output runs in any browser and on any host.

TypeScript is a superset of JavaScript. So developers can start with a JavaScript listing then use whichever TypeScript features seem most appropriate. The addition of types and generics, plus improved class definitions and 'intellisense' support allow developers to create well structured code and introduce fewer bugs.

TypeScript header files add type information to existing libraries. A github-hosted project called DefinitelyTyped already contains an impressive collection of '.ts' definition files covering many popular libraries and frameworks, including jQuery, MongoDB, Node.js and D3.js.

There are IDE plugins for Visual Studio and WebMatrix, plus support for editors such as Sublime Text, Emacs and Vim editors. However, at present only Microsoft's Visual Studio 2012/2013 tool delivers comprehensive support for all TypeScript's features.

To discover more watch Anders Hejlsberg's hour long Build 2013 talk or navigate to the TypeScript preview website and select the 'Play' mode.

CoffeeScript appeared in 2009 when Jeremy Ashkenas placed an early version on GitHub. The stable 1.0 version appeared in December 2010.

CoffeeScript aims to increase developer productivity via syntax influences from Python, Ruby and Haskall. It's a succinct language, with noticeably less syntactic fluff. Consequently, a CoffeeScript listing is not only easier to read, understand and debug, but typically requires less than half the code when compared with JavaScript.

CoffeeScript code is trans-compiled into pure JavaScript. This means any Javascript framework or library can be referenced, including the ever popular jQuery and Prototype products. There's support in Node.js and Ruby On Rails version 3.1, plus an online tool to convert existing JavaScript code into CoffeeScript.

To find out more checkout the free open source introduction to CoffeeScript.

Click to read more analysis articles and posts.