Monday, December 14, 2015

Book Review: Learning Underscore.js

The purpose of Alex Pop's Learning Underscore.js (Packt Publishing, October 2015) is to "explore the Underscore.js library using a test-driven development approach." There are seven chapters in this book on Underscore.js covering just under 200 substantive pages.


The Preface of Learning Underscore.js discusses JavaScript's recent surge in popularity and the role of Underscore.js in that rising popularity. The author states, "For a JavaScript developer, Underscore is the JavaScript-based library that can be used to create code that runs everywhere or as a foundation for adopting a functional programming style."

The "What this book covers" section of the Preface provides brief summaries of each of the book's seven chapters. The "Who this book is for" section states that the book is for "developers with fundamental JavaScript knowledge who want to use modern JavaScript libraries to advance their programming skills." You can find the full content of these two sections of the Preface in the author's blog post Announcing my upcoming book "Learning Underscore.js".

The Preface also has a section "What you need for this book" that a Node.js installation and text editor or IDE supporting JavaScript are the necessary tools for working with the book's examples.

Chapter 1: Getting Started with Underscore.js

The initial chapter of Learning Underscore.js "introduces you to Underscore and explains the main problems addressed by this library together with a quick introduction to functional programming." The chapter also opens by stating it is assumed that the reader knows "JavaScript programming fundamentals and how to create basic web pages using HTML, CSS, and jQuery." The chapter states that it is Underscore 1.8.3 covered in this book and that Underscore "provides an extensive set of functions that simplify and enhance handling of JavaScript objects, arrays, and functions ... by providing missing functional programming features to JavaScript."

Chapter 1 explains Underscore's original rise to prominence based on its "support for some of the functionality introduced by ECMAScript 5" that "made it a useful library for web applications targeting older browsers ... and for developers that wanted to write code that was based on ES5 without worrying about browser support." The section goes onto explain that this is not Underscore's primary use anymore and that a library such as es5-shim is better suited for that. The chapter uses this discussion to point out, "All of this book's examples assume that they are executed against a JavaScript version that is ES5 compliant."

The first chapter features a section called "Getting started with Underscore by example" that uses examples to introduce Underscore's features, capabilities, and advantages over straight ECMAScript 5 (JavaScript). The first examples contrast implementing "find" capability with forEach calls versus using Underscore's find. The Underscore global object and its default name reference (_) are introduced in the context of these first two examples.

Other examples in Chapter 1 illustrate Underscore's support for functions countBy, pairs, each, map, and reduce. Because these last few Underscore functions are functional in nature, it's a natural fit or this section of the first chapter to provide brief background on "functional programming style."

The section of Chapter 1 called "Useful patterns and practices for JavaScript applications targeting ECMAScript 5" introduces "some JavaScript patterns and practices" that are "essential when writing ES5 code, but you don't need them when writing ES6 code using classes and modules." Topics in this section include dealing with JavaScript's hoisting and the "difficult problem" of global scope with immediately-invoked function expression and revealing module pattern. The chapter also introduces JavaScript's strict mode.

The final major section of the first chapter, "Setting up a development workflow for exploring Underscore," introduces several key components of a "development workflow that is popular with JavaScript developers." These include discussion of installing Node.js with Node Package Manager (npm) on Windows, Linux, and Mac OS X. This section also describes managing dependencies with Bower, choosing a JavaScript editor (such as WebStorm, Cloud9, Node.js Tools or Visual Studio, Sublime Text, and Atom), and testing with Jasmine.

Chapter 2: Using Underscore.js with Collections

The second chapter of Learning Underscore.js revisits the "key Underscore functions" each, map, and reduce introduced in the first chapter. Concepts such as "JavaScript reflection like techniques" and duck typing are discussed in this context.

Chapter 2 delves into searching with Underscore and introduces Underscore functions some, findWhere, and contains in addition to revisiting find. Similarly, the second chapter's dive into sorting looks at Underscore functions filter, where, reject, partition, and every.

