Book review: JavaScript: The Good Parts

This review covers JavaScript: The Good Parts, by Douglas Crockford.  Sebastopol, California: O’Reilly Media, Inc., 2008.

javascript-the-good-parts-cover-scan

Summary

This book undertakes to introduce the most important  things you need to know to use the good parts of JavaScript well, in 100 pages.  The author’s premise is that within the JavaScript language is a much cleaner subset, and that if you restrict yourself to that subset you’ll be able to write better programs.

There is also an appendix of Bad Parts, and another appendix of Awful Parts (Bad Parts that cannot be avoided).

The Good

  1. Terse!  Succinct! The book says so much in so few pages.  I’ve bought 800 or 1000 page books before that had almost nothing to say, but this book’s every word is there for a purpose.  Even the code snippets, while supporting some statement in the text, sometimes provide additional value by illustrating a language detail not mentioned in the text.  For example, a code example on page 22 shows var a = {}, b = {}, c = {} and a = b = c = {}, demonstrating a couple of ways JavaScript supports multiple assignments in one statement.
  2. Dry, non-sugarcoated  style. This helps keep the tone light when dealing with a programming language that admittedly has a lot of warts.  Examples:

    undefined and NaN are not constants.  They are global variables, and you can change their values.  That should not be possible, and yet it is.  Don’t do it. (p. 107)

    And:

    Unfortunately, for in makes no guarantee about the order of the properties, and most array applications expect the elements to be produced in numerical order. Also, there is still the problem with unexpected properties being dredged up from the prototype chain. (p. 60)

  3. Points out surprises in the language. This is one of the strongest points of the book.  Two quick examples:
    • The typeof operator returns object sometimes when you wouldn’t expect it (p.16);
    • Dealing with array objects:

      Because an array is really an object, we can add methods directly to an individual array…Since the string ‘total’ is not an integer, adding a total property to an array does not change its length.  Arrays are most useful when the property names are integers, but they are still objects, and objects can accept any string as a property name.  (p.62)

  4. Introduces you to the benefits of a loosely-typed language. Crockford says:

    JavaScript is a loosely typed language, so JavaScript compilers are unable to detect type errors.  This can be alarming to people who are coming to JavaScript from strongly typed languages.  But it turns out that strong typing does not eliminate the need for careful testing.  And I have found in my work that the sorts of errors that strong type checking finds are not the errors I worry about.  On the other hand, I find loose typing to be liberating.  I don’t need to form complex class hierarchies.  And I never have to cast or wrestle with the type system to get the behavior that I want.  (p. 3)

    It was really interesting to read this book soon after finishing Effective Java, since that book  had such a focus on using strong type checking to prevent errors.

  5. Helpful railroad diagrams (p. 6).  I found these really helpful in making a grammer intuitively understandable.  You can take your finger and trace around to see what’s allowed and what’s not (for examples, see the JSON site).
  6. Explains closures. (pp. 37-39)

The Bad

  1. Organization. The author says at the beginning of the book:

    There is a lot of material packed into it.  Don’t be discouraged if it takes multiple readings to get it.  Your efforts will be rewarded.  (p. xi.)

    I understand the desire for terseness and succinctness — these are strengths of the book.  But there are organizational elements that make the material more difficult to absorb than it needs to be.  A couple stand out:

    • Forward references. There are points in the book where a term or concept is stated in the text without explanation and you haven’t been given the tools to understand what you’re seeing yet — by later in the book you have what you would need to go back and understand, but at the moment, you about have to shrug and go on.  Examples:
      • The term “truthy” is introduced on page 10, but not explained until page 12
      • The term “refinement” is introduced on p. 14, but not explained until page 17
      • “The / operator can produce a noninteger result” (p.16)…  Huh?  (I now think this may be referring to NaN)
      • The Object.create function described at the bottom of p.22 was a mystery to me the first time through
    • Grammar front and center. Chapter 2 is full of railroad diagrams of the language grammar, with accompanying explanation.  The diagrams are helpful in understanding the syntax — but as I proceeded through the chapter I wished I had had more concepts first.  Then I would be able to dig into the grammar with some understanding.
  2. Lack of cross-references within the text. Even though it’s only 100 pages, it would still be quite helpful to have a page reference instead of just “There will be more about X in the next chapter” (three examples of this on page 17) or “in Chapter 6” or “at the end of this chapter” (p 31).
  3. Index is mediocre. The index is a Bad Part of this book.  It is missing several entries that would have been helpful to me.  Examples:
    • Forward references such as truthy, falsy and refinement are missing from the index
    • The topic of “global abatement” is not specifically in the index so you have to sift through three entries about global X looking for the page that deals with global abatement.  Similar for “semicolon insertion” — the index entry is for “semicolons” and you have to check out the two pages listed to find the part about automatic semicolon insertion.

    The index falls short of what I expect for an O’Reilly book.

  4. Doesn’t understand what positive and negative regex lookahead are for (p. 75).  A minor issue that I’m tuned into since reading Mastering Regular Expressions, 3rd ed. Crockford says lookahead is not a good part of the language… but would he maintain his opinion if he had seen my wonderful examples of how they can help?

Not Sure If Good or Bad

  1. Defines a new language? The author suggests augmenting the language in several ways to neutralize deficiencies in the language as it exists:
    Suggestion Strength Suggestion Page
    we can… Add method method to Function.prototype 32-33
    we can fix it Add integer method to Number.prototype 33
    That is an easy oversight to fix Add trim method to String.prototype 33
    we can fix that Add curry method to Function.prototype 43-44
    we can work around that Add is_array function 61
    can easily correct Add dim method to Array.prototype 63
    we can correct that too Add matrix method to Array.prototype 63
    You may want to Add isNumber method 105

    Maybe this is a good idea.  And it’s so little code — about 45 lines total!

  2. Explains how to create tamper-proof objects (pp. 52-55).  After getting a glimpse of some of the flexibility and benefits of a loosely-typed language, I’m not sure what to make of tamper-proof objects — they seem to work against all that flexibility, and Crockford does not offer any analysis of how this fits in with a philosophy of normally not locking down things.  Is this something that you’d normally reserve for special situations?  Use all the time?

Overall Impressions

Reading the book was like tagging along with an expert.  Sometimes he’s teaching you, but other times you don’t understand what he’s doing.  Sometimes you just have to be patient, and watch and learn.

I give the book an A in Content and a C in Organization/Indexing/Cross-Referencing.

The faults I have mentioned are not showstoppers.  The main part of the book is only 100 pages, and you can work around the forward reference problem by skimming through it a second time.  Crockford’s book helped me quickly get up to speed on the language and avoid many issues I would likely have stumbled against as a JavaScript beginner, and that was my hope for the book.  I would recommend the book to anyone hoping to do the same.

Advertisements
  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s