Monday, February 13, 2012

Book Review: Node for Front End Developers by Garann Means (O’Reilly Media)

In my previous post I mentioned that I needed to come up to speed with CoffeeScript for my next project.  That same project will be using Node on the server side. Since I liked the CoffeeScript book I thought  I would  give Node for Front-End Developers a read.  Once again, O’Reilly has published a book that is a quick read but gives you enough information so that you can put the book down and start writing code.  I found this book gave me enough information to start building my first node based application. 

The Review

The book starts off by walking the reader through how to set up his/her node environment.  It walks you through installing node and its package management system, npm. Once you have your environment set up the you are introduced to node-for-front-end-devsthe REPL (read – evaluate – print – loop) with a few short code snippets.  The chapter ends with a discussion how to declare which modules your application depends on by using the package.json file. 

Chapter two walks the reader down the path of serving up resources be it a string of HTML or static resources like HTML, CSS, and/or JS files. The first example shows you how to write a server that serves up HTML ‘by hand’ which is then quickly followed by how much easier it is to serve static pages when you use npm modules like connect.  

After learning how to serve static files the author introduces shows you how to interact with the client by processing HTTP GET requests. The first example uses the module querystring to process GETs with parameters.  It reminded me of the early days of the web.   Thankfully after another example of processing parameters on the URL with querystring, the author shows you how to do the same thing in a more concise manner using the connect module.

The next topic was how to process HTTP POST requests.  We followed the same pattern here, learn how to ‘roll your own’ to process a POST which was then followed by an example of how to do it using the connect module.  I liked the approach the author takes throughout the book, show you how to do it yourself first and then introduce a module that can do the same thing in a less verbose manner.

After a brief overview of how to handle JSONP requests the discussion moved onto real-time communication using socket.io.  Having just finished a project that uses SignalR (which is a .NET open source project that has similar functionality) I found this subject very interesting.  The example made socket.io seem simply and straight forward.  In fact, after I finished reading this chapter, I started a spike to redo the project I just finished using socket.io just to see if it was any easier with socket.io.

Chapter four introduced the type of server side templates. It walks you through how to use mustache to layout templates for your application showing you how to use templates and sub templates to promote re-use on the UI side. Towards the end of the chapter the author starts to discuss best practices on how to group your code, separate out code that handles a certain task to promote code re-use and separation of concern.

The next topic of discussion was data access and application flow.  The first part of the chapter uses Redis to show the reader how to work in data access to your node applications.  I hadn’t worked with Redis before but after that part of the book I am now looking into incorporating it into a few projects currently underway.  After the Redis discussion was complete workflow was discussed using a pub/sub example using events.

Up to this point in the book I found the flow of each chapter easy to follow.  I appreciated the process of doing it by hand and then doing it again with a pre-existing module.  It’s a great way to show you how something works and exposing you to the node module ecosystem.  It seems like there’s a module for just about anything you may want to do.

The last chapter varied from the previous ones by jumping into a big chunk of code right away.  The code is an example of how to create an MVC application with node.  On the plus side, this chapter introduces the express module, which was inspired by ruby’s Sinatra web framework and is used by many other node modules.  Express has a tool that will create a directory structure for your app and has a view engine, Jade, to create HTML views.  Jade’s syntax takes a little bit to get use to but once you do it makes creating HTML views easier and much easier to read.  Overall the chapter wasn’t bad it was just a little code heavy when compared to the previous chapters.

My Thoughts

I came to this book with very little experience with Node.js, I had created a small app for my personal use but nothing huge.  Now that I have read this book I feel comfortable enough to use it in a few projects I have on the horizon.  I liked the methodology the author took for the first five chapters of doing it the hard way and then showing the reader the easier way to do with available modules.

On a side note, this is the second short book I’ve read by O’Reilly.  I hope they continue these type of short but sweet introductions to new(er) technologies.  It certainly helps us come up to speed quickly. 

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/ .

Saturday, February 4, 2012

Let Me Introduce Myself

Welcome to my new blog, My Programming Adventure.  I am .NET developer by day who has recently been drawn back into the open source world.  In my time away from the Linux/Unix world there has been a HUGE explosion in solid tooling and languages available for free.  Literally, anything you need you can find for free and more than likely the free ‘stuff’ will be better than anything you can pay for to do the same job.

One of the languages that I have really become fond of is Clojure.  While I was learning Clojure I found out about ClojureCLR, a version of Clojure that runs on the .NET stack .  Since I work in a .NET environment I wanted to use it in our production environment at work. While I was learning the ins and outs of ClojureCLR I found out that there was very little out there in the way of ‘How-To’ examples so I started a blog called My Clojure Adventure.  There I give small examples on how to connect to a database, interact with .NET libraries among other things. 

While I enjoy writing posts for My Clojure Adventure there are times where I have other topics I’d like to blog about that either have very little or nothing to do with Clojure.   That’s where My Programming Adventure comes in.  On this blog I will be writing about technologies, languages, books or whatever other programming related subject catches my eye.  An example of a few topics I’ll be writing about shortly are CoffeeScript, Node, and setting up Emacs for writing C# code.

In the next few days I will have a book review on The Little Book on CoffeeScript.   Now that I’ve introduced myself its time for me to go and work on my book review!  Come back and let me know what you think.