The first chapter had introduced an example of an aggregate function (reduce) and of a transformation function (map). The second chapter defines the difference between these two types of functions, delves more deeply into how to apply both of these types of functions, and introduces new Underscore functions of each type. The newly introduced aggregation functions are max, min and the newly introduced transformation functions are sortBy, groupBy, and indexBy.

Chapter 3: Using Underscore.js with Arrays, Objects, and Functions

Chapter 3 of Learning Underscore.js begins by looking at some array-specific functions provided by Underscore: first, rest, last, initial, union, intersection, difference, and zip. It also looks more briefly at several other "array-related functions."

The third chapter's section "Objects" opens with the sentence, "Underscore has a series of dedicated functions targeting objects, which extends the features provided for collections." Functions covered (code listings and explanations) in this section include keys, allKeys, values, invert, pairs, contains, pick, omit, extend, clone, has, property, propertyOf, matcher, isEqual, and isMatch. This section also looks at "assertion functions" for "object validation," "determining object types," for String objects, and for Number objects.

Chapter 3's section "Functions" opens with the sentence, "Underscore has a series of functions that specifically target objects of the type Function: it has functions that target other functions." The section discusses bind, bindAll, partial, memoize, wrap, negate, compose, delay, throttle and debounce, once, after, and before. The third chapter ends with a list o miscellaneous utility functions each with very brief descriptions.

Chapter 4: Programming Paradigms with Underscore.js

The first three chapters of Learning Underscore.js provided an "overview of most of the Underscore features" and the fourth chapter looks at using Underscore in object-oriented and functional paradigms. The chapter states, "JavaScript is a dynamic multi-paradigm programming language that can be used with an imperative, object-oriented, or functional programming style." The chapter provides an overview of the object-oriented paradigm as it pertains to ECMAScript 5 and ECMAScript 6 versions of JavaScript before looking at "Using Underscore with the object-oriented programming paradigm." The stated objective of this section is to demonstrate that "the Underscore object-related functions [can help] us build a class implementation that should feel familiar to developers coming from a class-based OOP language."

Chapter 4 also looks at "the functional programming paradigm," looks at what it takes to migrate from an object-oriented paradigm to a functional-oriented paradigm in JavaScript, and then looks at "functional programming with Underscore.js." The chapter explains that Underscore provides "built-in support" for the "method-chaining concept" with the chain, map, and tap functions.

Chapter 5: Using Underscore.js in the Browser, on the Server, and with the Database

Learning Underscore.js's fifth chapter looks at using Underscore in the contexts of browser (Underscore templates), server (Node.js), and database (MongoDB and PostgreSQL).

The fifth chapter's discussion of using Underscore in the browser looks at using Bootstrap as a "frontend design framework" and the looks at how applying Underscore templates enables creation of "a better solution." Chapter 5's look at JavaScript on the server provides more information on using Node.js before describing how to "[convert] JavaScript code to Node.js modules."

The "Using Underscore with MongoDB" section of the fifth chapter introduces that "popular document database" and provides a high-level overview of MongoDB conceptually and compares and contrasts this NoSQL implementation with relational databases. The section looks at how MongoDB makes use of JavaScript and describes how to install MongoDB on Windows, Ubuntu Linux. and Mac OS X. This section also explains how to configure and run MongoDB and then provides an example of using the MongoDB JavaScript API and the MongoDB JavaScript shell. There are several pages explaining and demonstrating application of the "MongoDB Node.js driver."

Chapter 5 also has a section called "Using Underscore with PostgreSQL" that introduces PostgreSQL's support for "JavaScript as a procedural language through the PL/v8 project." This section describes how to install and configure PostgreSQL, how to install PostgreSQL on the same three operating systems as shown earlier for MongoDB, and "using psql with basic database commands." The section also looks at PostgreSQL data types before delving into details regarding PostgreSQL's jsonb type.

Being a fan of the PostgreSQL database, I particularly enjoyed this section on using JavaScript with PostgreSQL and especially liked the section on "using PostgreSQL with plv8." This section first shows use of JavaScript to write PostgreSQL stored functions and then shows how to use Underscore with those implementations.

