Blog

  • Home /
  • Blog /
  • Exploring CoffeeScript Part 1 – And Then There Was Coffee

Exploring CoffeeScript Part 1 – And Then There Was Coffee

June 21, 2011

Those of you who have been reading this blog lately know that I’m quite enthusiastic when it comes to JavaScript. As the common behavioral language of the web, JavaScript has been making its way into other aspects of application programming over the last couple of years, notably with server-side frameworks like Node.js and NoSQL databases like CouchDB. I personally find JavaScript to be an awesome programming language, especially when you stick with the good parts. So I’m more than pleased that JavaScript is finally making its way out of the browser to fulfill other purposes as well.

But a couple of months ago I ran into another programming language called CoffeeScript. This neat little language, created by Jeremy Ashkenas, simply compiles its syntax into JavaScript code. In fact, there’s no interpretation of CoffeeScript code at runtime. CoffeeScript essentially builds on top of JavaScript as the core language while adding some syntactic niceties that are heavily inspired by Ruby. The CoffeeScript compiler emits very clean JavaScript code that not only follows best practices but also complies to JSLint without warnings. This all basically means that wherever you can use JavaScript, whether it’s in the browser, on the server-side or for a particular database, you can use CoffeeScript as well. In fact, you can use any JavaScript library right in your CoffeeScript code!

But why should one use CoffeeScript over JavaScript? Well, for starters, the CoffeeScript syntax is much more concise than its JavaScript equivalent. There’s a lot less typing involved for getting the equivalent JavaScript code. Check out this example on the CoffeeScript wiki.

Second, the CoffeeScript language is much richer than JavaScript. It add splats, classes, destructuring assignments and many more small details that makes your code more readable and fun. 

And thirdly, because the creator of Ruby on Rails, David Heinemeier Hansson, says so :-). The upcoming 3.1 release of Ruby on Rails will include CoffeeScript in the box, which means that other web frameworks will probably also add support for CoffeeScript in the future.

Enough with the marketing talk already. Let’s have a quick look at how to get CoffeeScript up and running. For starters, you need to have Node.js and npm installed. Check out the installation guide or Matthew Podwysocki's on how to get Node.js up and running on Windows using Cygwin.

When you have node.js all set up, you can simply use the following command to install the CoffeeScript compiler using npm:

npm install -g coffee-script

Now that we have CoffeeScript installed, we can compile .coffee files into .js files or we can use the interactive REPL. Let’s go for the canonical “Hello Pluto” example here and create a file named hello.coffee with the following code:

Notice that we don’t have to provide parentheses here in order to call the console.log function. Next we can use the following command to print the compiled JavaScript directly to our console window:

coffee -p hello.coffee     

or we can use this next command to compile our hello.coffee file to a corresponding hello.js file at the same location in the file system:

coffee -c hello.coffee

The compiled JavaScript output looks like this:

console.log 'Hello CoffeeScript'

You might wonder why the call to console.log is wrapped into a self-executing function. The reason for this is to prevent naming collisions when using our code in conjunction with other JavaScript libraries. You can tell the CoffeeScript compiler to omit this self-executing function by specifying the –b, –bare command-line option.  

We’re also able to keep the CoffeeScript compiler running with the –w, --watch command-line option. This basically lets the CoffeeScript compiler watch for the specified .coffee files and recompile them as soon as they’re changed.

coffee -cw hello.coffee   

So these are the very basics on how to get up and running with CoffeeScript. Expect to see more of the interesting stuff that this amazing programming language has to offer as I’m going to be writing a couple of more blog posts about CoffeeScript. I certainly recommend that you have a decent knowledge of JavaScript before you pick up CoffeeScript. Being able to understand the JavaScript code that is emitted by the CoffeeScript compiler can be crucial at times.

Until next time.

If you and your team want to learn more about how to write maintainable unit tests and get the most out of TDD practices, make sure to have look at our trainings and workshops or check out the books section. Feel free to reach out at infonull@nullprincipal-itnull.be.

Profile picture of Jan Van Ryswyck

Jan Van Ryswyck

Thank you for visiting my blog. I’m a professional software developer since Y2K. A blogger since Y2K+5. Provider of training and coaching in XP practices. Curator of the Awesome Talks list. Past organizer of the European Virtual ALT.NET meetings. Thinking and learning about all kinds of technologies since forever.

Comments

About

Thank you for visiting my website. I’m a professional software developer since Y2K. A blogger since Y2K+5. Author of Writing Maintainable Unit Tests. Provider of training and coaching in XP practices. Curator of the Awesome Talks list. Thinking and learning about all kinds of technologies since forever.

Contact information

(+32) 496 38 00 82

infonull@nullprincipal-itnull.be