I actually bought the dead-tree version from Amazon, and was pleasantly surprised that the first thing you see when opening the book is an instruction on how to download the ebook from the publisher for free. Thanks Manning, this is treating your customers well. And it makes Amazon's “let the publisher know that you would like to read this on the Kindle”, which appears at least on amazon.de, pretty pathetic – they already let you read it on the Kindle, and they do so without any of the well-beloved Amazon DRM.
Anyway, I digress.
It took me about one and a half days to get through the book, which is a testament to the readable style in which the book is written. Resig manages to present code and talk about it in a way that makes it easy to follow, without resorting to overly simplified and contrived examples. In many programming books, you often have to stop after each prose sentence and think “Wait, which line of the listing is he talking about now?”. The Ninja book in contrast somehow manages to present itself almost like the author is talking about code that's projected onto a screen, making it possible to follow both the code and the explanation at the same time.
The writing style is friendly, not dry, without being facetious. Most of the time, at least. And there are ninjas and sumarais, and even jedis!
It's hard to find a succinct quote that exemplifies a writing style, but here is a try. This is how Resig describe the problem
You may have been to a family reunion where a garrulous uncle won't stop talking and insists on telling the same stories over and and over again. If no one else gets a chance to break in and get a word in edgewise, the conversation's not going to be very pleasant for anyone (except for Uncle Bruce, of course). Likewise, code that hogs all the processing time results in an outcome that's less than desirable; producing an unresponsive user interface is never good. […]
What we need to do is shut Uncle Bruce up at regular intervals so that other people can get a chance to join the conversation.
This prose is accompanied by code samples that actually show how this breaking up works. Although the author then goes into great detail explaining the code, he does not fall into the trap of beating the dead horse that is Uncle Bruce. It's a good story to illustrate the problem, but in the end we're talking about code, and metaphors only go so far. And the author realizes this.
Let's get from style to content now. Who is this book for? Let's put it this way:
If you're the kind of person who knows their way around in the jQuery source code very well, the second half likely won't have many surprises for you either. But either way, the second half is an interesting description of various browser and cross-browser issues that are definitely worth having heard of.
However it's more a collection of anecdotes than a list of recipes. Remember, this is not an introductory textbook. You won't be spoonfed solutions to your own everyday cross-browser issues; you will be told stories of solved issues, and all the book can offer is a look into the necessary mindset and a certain amount of inspiration on how to tackle such issues.
There are a few gripes I have with the book, which I will explain. To spoil the ending: Despite these gripes, I still encourage you to give it a read.
Signs of age
You really see that much of this book was written years ago. This doesn't invalidate the book, but it means you should read it with a few grains of salt. On his blog, John Resig gives some insight on where this delay came from.
To me, the most glaring example of these five years in the making is the following: In chapter 4, “Wielding functions”,
the book introduces
arguments.callee. Pretty much the first thing in that section is the following (very important) warning:
It is a fair point to introduce
callee for this reason. But to me it seems more like this section was written
in 2008 – when
arguments.callee was alive and kicking –, and then the warning was tucked on for the final
release of the book. Case in point: In chapter 14, “Manipulating the DOM”, the book describes a problem
in Internet Explorer with cloning of DOM elements, and gives an example from the jQuery code of how you can check for
the presence of this problem. And this code snippet makes use of
It's not vital to this code; the point of listing 14.8 is somewhere else – but I would expect a deprecated
language feature (which the book called out as such previously, multiple times) to not be used in example code. After all,
John Resig himself removed
this use of
arguments.callee from jQuery in early 2009.
You may find this to be nitpicking, but it unfortunately illustrates the long coming of this book. Much of the book is timeless, so it's not the biggest of issues. But when reading, keep in mind that not everything may be as up-to-date as you would expect from a book released in late 2012.
In a book that strives to make you a ninja, I would have expected some more discussion about edge cases. You don't have to handle or even mention all of them no matter how unlikely. But some pointers to possible situations that aren't as easy as the simplest case would be nice.
For example (this is in section 6.3, “Writing class-like code”), when using a regex on a function's string representation to check whether the function uses a certain feature, one should mention that this approach is making an assumption: The function is using this feature by its original name, and this name actually appears in the function source, and not e.g. in a string variable coming in from outside the function.
In another example, when introducing function overloading based on
arguments.length (in chapter 4), but later doing it
by checking arguments for
undefined (e.g. in chapter 12), it deserves a mention that these two are not equivalent.
It may not make a difference in this particular case (or, for that matter, in those many places where this is done in jQuery),
but from a general point of view, this is the kind of thing a ninja has to be aware of: Passing
undefined as a function
parameter and passing nothing at all are not the same thing.
On a final minor note: I am for the most part a proponent of the Crockfordian school of thought. Things like a comparison
and decrement-operator-as-expression are not a good idea for code readability in my view. Thus I am not a huge fan of some elements of Resig's
library of all times, a library that probably did more good for world-wide developer productivity than Stack Overflow and
caffeinated beverages combined. So who am I to argue.
Just don't expect this book to make you a ninja. It may give you a lot of weapons, powerful and dangerous at once. But to really become a master, you need to practice using them. Out there, fighting, in the real world.
previous post: An unexcited look at browser sniffing