A tale of two Javascript books

23. March 2012

 

 

Have you ever read a technical book and thought to yourself “Man, the author of this book must be a gigantic jerk!”?  I can honestly say, I never have until now!

 

 

This review of sorts covers two different Javascript books I’ve read recently; two very different books with two very different goals.  As I have been working with Node and more recently Appcelerator, I decided I needed to polish up my Javascript skills, as things are much different these days than the DOM manipulation I traditionally used Javascript for.  I started out by searching for “Javascript closure” on Safari Books Online and it brought me to Javascript: The Good Parts, it was short and I was suffering insomnia so I read it that evening.  The book left me with so many questions that I ended up reading/contrasting it against Javascript: The definitive guide.  Aside from both books being about Javascript and both having colons in their titles, these two books couldn’t be further apart!

 

 

 

Let’s start with the Javascript: The Good Parts review.

 

 

First off, it’s worth mentioning the author, Douglas Crockford.  If you’ve never heard of him, he is the guy originally responsible for drafting the JSON standard and Yahoo’s head JavascriptJavascriptTheGoodParts guy, responsible for projects like the yUI library.  Simply put, this man knows his stuff when it comes to the topic.  Sadly, he knows it too and doesn’t let you forget it.  Many times in the book he pushes his opinion as fact, and often on very odd things ( like never use ++ operators ).  He often makes sweeping statements that really don’t work in a black and white world.  That said, if you can get over the personality ( or you aren’t sensitive to such things ), this is a wonderful little book.

 

 

I do mean little too, as this book weighs in at a mere 176 pages.  Amazingly enough there is a ton packed in to those 176 pages and even more amazingly, a great deal of it could be pruned and would make this a superior book ( such as the regular expressions chapter ).  A good example of what could be pruned out completely are the absolutely bizarre charts this book is littered with.  Here is one for example:

 

 

whitespace

 

 

That chart was meant to explain Javascripts whitespace rules… did it help you, even slightly?  You could have more concisely and effectively conveyed all of that information in a sentence or two.  I found that over and over, the author being overly clever or complicated leaves the reader baffled on what should be a pretty simple concept.  He does the exact same thing with code examples too, consider this code sample regarding inheritance:

 

var Cat = function (name) { this.name = name; this.saying = 'meow'; }; // Replace Cat.prototype with a new instance of Mammal Cat.prototype = new Mammal( ); // Augment the new prototype with // purr and get_name methods. Cat.prototype.purr = function (n) { var i, s = ''; for (i = 0; i < n; i += 1) { if (s) { s += '-'; } s += 'r'; } return s; }; Cat.prototype.get_name = function ( ) { return this.says( ) + ' ' + this.name + ' ' + this.says( ); }; var myCat = new Cat('Henrietta'); var says = myCat.says( ); // 'meow' var purr = myCat.purr(5); // 'r-r-r-r-r' var name = myCat.get_name( ); // 'meow Henrietta meow'

 

 

Notice how half of it is taken up by a purr() method which is overly convoluted and has nothing to do with the lesson at hand.  It simply causes the reader to have to dedicate a few more cycles to deciphering an example ( which is completely irrelevant to the topic at hand ), while allowing the author to feel just a little bit more clever. 

 

These kinds of things happen over and over and it makes reading the book more difficult than it should be, for no good reason.  The other major problem with trying to decipher the code samples in this book are the errors, my god the errors!  See every technical book is going to have a certain number of errors, but for a 176 page book, this one has far more then it’s share.  Just take a look at the errata.  Nothing makes learning a new concept more difficult than following code that doesn’t work!  When you are new to a subject, you assume the code you are working from is correct and it’s your newbishness that is the source of the problem.  That often simply isn’t true with this book, when you read it, if you encounter problems be sure to check the errata before you bash your head off the wall with frustration.

 

 

Next up is determining who this book’s audience is, which is a problem the author obviously struggled with.  Consider the following two paragraphs taken from the preface:

 

This is a book about the JavaScript programming language. It is intended for            programmers who, by happenstance or curiosity, are venturing into JavaScript for the   first time. It is also intended for programmers who have been working with JavaScript at a novice level and are now ready for a more sophisticated relationship with the language. JavaScript is a surprisingly powerful language. Its unconventionality presents some challenges, but being a small language, it is easily mastered.

 

Followed a paragraph later by:

 

This is not a book for beginners. Someday I hope to write a JavaScript: The First Parts book, but this is not that book. This is not a book about Ajax or web programming. The focus is exclusively on JavaScript, which is just one of the languages the web developer must master.

 

Let me put this in the simplest terms possible This is not a book for beginners!  If you are “venturing into Javascript for the first time”, well frankly you are absolutely screwed if this is the book you chose!

 

 

So I’ve dumped on this book pretty good, it’s short, the author comes across arrogant and I seem to hate at least half of it, so why the hell am I bothering to mention it to you?  That’s because the remaining half of this book is downright excellent.  It basically is the Javascript equivalent of Effective C++, which is one of the best technical books ever written.

 

 

Javascript as a language is an absolute minefield waiting to explode killing ( or at least mentally scaring ) the developer.  It lacks the data hiding facilities that basically every other language in existence has.  More so, it has extremely dangerous but seemingly benign constructs like new and == that are just waiting there to attack you.  Finally, the scoping rules are almost as byzantine as you’d expect from C++! It is far too easy to make a variable global, or mute out an existing variable leading to undesired behavior ( bugs ).  This book helps you to navigate that minefield, probably better than any other book in existence.

 

 

More to the point, most people simply use Javascript wrong ( now there I go sounding arrogant! Winking smile ).  But simple fact is, years of browser based DOM manipulation code written to mimic traditional OO code has lead to many people forming extremely bad habits, myself included.  This book does a very good job of showing you the intricacies of the language, as well as effective work arounds for perceived faults.  Some of these “faults” I disagree with, but I certainly did learn from the exercises anyways.

 

 

So in the end, do I recommend this book?  That’s a tricky one.  I really don’t like the way it is written, I am not keen on the way he teaches ( the choice of illustrations and code examples ), the excessive errors certainly don’t help.  That said, I learned a ton reading this book, in a very short period of time.  Besides, it’s less than 20 bucks!  So in the end, yes I do recommend you read it, much like Effective C++, it will make you a better Javascript programmer.  But for learning the language or as a first book, certainly not.  Fortunately in the regard, we have another book!

 

 

 

Javascript: The Definitive Guide review.

 

 

If Javascript has a bible ( like K&R C for the C Programming Language ), it’s this book.  The Definitive Guide is a much more traditional book, much longer at 1032 pages and with a much more traditional price tag.  This part of the review is going to be much shorter, as essentiallyJavascriptTheDefinitiveGuide there is a lot less to say.  The authors writing style in much more approachable than Javascript:The Good Parts and the code examples are far easier to digest.

 

 

One up or downside to this book, depending on your needs, is the amount of browser/DOM related content.  While Good parts effectively had no content pertaining to using Javascript in the browser ( an area extremely well documented in books and online ), the same is not true for this book.  Fully half of this book is on “client side” Javascript programming.  Then again, even at 50%, this is still a 1000+ page book, so you still got another 500 pages of non-client side Javascript.  Just remember, if you are using Javascript outside of the browser, if it starts with document. it probably isn’t supported.

 

 

The book is basically broken down into 4 sections.  The first is Core Javascript, which covers the language itself, regardless to how it is used.  This section is where you cover concepts like reserved words, objects, arrays etc.  The second is Client Side Javascript, which basically covers using Javascript in the browser, covering topics such as the DOM, jQuery, HTML5, etc.  The final two sections are reference sections for both the core and client side libraries.  These sections are strictly for reference.  Truth is though, with the sad state of the internet on this subject, a handy book reference is quite a useful thing.

 

 

For learning the language and your goto reference book, it’s really hard to beat Javascript: The Definitive Guide.  Taken in tandem, these two books are all you would need to succeed at Javascript development.  The Definitive Guide to get you started, and as a reference as you go along.  Then read Best Parts, to make you a better developer… if you are willing to put up with some annoying attitude that is!

Programming ,







blog comments powered by Disqus