Book review: “JavaScript: The Good Parts” by Crockford

I heart David Flanagan. I'm making my way through "The Ruby Programming Language" this summer. Its exhaustiveness really satisfies. But a decade ago, my programming Bible was Flanagan's "JavaScript: The Definitive Guide". As I transitioned from a career in content to a career in code, "the Rhino book" taught me everything I needed to know about object-oriented JavaScript, DOM scripting and the other building blocks of today's Ajax landscape. I've bought a hard copy of each of the book's five editions. It remained, until recently, the only JavaScript book I'd recommend.

JavaScript: The Good Parts by Douglas Crockford (cover image)

That all changed with the recent publication of "JavaScript: The Good Parts" by Yahoo's Douglas Crockford. Crockford probably needs no introduction. His incisive website and frequent blog posts have championed JavaScript's power and potential while calling out its drawbacks and frequent misuse. Now, with "JavaScript: The Good Parts," he has managed to provide a reference as useful for JavaScript pros as it is for novices. Part language primer, part apologia and part critique, Crockford's book draws from and extends many of his long-gestating themes about how to use JavaScript - and how not to use it.

The author's premise is so simple and intuitive that it sounds like rubbish until you suddenly realize that this is how all programming languages should be taught:

When I was a young journeyman programmer, I would learn about every feature of the languages I was using, and I would attempt to use all of those features when I wrote. ...

Eventually I figured out that some of those features were more trouble than they were worth. Some of them were poorly specified, and so were more likely to cause portability problems. Some resulted in code that was difficult to read or modify. Some induced me to write in a manner that was too tricky and error-prone. And some of those features were design errors. Sometimes language designers make mistakes.

Most programming languages contain good parts and bad parts. I discovered that I could be a better programmer by using only the good parts and avoiding the bad parts. After all, how can you build something good out of bad parts?

Crockford has no problem applying this thesis to JavaScript - an exercise he already nailed once in his seminal essay "JavaScript: The World's Most Misunderstood Programming Language." He describes JavaScript as "Lisp in C's clothing" - a powerful dynamic language that was rushed to market and stuck with a gimmicky name but achieved almost instant ubiquity thanks to the success of the web browser. Crockford doesn't condemn those features, such as non-classical inheritance, that set JavaScript apart from other popular languages. In fact, he bemoans the concessions JavaScript made to the Java crowd - puzzling syntactic choices that obscure the language's true nature.

Once he's established his guiding principles, Crockford spends several chapters cataloging JavaScript's "good parts." And that's pretty much the entire book, save for some excellent appendices. (More on those later.)

So what are those good parts? Crockford seeks to showcase the power and expressiveness of JavaScript's prototypal inheritance, first-class functions, closures, dynamic objects, and object/array literal syntax. To get there, he first describes the language's low-level features: types and operators, statements and syntax. Here, as always, he weighs every sentence carefully, making sure it can introduce new concepts to the uninitiated while also bolstering the understanding of folks who've previously encountered said concepts.

Throughout, he employs the plain, concise and often wry style for which he's known. If you read this book aloud, it would sound like the lectures of a particularly smart, snarky college professor. Even throwaway code samples sparkle with personality; "Lost" junkies, for instance, will enjoy playing "spot the Oceanic 815 reference."

Where necessary, Crockford offers simple solutions, in code, to overcome some of the trickier aspects of the language's inheritance and reuse patterns. His persuasive explanation of how to untangle prototypal inheritance - rather than building classical inheritance structures atop it - mark him as a programmer who's thought deeply about his craft. So much software development boils down to repeating the same steps one has carried out countless times before. Crockford leads by example, showing how to rethink received wisdom and rewire one's own algorithms. Such introspection about the process of programming should inspire all but the most haphazard of developers.

I love how this book simply ignores so many small, low-value features of the language. Crockford basically ignores the existence of Function.call in favor Function.apply. He never explains why, but it's obvious: When seeking to bind a function to a new object context, most programmers have to stop and think to remember which of these methods binds arguments as an array rather than as a list of parameters. Why learn both when simple convention can allow one to suffice?

I've been writing JavaScript for a decade, and I'm still impressed with the meat-and-potato chapters of this book. In just 170 pages, Crockford shows developers how to use JavaScript the right way. Is the aforementioned Rhino book still a valuable and exhaustive resource? Yes. But for someone looking to dive into JavaScript for the first time, "JavaScript: The Good Parts" offers advice and perspective to go along with language documentation. Reading it, I felt like I was encountering the language for the first time - and understanding it better than ever.

That said, Crockford's appendices may provide the most value to longtime JavaScript coders. There, he documents both the "awful parts" and the simply "bad parts" of the language in some detail. These sections work best when Crockford provides concrete evidence to support his assessments, as when he explains the danger blockless statements pose:

An if or while or do or for statement can take a block or a single statement. The single statement form is another attractive nuisance. It offers the advantage of saving two characters, a dubious advantage. It obscures the program's structure so that subsequent manipulators of the code can easily insert bugs. For example:

if (ok)
    t = true;

can become:

if (ok)
    t = true;
    advance(  );

which looks like:

if (ok) {
    t = true;
    advance(  );
}

but which actually means:

if (ok) {
    t = true;
}
advance(  );

Programs that appear to do one thing but actually do another are much harder to get right. A disciplined and consistent use of blocks makes it easier to get it right.

He's far less persuasive when he decides to forego examples. Proclamations such as the following come off like cant:

The continue statement jumps to the top of the loop. I have never seen a piece of code that was not improved by refactoring it to remove the continue statement.

Despite this occasional lack of substantiation, Crockford has succeeded in writing the definitive book on a language for which a definitive book has long been available. That's no small achievement. If you care about the quality of your JavaScript code, zip over to Amazon or your Safari Bookshelf ASAP.

Topics: ,

Comments: 6 so far

  1. Agree! This is a great book!

    Comment by Sidney, Tuesday, July 29, 2008 @ 1:15 pm

  2. Great review, I just bought this book recently and can agree with your assessment.

    Comment by Russell Jones, Tuesday, July 29, 2008 @ 2:27 pm

  3. [...] More… [...]

    Pingback by Ajaxian » Book review: “JavaScript: The Good Parts” by Crockford, Tuesday, July 29, 2008 @ 4:14 pm

  4. I totally agree. Crockford’s prose is lean, incisive and informative; he is the Hemingway of programmers.

    Comment by monsur, Tuesday, July 29, 2008 @ 8:47 pm

  5. [...] my recent review of “JavaScript: The Good Parts,” I compared this new Douglas Crockford treatise with David [...]

    Pingback by Pathfinder Development » More on Crockford’s and Flanagan’s approaches to JavaScript, Tuesday, August 5, 2008 @ 1:03 pm

  6. I agree, Crockford is an excellent author and I really, really like the way he thinks. People using the ’single statement’ for ifs is one of my pet peeves when adopting plugins and the like - it’s a pain to go around adding brackets because some nimrod thought he’d save half a second at the time. One, if you minify code where people did that, it causes errors.

    ANyway, this book is very recommended. Read it, believe it - and you’ll be a better programmer!

    Comment by Mono, Tuesday, January 6, 2009 @ 3:45 am

Leave a comment

Powered by WP Hashcash

About Pathfinder

Follow the Blog

    Get a monthly update on best practices for delivering successful software.

    Subscribe via email

      

    Subscribe via RSS      RSS icon

Topics

Search

WordPress

Comments about this site: info@pathf.com