The New Kid in Town

On Christmas Day 2009, Jeremy Ashkenas first released CoffeeScript, a little language he touted as “JavaScript’s less ostentatious kid brother.” The project quickly attracted hundreds of followers on GitHub as Ashkenas and other contributors added a bevy of improvements each month. The language’s compiler, originally written in Ruby, was replaced in March 2010 by one written in CoffeeScript.

After its 1.0 release on Christmas 2010, CoffeeScript became one of GitHub’s “most watched” projects. And the language attracted another flurry of attention in April 2011, when David Heinemeier Hansson confirmed rumors that CoffeeScript support would be included in Ruby on Rails 3.1.

Why did this little language catch on so quickly? Three reasons come to mind: familiarity, safety, and readability.

The Good Parts Are Still There

JavaScript is a vast language. It contains multitudes of features, which obscure its essence. JavaScript offers many of the benefits of a functional language while retaining the familiar feel of an imperative one. This subtle power is one of the reasons that JavaScript tends to confound newcomers. Functions can be passed around as arguments and returned from other functions, and objects can have new methods added at any time. In short, functions are first-class objects.

All that power is still there in CoffeeScript, but with a syntax that encourages you to use it wisely. Gone is the function keyword, along with its accompanying curly braces. Instead, blocks of function code are demarcated with -> or => and indentation. Likewise, the crucial yet often baffling this keyword has a distinctive symbol to serve in its stead, @. These are small changes, but they go a long way toward making the way a program works more obvious.

The Compiler Is Here to Help

Imagine a language with no syntax errors, a language where the computer forgives you your typos and tries as best it can to comprehend the code you give it. What a wonderful world that would be! Sure, the program wouldn’t always run the way you expected, but hey, that’s what testing is for.

Now imagine that you write that code once and send it out to the world, typos and all, and millions of computers work around your small mistakes in subtly different ways. Suddenly statements that your computer silently skipped over are crashing your entire app for thousands of users.

Sadly, that’s the world we live in. JavaScript has no standard interpreter. Instead, hundreds of browsers and other environments run JavaScript in their own way. Debugging cross-platform inconsistencies is a huge pain.

CoffeeScript can’t cure all of these ills, but the compiler tries its best to generate JavaScript Lint-compliant output,[3] which is a great filter for common human errors and nonstandard idioms. And if you type something that just doesn’t make any sense, such as 2 = 3, the CoffeeScript compiler will tell you. Better to find out sooner than later.

It’s All So Clear Now

Writing CoffeeScript can be highly addictive. Why? Take this piece of JavaScript:

 function​ cube(num) {
 return​ Math.pow(num, 3);
 }
 var​ list = [1, 2, 3, 4, 5];
 var​ cubedList = [];
 for​ (​var​ i = 0; i < list.length; i++) {
  cubedList.push(cube(list[i]));
 }

Now here’s an equivalent snippet of CoffeeScript:

 cube = (num) -> Math.pow num, 3
 list = [1, 2, 3, 4, 5]
 cubedList = (cube num ​for​ num ​in​ list)

For those of you keeping score, that’s half the character count and less than half the line count! Those kinds of gains are common in CoffeeScript. And as Paul Graham once put it, “Succinctness is power.”[4]

Shorter code is easier to read, easier to write, and, perhaps most critically, easier to change. Gigantic heaps of code tend to lumber along, as any significant modifications require a Herculean effort. But bite-sized pieces of code can be revamped in a few swift keystrokes, encouraging a more agile, iterative development style.

It’s worth adding that switching to CoffeeScript isn’t an all-or-nothing proposition—CoffeeScript code and JavaScript code can interact freely. CoffeeScript’s strings are just JavaScript strings, and its numbers are just JavaScript numbers. Even its classes work in JavaScript frameworks like Backbone.js.[5] So don’t be afraid of calling JavaScript code from CoffeeScript code or vice versa. We’ll talk about using CoffeeScript with two of JavaScript’s most popular libraries in Chapter 5, Web Applications with jQuery and Backbone.js .

But enough ancient history. Coding is believing, everything else is just meta, and as Jeff Atwood once said, “Meta is murder.”[6] So let’s talk a little bit about the book you’re reading now, and then—in just a few pages, I promise!—we’ll start banging out some code.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.189.171.153