Book Review: Advanced JavaScript by TJ Holowaychuck

March 29th, 2010

I’m not sure if it’s been done before, but I may very well be the first GitHub stalker (hold your applause, please). Who is my target you ask? Vision Media, aka TJ Holowaychuk on GitHub, author of ExpressJS, a Sinatra-inspired Web Framework written for NodeJS.

After cloning and examining a lot of his repos (Use the Source!), I found out in an endnote that TJ had published a book entitled Advanced JavaScript from DevMag. It’s a short book, but one that opened my eyes in a surprising number of ways.

Overview

The books Table of Contents are as follows:

  • Ch 1: Closures, Scope and Context
  • Ch 2: Prototypal Inheritance
  • Ch 3: Advanced Meta-programming techniques
  • Ch 4: Behavior Driven Development
  • Ch 5: Creating a jQuery Clone
  • Ch 6: Tools of the trade

The meat of this review will focus on Chapters 3-5, which made me rethink the way I develop JavaScript. No offense to the author(s), but the contents in Chapters 1 and 2 have been better explained in other texts, notably JavaScript: The Good Parts by Douglas Crockford, and Chapter 6 should have probably just been an Appendix.

But back to the good stuff: Chapter 3 goes through the creation of a JavaScript clone of some of the most basic functionality found in Sinatra, namely creating routes and executing them when matched. While the concept of DRY is often promoted as The Thing To Do, this chapter clearly explained some of the ways to make your programs smarter so that you can in fact avoid repetition! Very nice.

The revolution will not be televised, it will be tracked on GitHub

The revolution started for me in Chapter 4. I’d read about some agile programming techniques, but I’d never heard of BDD, or Behavior Driven Development. Chapter 4 talks about how to implement BDD with JavaScript using JSpec, an awesome unit testing framework, and one that totally changed how I think about my projects.

An aside on BDD

Essentially, BDD is an “outside-in” methodology, meaning that you start by identifying outcomes with your clients (internal or external), and then map out the features that will achieve such outcomes. This often happens in a story format.

Overall, it’s fantastic because it allows your clients to stay on the same page as you, the developer. You define concrete outcomes with your clients, and which steps needed to be taken for each feature to work. This means that both you and your clients have a solid understand of what needs to be done, and what “done” truly means. To read more about BDD specifically, check out a great explanation of BDD from the inventor, Dan North.

Using JSpec to implement BDD

With regard to chapter 4, JSpec helps us implement BDD from the developers perspective. Once the outcomes and features have been hashed out into specific steps, or “units,” then the developer can begin doing his/her job! Now, BDD is an extension of test driven development, and therefore implies that we should begin not by writing our implementation, but by writing Unit Tests to ensure that the functionality defined within our story is properly executed! By working this way, developers get the myriad of benefits that come from proper unit testing, such as avoiding regressions (breaking your code).

However, it had never really clicked in my mind how to really do convenient / good unit testing with JavaScript, and that’s what Chapters 4 and 5 go into in detail. There are a ton of libraries out there, but none of them encourage Unit Testing as part of the workflow the way JSpec does, because most of them are just simple unit testing frameworks written in JavaScript. But JSpec is so much more!

Jspec doesn’t just include a unit testing JavaScript file, you download a whole Ruby gem and get amazing command-line utilities for creating new projects with testing and documentation all set up, and then very convenient methods for running the unit tests in all your browsers! For example, if you execute the following command:

jspec init mySpecTest

You’ll get a new folder called mySpecTest with a directory full of goodies, such as a convenient folder structure for creating consistent projects, and all you need to run the JSpec unit tests. With the specs from your client in hand, you could then quickly begin writing your unit tests to achieve the desired functionality! Then all you have to do is write your JavaScript to pass all of your tests, and you have a very discrete piece of functionality “done!”

Wrap Up

The book does a great job of going through how to use JSpec in a number of ways, and how to build a cool, limited jQuery clone using the principles of BDD. Overall, as I said, this has literally changed the way I think about writing JavaScript in the future. This will certainly be the approach I take (I had never really written rigorous tests for my JS before), but one that I can immediately see will provide a ton of benefits to the quality of my code.

If you’re like me, and you thought “Unit testing is pretty cool, but I really have no idea how to properly implement it,” you will walk away from this book knowing exactly how to use unit tests, and you’ll also probably be bursting with ideas about how you can improve your code and your development methodologies.

“Advanced JavaScript” rules, and for $4 it is well worth the price. Go buy it.

Addendum/Nitpicks

Now, while the book totally kicks ass, I did have some minor problems with it. I really think the book needs a good editor. There are some frankly just kind of sloppy grammatical and spelling errors throughout the book, and the writing in general could use a good solid critique to make it a bit better. Also, there’s a lot of source code presented in the book, but the book didn’t come with all of the source in separate files. It had a few files, but it was missing some of the JSpec files that I would have found most interesting.