Home

CoffeeScript Implementation of Conway's Game of Life

Over the weekend I wanted to get a bit deeper into the CoffeeScript and get around how it feels working with it. I gave myself task to do something simple  but inspiring. Therefore I implemented simple version of Conway’s Game of Life to be played in browser. Go here to see how it looks like.

CoffeeScript

CoffeeScript is a little language that compiles into JavaScript. Underneath all of those embarrassing braces and semicolons, JavaScript has always had a gorgeous object model at its heart. CoffeeScript is an attempt to expose the good parts of JavaScript in a simple way.
In other words, CoffeeScript is an attempt to replace JavaScript syntax with a better one based on experiences from Ruby and Python worlds. Beside that it introduces couple of new abstractions which natively do not exist in the vanilla JavaScript like classes, list comprehensions, return of multiple values from function etc.

CoffeeScript brings a lot of relaxation on the original JavaScript syntax and makes writing it at first weird experience but only until you get used to it. Afterwards it gets obvious that it takes much less to write the same thing in CoffeeScript than in plain JavaScript.

My observations on developing with CoffeeScript

First and obvious observation is that with CoffeeScript you need additional tool in your toolset which will continuously compile your work to JavaScript. I used Jasmine for writing tests and I based my setup on this gist which proposes using guard Ruby gem for tracking changes and compiling CoffeeScript files into JavaScript ones.

It looked a bit weird at start, but after couple of hours playing it started getting better. One thing I had always to do is to look in the actual compiled JavaScript to see how to do some things i.e. how to pass index to function that maps an array. But it was intuitive enough that I almost didn’t need official documentation. Here I have to tell that it might not be as intuitive to people without experience with writing Ruby and Python code or lack of understanding for rather mathematical

1
(a, b) -> a + b

function notation.

At the end I really liked it. Problem that I was solving didn’t need using some advanced CoffeeScript constructs like classes but one that I used like multiple return values from the function really show how much you can get out of language if you have some basic tools implemented within.

Last several months I was developing mostly in the Rails framework and since Rails 3.1 is going to have CoffeeScript enabled by default I suppose I’ll give CoffeeScript a chance again in next months and write more about experiences developing more complex things again in future.

Code is on GitHub

You can find the source code for the implementation of the game on GitHub

Related links

 

Comments