Chapter 6: Related Underscore.js Libraries and ECMAScript Standards

The focus of Chapter 6 of Learning Underscore.js is on libraries that are "alternative and complementary" to Underscore.js and on Underscore.js's "relationship with JavaScript standards and how to write the ECMAScript 2015 (ES6) code." The chapter explains that the alternative and complementary libraries can be desirable because Underscore itself is relatively small.

Underscore-contrib is introduced in this sixth chapter. The chapter explains the relationship of Underscore to Underscore-contrib and provides "an overview of Underscore-contrib functionality." It covers select functions from the sub-libraries provided by Underscore-contrib, discusses installing Underscore-contrib, and provides a couple of examples of applying Underscore-contrib.

The lodash library is also introduced in Chapter 6 as a JavaScript library originally forked from Underscore that focuses "on better performance, improved consistency across browsers, and enhanced code readability." This section outlines how lodash varied originally from Underscore and eventually became incompatible with Underscore. The section also provides an "overview of lodash functionality" that compares and contrasts some of Lodash's functions with similar ones in Underscore.

It surprised me a bit that a book about Underscore and with "Underscore" in the title mentioned ways in which lodash might be considered superior to Underscore. There is even a section in Chapter 6 on "migrating a project from Underscore to lodash", a reference to a Wiki page on migrating from Underscore to lodash, and a reference to another Packt Publishing title Lo-Dash Essentials.

Chapter 6 of Learning Underscore.js talks about Underscore's relationship to ECMAScript/JavaScript standards. The section begins with background details: "Up until Underscore 1.6, there was a strong correlation between the library and some of the native JavaScript functions available in ES5. Starting with Underscore 1.7, this link was broken and the library introduced implementations that performed better than the native JavaScript equivalents." The section goes onto look at ECMAScript 5.1 (ES5) functions that are similar to Underscore functions and lists some of the advantages of the Underscore versions of these functions.

The section "ECMAScript 2015 (ES6)" describes advantages offered by ES6 and describes features of ES6. This section references the ECMAScript 6 Compatibility Table and explains how transpilers can be used to "convert code from ... upcoming ECMAScript specifications ... into code that is compatible with older specifications." Babel is introduced as a transpiler for converting ES6 code to ES5 code and examples of using Babel from a web browser, from the command-line, and in conjunction with Node.js are provided. Several examples are provided of using Babel to use ES6 functionality.

Chapter 7: Underscore.js Build Automation and Code Reusability

The final chapter of Learning Underscore.js begins by looking at installing Gulp and using Gulp for deployments. The chapter also includes a section "Reusing code based on Underscore between client and server" that covers use of CommonJS, Browserify, Jasmine, and Gulp to enable using code on client and server. The final section of the chapter and of the book illustrates using babelify to add "ECMAScript 2015 (ES6) support to Browserify."

General Observations

  • Learning Underscore.js introduces Underscore.js and provides relatively extensive coverage of this JavaScript library. It goes a bit beyond Underscore by covering lodash and by covering other JavaScript-oriented libraries and tools that allow for working with new ECMAScript features.
  • I liked that Learning Underscore.js liberally referenced online materials for more details on many of the topics it introduced. Some of these articles and resources are excellent and I was glad to have them pointed out.
  • I liked the book's coverage of running JavaScript in different contexts such as browser, server, and database. The book helped show that it's possible to do this, but also demonstrated that doing so is still not trivial and does require additional effort to achieve a full degree of cross-platform compatibility.
  • Learning Underscore.js includes numerous code listings. Even in the PDF copy of the book that Packt Publishing provided for my review, the code listings are black font on white background with no color syntax and no line numbers. However, each chapter references where the code can be downloaded to be used in the reader's favorite IDE or editor. These references include Cloud9 links.


Learning Underscore.js provides a thorough introduction to Underscore.js that includes explanation of why it's popular among JavaScript developers and some of the functionality it makes available above and beyond current JavaScript implementations. The book points out different approaches that can be used to start taking greater advantage of ES6 features today.

1 comment:

Francis Kim said...

I like how this book blends into ES6