found drama

get oblique

review: JavaScript Enlightenment

by Rob Friesel

JavaScript Enlightenmenttl;dr: It’s a 150 page essay on auto-boxing, full of dangerous code examples and anti-patterns and soft warnings to “not do what I just did”, and some of the wording is not-wrong-but-not-quite-right, but hey there are some decent parts, I guess.

Cody Lindley’s JavaScript Enlightenment (O’Reilly, 2012) has an awesome title, and I wanted very much to like it–but I think I’m going to have a hard time recommending it to anyone. Maybe.

Lindley states that the target audience for his book is (effectively) JavaScript dilettantes. In his words:

…it is my hope that the material in this book will transform you from a JavaScript library user into a JavaScript developer.

I interpretted that statement to mean that he is looking at the designers and the junior developers and the programmers coming at JavaScript from other languages–all the ones out there that have been using jQuery and/or ExtJS and/or Dojo with effectiveness (or at least competence) and now they’re interested in digging into the guts of their favorite library, and perhaps even getting their heads around that thorny beast called JavaScript.

This is an admirable goal, and one that I fully support. But when I got to the end, I scratched my head and wondered aloud: “Did I just finish a 150 page essay on auto-(un)boxing?”

Understand objects and you will understand JavaScript.

This is (more/less) Lindley’s thesis statement for the rest of the book. From there, we have a lot of discussion and exposition about JavaScript’s types, including quite a bit of examination into how the various primitives are coerced (behind the scenes) into their wrappers and then back down again. Lindley is right, this is important stuff to know if you want to have a deep understanding of JavaScript–but if you’re at the point with JS libraries that you’re taking a wrench to the plumbing, then you’ve probably already gotten this information on your own already. (Or else have learned how to look it up in MDN or MSDN or the WebPlatform Docs.) But my skepticism about this might be totally unfounded–perhaps there is a big audience for this kind of book. 1

But… My biggest issue comes from what I perceive to be a… almost reckless or dangerous approach to presenting the material. What I mean by this is that while many of the code examples are technically correct, they are not idiomatic — in other words, you wouldn’t see code like that “in the wild.” 2 Sure, you can use the String constructor to create strings–but this almost never offers you any advantage over simply creating a string with literals. 3 Granted, to Lindley’s credit, he states in numerous places in the text that “no one really does this” or “you should just use the primitive/literal form of this” — but after also asserting (right at the very beginning) that the reader should pay more attention to the code… Well, I’m fearful that those same readers will pick up hard-to-break bad habits.

And I think that’s why my reaction was to think: “This is a dangerous book.” 4 As an author, you’re in a powerful position as a teacher and role model–so if you’re demonstrating “weird” code or error-prone techniques or calling things by the wrong name, 5 then you might wind up influencing a bunch of developers into those bad habits–even if you didn’t mean to, even if you warned them against those exact methods.

Perhaps I’m over-reacting a little bit, but believe strongly that these things matter.

“Comma-first” or abusing ASI is one thing… But using Number where you should be using parseInt is quite another.

I had other criticisms as well (e.g., talking about “associative arrays” is always a sketchy proposition when your target audience is JS novices; e.g., making no mention of strict mode whatsoever 6) but they’re really just pale variations on “the big one” above.

So where do I land on this book? For all my critique of it, I am not sure that I have a recommendation that fills quite the same niche. Addy Osmani’s Learning JavaScript Design Patterns came to mind first–but then I thought about how that might actually be a follow-up read to JavaScript Enlightment. If Osmani’s Patterns book is out, then so is Stefanov’s. And Lindley more/less explicitly calls out Crockford’s The Good Parts as the kind of book that would scare off his target audience. Nicholas Zakas’ new book, Principles of Object-Oriented Programming in JavaScript, is very similar in what it covers, and has more/less the same thesis… According to my survey, that’s the closest contender. (And each offers something that the other does not so…)

I guess my final verdict is that I’d recommend Zakas’ Principles of Object-Oriented Programming in JavaScript, and if it scares you too much then back-track to JavaScript Enlightenmentbut with the caution that you need to be disciplined in how you approach it, because we can’t have you littering your code with new Number(1).

Disclosure: I received an electronic copy of this book from the publisher in exchange for writing this review.

  1. At the time that I wrote this review, most of the ratings on Goodreads are 5 stars.[]
  2. Or shouldn’t, anyway.[]
  3. Most of the time it will hurt you, or confuse the typeof operator, or lead to some other language quirk that will catch a JavaScript novice off-guard.[]
  4. I had a similar reaction when I reviewed Eric Sarrion’s jQuery UI.[]
  5. For example: not calling “auto-boxing” by name. For example: calling it a “self-invoking anonymous function” instead of an “immediately-invoked function expression”.[]
  6. The omission of strict mode seems like a major oversight to me. I realize that Lindley’s discussion was germane to ES3 and not ES5–but 'use strict' is out there in the wild, and it can dramatically alter some of the behaviors described in this book. If the target audience really is people that are diving into the plumbing of their essential libraries, then some of them are going to see this and not realize the significant implications it might have w/r/t/ some bug they’re trying to trace.[]

About Rob Friesel

Software engineer by day. Science fiction writer by night. Weekend homebrewer, beer educator at Black Flannel, and Certified Cicerone. Author of The PhantomJS Cookbook and a short story in Please Do Not Remove. View all posts by Rob Friesel →

4 Responses to review: JavaScript Enlightenment

Leave a Reply

Your email address will not be published. Required fields are marked *

*

*