JavaScript: The Good Parts «
»


Code: , ,
7 comments

In JavaScript, there is a beautiful, elegant, highly expressive language that is buried under a steaming pile of good intentions and blunders. The best nature of JavaScript is so effectively hidden that for many years the prevailing opinion of JS was that it was an unsightly, incompetent toy.
p2

I disliked JavaScript until I read some of Douglas Crockford’s articles arguing that it is the world’s most misunderstood programming language. I pieced together a vision of a nice language burdened by misfeatures and poor implementations. In JavaScript: The Good Parts, Crockford fully realizes that vision in a dense, enjoyable book.

If you find yourself writing in JavaScript and not enjoying it, you need to stop and read this book. (Or even if you’ve just wondered why Steve Yegge obliquely goes on about it.) The book feels a bit like Kernighan and Ritchie in that it tersely defines the language and lets the reader work through the implications. I especially appreciate that Crockford assumes a competent reader and rewards patient and thoughtful reading rather than bulking up on exposition and screenshots. It’s very uncommon in modern writing.

The book opens by defining the syntax of JavaScript with a series of railroad diagrams that are far more enjoyable and readable than BNF:

JavaScript whitespace

Next it defines objects and functions and derives useful patterns for working in JavaScript. The Module pattern he gives on p41 “…can eliminate the use of global variables. It promotes information hiding and other good design practices.” It’s a nice piece of code to deal with a personal pet peeve.

After the sections on recursion, scope, closures, currying, and memoization I was surprised not to see a section on the Y combinator, which allows creating recursive anonymous functions. There’s a blog post with a nice derivation, but it seems oddly absent from the book.

More glaringly missing, though, is code to copy JavaScript objects and arrays. On p22 he writes “Objects are passed around by reference. They are never copied.” and a code comment on p64 explains how some code avoids a related bug, but Crockford never talks about how to create shallow or deep copies of objects and arrays. It is flatly bizarre to see the topic raised and warned about without any giving a solution to an interesting and common JavaScript problem. (I personally use jQuery.extend.)

After dealing with objects and functions, Crockford combines them to talk about JavaScript’s unusual prototypical inheritance, then moves on to a nice explanation of working with arrays and regexps. After talking about the standard library and giving style notes, there are a couple nice appendicies explaining what he left out and why and presenting JSLint for automated detection of common pitfalls.

There are a few odd notes like an imprecation against continue (indeed, none of his code tries to “fail fast”) that feels like it must be based on old scars inflicted by manual memory management in C/C++. The only really puzzling oddity was in the chapter surveying the standard types. He gives JavaScript reimplementations of Array’s pop, push, shift, splice, and unshift methods (but not concat, join, reverse, slice, or sort) for no discernable reason other than a mention on p98 that he’d wanted to write a self-parser in JavaScript for a chapter he contributed to another book.

I don’t think too much about these few oddities, though, it takes a certain amount of chutzpah to distill a beautiful language from a clunky, heavily-abused language and it’s harmless to see that manifest a bit of quirky style. Mostly I spent my time reading, pondering, and tinkering with the example code. There are some great examples for creating objects and working with the prototypical inheritance, and the array sorting code (p80) is an especially inspiring bit of useful code.

The only serious negative of the book is the incessant and distracting typos. There are countless small code layout issues, bad line breaks in comments (p39), weird indentation (p40), added line breaks in code (p41), occasional camelCase. There are also a few typos in the code segments that will cause weird exceptions or, worse, silent failures. If the book didn’t have so damn much useful content I’d suggest waiting for a third printing to correct these errors (I read the Sept 2008 printing, look for a [9/08] or later in the lower-right corner of the copyright page). The poor copyediting is compounded by O’Reilly’s awful errata page; you can’t filter by printing and it includes many duplicates, making for a frustrating slog through clutter to track down why a particular sample doesn’t run.

Front cover of JavaScript: The Good Parts
In spite of the flaws, I strongly recommend JavaScript: The Good Parts to anyone who regularly works with JavaScript. If it seems that I’m not making a good case for it, it’s because the power of the book is the enrichment gained from working through its examples and the negatives are easy to diagnose and explain. Read it. You’ll gain a new perspective of the language and refresh your toolbox with new techniques. Most importantly, you’ll stop being annoyed at writing JavaScript.


Comments

  1. Hmm… I might have to track this down because I have absolutely LOATHED JavaScript from the moment I started working with it. I still try everything in my power to avoid working with it! Especially having to tinker with some 3rd party’s badly written piece of slop code!

    Maybe after reading this I’ll be inspired to come up with some neat little gizmos for websites without going into convulsive fits first. ;)

  2. Thanks for the link to your Y Combinator — can you comment on why you didn’t address copying objects? I figured Y was just a little too esoteric to make the cut (as fun as it is) but copying actually felt like something was missing.

    And thanks for your wonderful writing on JavaScript, I’ve really appreciated the language and improved my code because of your work.

  3. Great job here Harkins, I too have always felt terribly about coding with Java. Most would agree as Douglas puts it, “… the prevailing opinion of JS was that it was an unsightly, incompetent toy.”

    I will definitely be picking this book up with your suggestions. Less pain from Javascript equals greater productivity.

  4. This seems like something I should pick up; I’m only a casual JavaScript programmer, hacking on Facebook grinder games/apps using Greasemonkey and JS, and I think this might help me write better JS and enjoy it more when I do.

  5. The issue of copying and object (i.e. making a new object that inherits from an old one) is addressed on p. 22 of the chapter on objects. Crockford adds the ‘create’ method to Object. I find it very elegant and useful.

Leave a Reply

Your email address will not be published.