Monday, June 1, 2015

Book Review: CoffeeScript Application Development Cookbook

Mike Hatfield's CoffeeScript Application Development Cookbook (Packt Publishing, March 2015) has the subtitle, "Over 90 hands-on recipes to help you develop engaging applications using CoffeeScript." This book on CoffeeScript features eleven chapters spanning approximately 340 substantive pages. As its title suggests, this is a "cookbook" style of book. Its recipes begin by covering introductory items and then move into more specific and more involved CoffeeScript uses.

Preface

The Preface of CoffeeScript Application Development Cookbook briefly explains CoffeeScript's significance: "JavaScript has become one of the most widely supported programming languages in the world. Unfortunately, little has changed with JavaScript in this time and it is plagued with gotchas that can make developing a pure JavaScript system challenging." The Preface also outlines each chapter with a sentence or two overview and you can find similar high-level descriptions of the chapters of this book in the author's LinkedIn blog post My Book Has Been Published.

The "What you need for this book" section of the Preface states, "To use the code in this book, you will need a code editor of your choice, a web browser, and an Internet connection to download Node packages and other libraries or frameworks." The "Who this book is for" section states, "If you enjoy developing applications that can be run on desktop, tablet, and mobile devices without needing to learn platform-specific languages, this is the book for you."

Chapter 1: Getting Ready

CoffeeScript Application Development Cookbook's initial chapter emphasizes the appeal of CoffeeScript and includes the statement, "JavaScript, though very successful, can be a difficult language to work with." He adds, "These pitfalls found in the JavaScript language led Jeremy Ashkenas to create CoffeeScript, a language that attempts to expose the good parts of JavaScript in a simple way. CoffeeScript compiles into JavaScript and helps us avoid the bad parts of JavaScript." This section lists several compelling benefits of CoffeeScript; as I read these, I could not help but think that CoffeeScript brings some of the same advantages to the world of JavaScript that Groovy brings to the world of Java.

The chapter's (and book's) first recipe discusses downloading and installing the tools used in a CoffeeScript development environment including Node.js, CoffeeScript via Node Package Manager (npm) and the -g option, and editors/IDEs Sublime Text (Better CoffeeScript) and Visual Studio (Web Essentials).

The "A quick dive into CoffeeScript" recipe introduces key CoffeeScript syntax. This is an interesting discussion that shows how CoffeeScript can be more expressive than JavaScript (sometimes via adoption of syntax sugar from other languages like Ruby) and can even cover up some of the ugliest parts of JavaScript (such as comparison coercion). This rather lengthy recipe also demonstrates command-line use of the coffee command and application of the CoffeeScript REPL.

One recipe in the first chapter talks about why debugging CoffeeScript can be tricky and demonstrates using source maps with Chrome to map CoffeeScript code to compiled and executed JavaScript code being run in Chrome. Another recipe demonstrates use of Node Inspector with source maps to debug CoffeeScript on the server.

Chapter 2: Starting with the Basics

Although the first chapter introduced some syntax and concepts of CoffeeScript, it's Chapter 2 that provides a more comprehensive introduction to CoffeeScript syntax and language. The chapter introduces CoffeeScript's Ruby-like (and Groovy like) string interpolation, Groovy-like multi-line strings ("block strings") with three double quotes, overriding toString() in CoffeeScript for an object's default coercion value, regular expressions, and other text/String manipulation.

Recipes in Chapter 2 also describe working with numbers in CoffeeScript and demonstrate using CoffeeScript with JavaScript functions parseInt(), Math.random(), Math.floor(), and basic math operations. Other recipes in the second chapter cover use of dates and times in CoffeeScript, and use of CoffeeScript arrays.

I found the section of Chapter 2 on CoffeeScript classes to be particularly interesting. It states that compiled CoffeeScript classes become "constructor functions that are wrapped within a closure" to "keep private members private." I wrote about doing this directly in JavaScript in the post JavaScript Objects from a Java Developer Perspective and its looks like CoffeeScript makes it even easier to take advantage of approaches like these. This section also talks about defining methods on the CoffeeScript classes that "are compiled into functions on the object's prototype." I have described this JavaScript behavior in the posts JavaScript Objects from a Java Developer Perspective and A Java Developer's Perspective on the Power and Danger of JavaScript's Object Prototype.

Chapter 2's discussion of CoffeeScript classes includes discussion on and examples of using class inheritance and defining class-level (as opposed to instance level) variables and methods. JavaScript's 'this' keyword is notoriously tricky and the second chapter of CoffeeScript Application Development Cookbook discusses use of CoffeeScript's fat arrow (=>) to deal with this.

Chapter 3: Creating Client Applications

CoffeeScript Application Development Cookbook's third chapter discusses "popular frameworks and libraries that can be used with CoffeeScript when developing the user interfaces and client-side application code." The covered frameworks and libraries are jQuery, jQuery UI, Backbone, AngularJS, Socket.io, Underscore.js, Lightbox, and Jade. This chapter is filled with code listings. Most demonstrate these libraries and frameworks used in conjunction with CoffeeScript, but the nature of this integration is such that the examples seem more about introducing the frameworks and libraries than about CoffeeScript. In other words, much of Chapter 3's content would be as applicable to straight JavaScript as to CoffeeScript.

Chapter 4: Using Kendo UI for Desktop and Mobile Applications

