Relevance Reviews #2: Ajax Hacks

This week, I am off to Vancouver to teach Ruby on Rails Today's in-plane reading is Bruce Perry's Ajax Hacks, published by O'Reilly.

Quick summary:Chapters 4 and 9 are excellent and may justify buying. Skip the rest.

The "hacks" format breaks the content of a technical book into (in this case 80) individual entries. As I mentioned in an earlier review, I like this format because like most developers I learn best by example.

Unfortunately, the "hacks" metaphor breaks down in this book. Chapters 1-3 are not really hacks at all; they are mostly a raw-JavaScript Ajax tutorial, disguised as hacks to fit their form of the book. This is unfortunate because inexperienced readers may choose to use "intermediate" steps of the tutorial as "final" hacks in their applications. We have a bunch of great Ajax libraries; very few people should have to write raw-JavaScript Ajax. That said, it is definitely still worth learning raw JavaScript Ajax. You should always understand one level of abstraction underneath your primary work. Once you've learned, pick a library for your production work.

Chapter 4 ("Power Hacks for Web Developers") is much better -- a grab bag of techniques that will have something new for most readers, even if they have Ajax experience. This chapter also benefits from multiple authors, as you get to see different stylistic choices being made. Also, this chapter encourages more thinking about "what can I do?" instead of just "how can I do it?" There are several examples of mashups that combine existing web services under a single Ajaxy front.

Chapters 5-8 cover a variety of Ajax libraries: DWR, Prototype+Rico, Ruby on Rails, and Scriptaculous. First let me say that I think the choice of which libraries to cover is spot on. The only obvious omission is Dojo, and Dojo is so big and ambitious that a chapter overview in "hacks" format would be useless. And that leads me to the problem: These chapters are not collections of hacks, rather they are introductory chapters that have been wrestled into a "hacks" format. All of these topics have much better coverage online and in other books (which I will be reviewing as this series continues).

The Rails and DWR chapters have particular issues: Chapter 7 (the Rails chapter) is way behind on Rails, claiming coverage of Version 0.9.4. Thomas and Hansson's excellent book Agile Web Development with Rails has a more up to date Ajax chapter, despite having a publish date that is nine months earlier. The DWR hacks (Chapter 5) miss almost everything that is cool about DWR. Hack #48 demonstrates accessing a custom object by having the object store values in a Map and do its own toJSON work. There is no mention of DWR's ability to automatically marshal real-world Java beans, which are typically not backed by Maps. Hack #49 commits the cardinal sin of RPC, doing in multiple roundtrips what could be done in one. This is ironic, since DWR supports a batching operation which automates doing the right thing.

Chapter 8 rocks. Read it. Hacks #68 and #69 demonstrate not one, but two, different ways to "fix" the back button in Ajax applications. #70 adds timeouts to Ajax invocations. #75 and #77 begin to embrace the browser as its own MVC environment, creating client side caches and search indices. This is almost the opposite of what most people think of Ajax -- using the flexibility of JavaScript to reduce communication with servers. Finally, my personal favorite, #73, clearly explains how to use dynamic script tags to cheat the browser security model, letting one page invoke services from multiple domains. (Use with caution, of course.)

In summary: Intermediate to Advanced Ajax developers should read chapters 4 and 8, and beginners should read something else.