Digital Web Magazine

The web professional's online magazine of choice.

Review: Pro JavaScript Design Patterns

Got something to say?

Share your comments on this topic with other web professionals

In: Reviews > Book Reviews

By Tiff Fehr

Published on January 14, 2008

Back in 2006, I made a New Year’s Resolution to improve my JavaScript abilities. I failed miserably.

That year was an explosion of enthusiasm for Ajax methodologies, but looking under the hood of Flickr, A9 and Google Maps was intimidating for a barely-literate scripter like myself—JavaScript seemed to have morphed into a totally new language. With effort, I got a bit of a traction in DOM scripting, but faced resistance from colleagues about building anything more powerful in JS —the classic server-side vs. client-side stand off. Daunted but determined, I made a bit of progress and rolled my resolution over into the next year.

2007 shifted attitudes toward JavaScript even further than 2006, with widespread, focused discussion of JavaScript libraries and best practices—so much so that we wrapped the year with the release of Pro JavaScript Design Patterns by Ross Harmes and Dustin Diaz. If object-oriented design patterns are a sign of language standardization and maturity (as well as developer maturity) then, at the start of 2008, it seems JavaScript has come of age.

What’s it all about?

Pro JavaScript Design Patterns is a disciplined look at applying design patterns to Javascript through a strong object-oriented focus. For the benefit of pure front-enders, design patterns are common structures in programming—traditionally more common in strongly object-oriented languages such as C++ and Java—that focus on repeatable solutions which can be ported into multiple languages. Think of them as standardized, time-tested ways to answer specific programmatic problems, independent of one programming language or another. Object-oriented programming is a paradigm in which specific attention is paid to creating robust objects in code, with clean interactions built to be easily reused. In Pro JavaScript Design Patterns, Harmes and Diaz demonstrate how the innate flexibility of JavaScript can adapt to design pattern conventions, while also allowing programmers to choose from a variety of programming styles.

As client-side scripting grows in complexity behind elaborate rich internet applications and websites, design patterns can be used to build improved data security, performance, and consistency within large projects and teams. However, implementing design patterns requires attention to object-oriented programming, which—let’s face it—could do all of us some good.

Who is it for?

Pro JavaScript Design Patterns is clear about its audience: front-end developers adding object-oriented thinking to their JavaScript skills, and server-side programmers looking to pick up client-side coding. Yet a subtle third audience could be those who consider JavaScript a “toy language”, not up to par with the abilities of server-side languages. By applying the rigor and vocabulary of object-oriented design patterns to JavaScript, Diaz and Harmes demonstrate that JavaScript can have equal footing with traditional object-oriented languages, as well as some interesting advantages.

The book is not for beginners, or those lacking familiarity with object-oriented principles in programming. The original development of design patterns came out of large team projects for server-side programming, and as such, they are best used by larger-scale, team-driven projects. While it is important to understand how such projects could work with design patterns, I’d hazard a guess that most day-to-day scripting won’t call for an abundance of patterns. Still, knowing more about the direction JavaScript is heading is always a good thing, and many of the patterns align with coding best practices.

What will I learn?

JavaScript is a language that grows in complexity with a programmer’s sophistication. Harmes and Diaz take pains to cover the principles underlying object-oriented programming and show how they can be combined to create basic patterns that serve as a foundation to larger design patterns. Examples build on each other and combine to illustrate object-oriented approaches as building blocks. Above all, the authors stress that programmers should make intelligent decisions about how much object-oriented focus to put into smaller projects that might not need too much sophistication. Throughout the book, there is a frank discussion of the pros and cons of each technique, as well as points where it might be overkill.

The book is organized into two parts. Part One covers the basics of object-oriented JavaScript, explaining the underlying concepts that make design patterns the powerhouses that they can be. Diaz and Harmes start with the intricacies of concepts like interfaces, encapsulation, and inheritance, which form the basis of small patterns in JavaScript. Building on those, the chapter on the singleton pattern is a must-read for any level JavaScript developer, and forms an anchor concept for larger design patterns in turn. Singletons provide many advantages for websites using JavaScript from external sources: namespacing and modularization of code, as well as the ability to enclose heavy coding into objects that load after the main onload event (‘lazy instantiation’). For those with object-oriented experience, the chapters focusing on these basic patterns demonstrate how JavaScript can have an elegance and efficiency rivaling other object-oriented languages.

Part Two uses the styles explained in Part One to address specific design patterns. Diaz and Harmes show how JavaScript can follow classic design patterns like factory patterns (creating objects without specifying classes), adapter patterns (adapting the interface of one class to another, and excerpted this week on Digital Web), facade patterns (simplified interactions with a larger body of code via classes) as well as many others. Each pattern chapter serves as a reference, along with a frank discussion of uses, benefits and drawbacks. The authors also include detailed examples and even propose counter-examples to show where individual flourishes can take advantage of JavaScript’s flexibility.

Should I buy it?

We’ve all come up against projects requiring just a bit more than our current comfort levels. Part of the challenge and fun of being a web professional is diving into a new technology or skill set with just a few pointers about where to go to learn—and Pro JavaScript Design Patterns is a great reference to have in that regard. You may not have projects on the horizon that require writing object-oriented JavaScript for large projects where design patterns can really shine, but you will benefit from keeping them in mind. Even taken conceptually, design patterns can be used to bridge differing approaches between programmers, and add a unifying language about how problems are solved.

As a cheery DOM scripter with a JavaScript background including simplistic object-oriented functions, I had a fair learning curve regarding changes to JavaScript’s syntax in an object-oriented approach, particularly around function declarations. While the book strives to describe examples in plain English, jumping from scripting with basic encapsulation to high-powered object-oriented notation is syntactically intimidating—less intimidating than looking under the hood of most apps, though.

Summary

Pro JavaScript Design Patterns brings a strategic discipline from server-side, object-oriented programming mindsets to client-side JavaScript. By looking at client-side scripting as a series of problems where design patterns can shape the solution, programmers can use the flexibility and “expressiveness” of JavaScript to benefit communication, performance, and maintainability.

Helpful links

Got something to say?

Share your comments  with other professionals (6 comments)

Related Topics: Technology, Programming

 

Tiff Fehr is a user experience engineerette, specializing in design & front-end development. She lives in Seattle and works for msnbc.com. Tiff can be found tinkering away on tiffehr.com, amongst other side projects. She truly digs working for Digital Web Magazine, and adores her colleagues.

Media Temple

via Ad Packs