Chapter 4 of CoffeeScript Application Development Cookbook introduces Telerik Kendo UI® Core, which the chapter describes as "an exciting open source framework from Telerik to implement user interfaces in pure HTML5, JavaScript, and CSS3." Recipes in this chapter detail downloading and referencing Kendo UI Core and applying Kendo UI Core widgets and mobile widgets. As is the case for Chapter 3, Chapter 4 has numerous code listings that demonstrate use of Kendo UI Core and Kendo UI Mobile with CoffeeScript, but many of these examples could be easily used for learning about Kendo UI used in conjunction with JavaScript directly if desired.

Chapter 5: Going Native with Cordova

CoffeeScript Application Development Cookbook's fifth chapter introduces Apache Cordova, which it defines as "a framework ... that allows you to wrap your web applications inside a native wrapper that can be packaged and made available via the various app marketplaces." It adds that "Cordova currently supports iOS, Android, Blackberry, Windows Phone, and FireFoxOS." Recipes in this chapter cover downloading Cordova via npm, creating a mobile application from Cordova's command-line tool, and using Cordova to access the device's camera, geolocation, contacts, and device information.

Chapter 6: Working with Databases

The sixth chapter of CoffeeScript Application Development Cookbook "how to use CoffeeScript to perform common create, read, update, and delete (CRUD) operations" against datastores SQLite, Redis, MongoDB, and CouchDB. The chapter's recipes focus on performance of CRUD operations against these various types of datastores from CoffeeScript.

Chapter 7: Building Application Services

The recipes of Chapter 7 describe using "CoffeeScript to perform common Internet-related tasks and create RESTful services to be used by our applications." These recipes cover things such as encoding and decoding "binary and base64-encoded files" (atob, btoa, fs), working with domain name services (dns) and IP addresses, parsing URLs (url), creating RESTful web services (express.js), and creating package configuration files with npm init. The chapter also introduces the Chrome extension Postman.

Chapter 8: Using External Services

Chapter 8's recipes focus on how to use third-party services such as Amazon S3, Twilio, GeoNames, email, and FTP in CoffeeScript applications. The chapter also introduces REST Client for Node.js, nodemailer, and, node-ftp.

Chapter 9: Testing Our Applications

Chapter 9 of CoffeeScript Application Development Cookbook describes "various methods of testing our CoffeeScript applications using a variety of techniques and libraries" such as QUnit, Mocha, Zombie.js, Sinon.js, Bower package manager, and live-server. As the recipes are presented, common testing concepts are discussed such as test-driven development and test doubles. This chapter presents numerous screen snapshots to illustrate test results in the browser and console.

Chapter 10: Hosting Our Web Application

Recipes in the tenth chapter of CoffeeScript Application Development Cookbook describe using Grunt to prepare CoffeeScript applications for deployment and then deploying those applications to "both the Heroku and Microsoft Azure cloud platforms." The Grunt-related recipes demonstrate downloading and installing Grunt and using Grunt to compile CoffeeScript into JavaScript and to minify the compiled JavaScript. The recipes on deploying to Heroku and Azure demonstrate creating accounts on those providers and deploying CoffeeScript applications to them.

Chapter 11: Scripting for DevOps

The final chapter of CoffeeScript Application Development Cookbook looks at "ways that DevOps can use CoffeeScript to perform some common tasks." Recipes in this chapter demonstrate application of child_process.exec() and child_process.spawn for executing shell commands from CoffeeScript, manipulating files in CoffeeScript with node's fs and fs-extra modules, archiving files and directories with archiver, parsing command-separated values files with node-csv, parsing fixed-width text files with moment.js and accounting.js, formatting dates with moment.js, formatting numbers with accounting.js, and padding and aligning output.

General Observations

  • As described in this review, CoffeeScript Application Development Cookbook covers a wide spectrum of technologies and concepts associated with CoffeeScript in addition to introducing and covering CoffeeScript itself.
    • Numerous open source JavaScript libraries and frameworks are presented and demonstrated that can be used with CoffeeScript or JavaScript.
    • Chapters 1, 2, 9, and 11 are the most CoffeeScript focused (though even some of them cover related technologies) and the remaining chapters emphasize related libraries and frameworks, what they are, and how they can be used from CoffeeScript code.
    • CoffeeScript Application Development Cookbook references Node.js several times in multiple recipes and contexts. This seems appropriate for a modern JavaScript (or in this case, CoffeeScript) book.
    • Debugging and unit testing of CoffeeScript code are covered in this book along with other CoffeeScript development topics.
  • The screen snapshots in CoffeeScript Application Development Cookbook are generally clear and illustrate well what's being described.
  • The text of CoffeeScript Application Development Cookbook is easy to read and generally flows well. There are some typos, but I never found any of them to prevent me from understanding what was being conveyed.
  • The code listings are generally short enough to be readable and focus on the point being made. These listings are black text on white background with no color syntax and no line numbers.
  • CoffeeScript Application Development Cookbook seems best suited for those who have some (even if it's minor) JavaScript experience and are looking for an introductory level book on CoffeeScript. Although the book is recipe-based, it's recipes are ordered and presented in such a way that, if read cover to cover, are fairly introductory and build on each other without requiring reading the earlier recipes.

Conclusion

CoffeeScript Application Development Cookbook does a nice job of introducing CoffeeScript and explaining how it can make JavaScript development easier, especially for those more comfortable with other class-based object-oriented languages. A few chapters focus on CoffeeScript, but most chapters cover a wide spectrum of related technologies and concepts. By covering use of numerous open source JavaScript libraries and framworks with CoffeeScript, CoffeeScript Application Development Cookbook repeatedly reminds the reader that one of the significant advantages of CoffeeScript is the ability to use JavaScript libraries and frameworks even while taking advantage of CoffeeScript's easier syntax and more consistent concepts.

No comments: