Maintainable JavaScript (book review)

Maintainable JavaScriptJavaScript has, it seems, finally come into its own, and with its new immense popularity, attracted the attention of opinionated writers of every ilk. The latest book in O’Reilly’s growing collection of excellent books on all things JavaScript – _Maintainable Javascript _by Nicholas Zakas -- is a worthy addition to the cannon, not because it brings anything new to the table per se, but because it consolidates a lot of different source material into a roadmap of sorts, useful for two primary target audiences: 1) developers who are new to JavaScript development but are otherwise familiar with traditional software practices such as unit testing, continuous integration, structured builds, etc., and 2) casual JavaScript developers who have probably hacked some jQuery in the browser but are really interested in what all the cool kids are doing now. Zakas, a former Yahoo employee with blood, sweat and tears invested in YUI, has divided his work into three parts, two of which are augmented by detailed appendixes. The first part covers JavaScript style guidelines, and is really a cross-section of The Gospel According to Douglas Crockford (a.k.a., The Good Parts), and YUI, jQuery and Dojo recommended coding conventions. The JavaScript community is anything if not opinionated, and the division over minutia really shines in this section. Zakas stresses that, “[r]ather than trying to force my preferences upon you, this [section] highlights important aspects that should be covered in your style guidelines.” He notes that his own personal heresies can be found in Appendix A in case the official dissension isn’t enough. Though I troll, the material in section one is still pretty important. Zekas is right when he claims that teams should make an effort to follow consistent guidelines (regardless of _which _guidelines they happen to prefer). Style variations in big projects can be a source of confusion at best, and costly bugs at worst. There are many conventions on which JavaScript community leaders agree, and when they differ, it is often obvious that preference, not actual danger, is the driver. Seasoned developers will feel at home reading about formatting, commenting, proper statement and expression structure, and how to property declare variables and functions. For junior folk, or those new to the language, these conventions can be invaluable specifically because JavaScript behaves in ways that are occasionally surprising (e.g., variables are scoped at the function level, not the block level, so variable declarations can be trickier than most other languages). In part two, Zakas examines some best practices for writing JavaScript code. There is less subjective material here, mostly because “best practices” in JavaScript are like guard rails on a highway. They are not there for aesthetic appeal, they are there to save your life. Veteran JavaScript developers know how malleable JavaScript code can really be and prefer to prevent accidents rather than recover from them. Zakas discusses the dangers of global objects, the problems with type coercion and equality checks, when and how to safely modify objects, best practices for throwing and handling errors, and several other topics (like separation of concerns) that, while not particular to JavaScript, remain treasured as received wisdom. Part three is not so much about JavaScript code as it is about structuring and running a JavaScript project. Most developers first experienced JavaScript as a second-hand language used to manipulate web pages but otherwise, considered it inconsequential. JavaScript is now a first class citizen among languages, though, and as projects increase in size and complexity, a number of tools and practices have emerged to make managing those projects easier. Zakas starts this section by discussing a build process, for which he chose the well-known tool Ant as a demonstration candidate. The build process forms the backbone of each chapter in this section, as each topic relates to the build process in some way. A few salient chapters cover common browser needs, such as file concatenation and minification, and several others cover universal concerns such as automated documentation and automated testing (both unit and integration). Zakas covers a primary technology in each chapter (e.g., JSDoc toolkit for automated documentation) but provides a very useful, fairly comprehensive list of alternatives in an appendix. The final chapter in this section looks at the total build configuration with all moving parts – a very useful ten-thousand foot view of a large-scale JavaScript project setup. _Maintainable JavaScript _is fairly short, and quite enjoyable, although I feel that it does repeat a great deal of content from Crockford, et al, especially in the first two sections. This is not necessarily bad, but if the reader is already familiar with the opinions of these community leaders, it would be acceptable (and beneficial) to forego the first two sections and jump straight to section three. The appendices are also useful for all readers, regardless of previous exposure. JavaScript virgins would do well to pay attention to all sections, however, and would also do well to read Crockford, if only for his more esoteric explanations of JavaScript’s quirks and goods. Zakas does a good job making this information accessible and concise, and his inclusion of alternative perspectives provides a wider view of the community as a whole. If you’re new to JavaScript, or you’re looking to stand up your first significant JavaScript project, invest in this book. It is money well spent.