Sunday, March 17, 2013

Book Review: JavaScript: The Good Parts

Web programming continues to grow in importance -- especially in our current world with its plethora of devices running on a variety of platforms.  Whether we like it or not, JavaScript has become the lingua franca of device programming.  Every web-connected device that we have -- desktop, laptop, tablet, phone, phablet, game console -- understands JavaScript.

This led me to read JavaScript: The Good Parts by Douglas Crockford (Amazon link).  This is a very dense book.  A lot of information is presented in a very small space.  Even though one could read through it very quickly (100 pages + appendices), you will probably find yourself spending a lot of time with each section to fully understand what's going on.

Last Decade vs. Today
My primary experience with JavaScript is from the early 00's when we were using it as a scripting language that added a bit of functionality to our otherwise static webpages.  I would populate combo boxes based on JavaScript arrays, enable/disable buttons, and do simple DOM manipulation (such as showing/hiding divs on a page).

Back then, debugging was all but non-existent.  Many of us remember a time where debugging consisted of running the page in a browser and looking for the yellow triangle in the corner.  That told us "something didn't work" -- and that was it.

Today, JavaScript is used for more than just simple DOM manipulation (although it is still used for that).  A huge number of libraries have entered popular use: jQuery, Knockout, Node, Backbone, and many, many more.  Plus, we also have JavaScript wrappers (such as CoffeeScript and TypeScript) that compile down to plain JavaScript but allow us to use different syntax and idioms.  JSON (JavaScript Object Notation) has grown as a light-weight data format that is preferred to XML is many situations.

In short, JavaScript is hard to avoid if you want to do relevant web programming.  But this leads to a problem.

The Primary Problem with JavaScript
The reason why most developers run into problems with JavaScript is that they think that they already know JavaScript.  On the surface, it looks fairly easy: it follows a C-style syntax and the idioms look familiar.  But there are a lot of misconceptions.

I've been fortunate enough to know Troy Miles (a mobile web guru in Southern California) and hear him speak a number of times about JavaScript and web programming.  (If you're interested in mobile and/or JavaScript, I would highly recommend that you check out his blog http://therockncoder.blogspot.com/ and YouTube channel http://www.youtube.com/rockncoder).

Because I had attended many of Troy's talks, I was prepared for what I found in JavaScript: The Good Parts.  One of these learnings is "Don't think that you already know JavaScript."  JavaScript is a language just like any other programming language.  We wouldn't expect to walk up to Haskell or Ruby and start programming competently without actually learning the language.  Why should we treat JavaScript any differently?

The Good Parts
The reason that Douglas Crockford references "The Good Parts" is because he's only covering a portion of JavaScript.  He starts by saying that JavaScript is full of bad stuff -- things that you should avoid because there's no way to use them without getting yourself into trouble.  But he argues that if we only stick the with the "good parts" of JavaScript, that we will find an expressive, elegant language that we can use to meet our needs.

One of the misconceptions about JavaScript is that it is related to Java.  But it's not.  Java is related to JavaScript the same way that car is related to carpet.  Because of this misconception, developers often expect to treat JavaScript like an object-oriented language.  That leads to much pain.

In reality, JavaScript is a functional language.  If we treat it as such, then we'll be able to take full advantage of what it has to offer.  Functional programming is a different way of thinking from object-oriented programming, so this means that OO developers must learn the differences.

Helpful Bits
I found quite a bit of useful information in this book.  One of these has to do with the grammar of the language.  The book provides railroad diagrams that show the syntax.  These diagrams show up throughout the book and are collected for reference in an appendix.  You can see these on the O'Reilly website: http://oreilly.com/javascript/excerpts/javascript-good-parts/syntax-diagrams.html.

Another part that I found useful was how objects are managed.  Object literals give us a way to create object values.  In addition, prototyping gives us a pseudo-inheritance model that let's us create and update values in a single location that can affect multiple objects.

The biggest thing that I took away from this book is the dynamic nature of the language.  Values can be added or updated at any time.  If you ask for a value that doesn't exist, then you simply get an "undefined" back -- there are no errors for asking for something that's not there.

The Bad Parts
Douglas Crockford talks about several bad parts (in fact, there are appendices dedicated to "The Bad Parts" and "The Awful Parts").  These include things like global variables, scope, semicolon insertion, "falsy" values, and several others.

What's good about this is that we get a warning about things to watch out for.  These are things that are part of the JavaScript language and perfectly valid.  However, if we use them in our code, we will probably find that they are more trouble that they are worth.  They often lead us to false assumptions or inconsistent behavior.  These warnings are invaluable.

In addition to the warnings, Crockford provides several work-arounds and tips.  For example, JavaScript allows you to declare variables anywhere in a scope (including after they are used).  Scope is based on the function, so it's best to put all of your variables at the top of a function.  This is especially important since we won't get an error if we reference a variable that hasn't be declared.

Wrap Up
JavaScript is a vital language in today's multi-device world.  We need to make sure that we take the time to actually learn the language.  It is tempting to think that we already know it (since the syntax looks so familiar).  But if we study the language, we find that JavaScript is a lot different from what we have assumed.

JavaScript: The Good Parts is a great read for anyone wanting to break their assumptions and start down the road toward really learning the language.  With this book as a start, we are on our way to using JavaScript as the effective and elegant language that it can be.

Happy Coding!

No comments:

Post a Comment