Monday, February 6, 2012

Book Review: The Little Book on CoffeeScript by Alex MacCaw (O’Reilly Media)

When I started reading The Little Book on CoffeeScript I had zero experience with CoffeeScript. I was looking for a book to quickly bring me up to speed since my next project will rely heavily on CoffeeScript.  At 60 pages I thought the book the_little_coffeescript_bookwould give me enough information to start writing CoffeeScript code.  After reading the book I can say that I made the right choice.  This book has given me enough knowledge to get started writing CoffeeScript code. 

The Review

The book starts off with a chapter on CoffeeScript syntax in a nice, concise manner.  It covers functions, loops, arrays and CoffeeScript specific operators and aliases.  There were plenty of examples with just enough text to explain what was going on in the code. 

The next chapter discussed classes, specifically how to declare and use them.  While discussing class properties the author pointed out a shortcut as to how to set a class property to a value that will save you typing.  Lets say you have a class called Animal with a Name property that you want to set by passing a value to the class’s constructor.  Here’s the ‘long hand’ code for that:

class Animal 
constructor: (name) ->
@name = name

Not a lot of typing but the author shows you how you can do it in fewer lines. The ‘short hand’ way is here:

class Animal 
constructor: (@name) –>

It doesn’t seem like much but over the long haul I appreciate the short hand method.  There were a few other places in the book that the author shared shortcuts like this with the reader. 

Following the classes chapter, the next type for discussion was CoffeeScript idioms.  Here the author points out that using the English words for things like and instead of && and or instead of || were the preferred way to do logical ‘ands’ and ‘ors’.   Most of the chapter is dedicated to showing the reader how to accomplish things like how to perform ‘each’, ‘select’, ‘map’ and other functionality in the language.  The text had a nice way to show a person who is new to CoffeeScript how to do the ‘typical’ programming tasks.

The next chapter give a quick overview on how you can use CoffeeScript in conjunction with Node and node packages to create an application.  Overall, I found this chapter to be a nice introduction to creating an application but there were a few problems I ran into while following along.  The issues I found were that when I went to run the app I was missing five modules:  underscore, async, connect, qs and mime.  Thankfully the error messages were straight forward and fixing the problem was as easy as running ‘npm install <module name here>’ for each module.  The last bit of the chapter walked the reader through how to deploy our application to Heroku.  It was much easier than I thought it would be.

After our hello world’ish app was created the author switched over to discuss how CoffeeScript can fix some of the JavaScript warts and how it can’t ‘fix’ some of the other JavaScript warts. The chapter is broken down into unfixed and fixed sections.  The unfixed section the shows you how and why the JavaScript typeof functionality is broken and then follows the explanation up with how you can ‘fix’ typeof by writing your own function to do it.  As an example of how CoffeeScript can ‘fix’ a JavaScript wart, the author informs the reader that CoffeeScript uses the strong equality check for all equality checks.

The book is summarized in a chapter written by Jeremy Ashkenas, the creator of CoffeeScript. In it he discuss the philosophy behind CoffeeScript, which the quote below sums up.

“express core JavaScript concepts in as simple and minimal a syntax as we can find for them.” – Jeremy Ashkenas

My Thoughts

When I started this book I had zero experience with CoffeeScript and I was hoping that after reading it I would feel comfortable enough to write my own code.  I would say that this book has shown me enough CoffeeScript that I feel comfortable enough to start writing code for my next project.  

You can find out more about the book by checking it out The Little Book on CoffeeScript over at http://oreilly.com/ .

No comments:

Post a Comment