Saturday, March 08, 2008

Two very Groovy books...

Over the 2007 holiday, I had the pleasure of doing technical reviews of two upcoming books published by The Pragmatic Programmers. First I reviewed Scott Davis' book, Groovy Recipes: Greasing the Wheels of Java by Scott Davis. I followed this up with a review of Programming Groovy: Dynamic Productivity for the Java Developer by Venkat Subramaniam. For the reader in a hurry, I will summarize by saying if you are just learning Groovy, Scott's book is a must have. If you have the Groovy Basics in hand, then Venkat's book will guide you to the next level. I highly recommend both books.

I met both Scott and Venkat at the Boulder Java Users Group. Scott both runs the JUG and is a frequent speaker. Venkat visits at least once a year to present talks.

The first talk of Scott's I had the pleasure of listening to dealt with agile software development. Scott is an amazing speaker whose excitement for the topic he discusses is truly infectious. I had already decided agile was the right way to go and Scott's talk helped me along the true path. I mention this because it was another talk of Scott's that introduced me to Groovy. Scott made two comments that caught my attention. First, he stated "Groovy is what Java would look like had it been written in the 21st Century." That is a pretty powerful statement. Second, he answered the question of why Groovy versus Ruby... the answer being the context switch is much easier for a Java developer using Groovy.

After listening to the talk, I downloaded the Groovy package and started playing with it. I soon realized there was a lot of power in this language. So I bought Groovy in Action, a.k.a. GINA, and really started digging into the language. At a later JUG, Scott mentioned that he and Venkat were planning to write their own book on Groovy. The language was evolving quickly and GINA was already getting out of date, so I started bugging Scott about the upcoming book. Long story short, eventually, the authors decided that they were each envisioning a different type of book and when Scott had a rough draft available, he asked me if I would be interested in being one of the technical reviewers. I jumped at the chance.

Scott's approach to the book is very agile, in that, after a brief discussion of the Groovy language, it essentially answers lots of very specific questions regarding "How to..." in Groovy. Each section starts with a code snippet that is designed to be copied directly into your code, letting you quickly solve a problem (e.g. parsing XMl documents) and move on. The detail in these examples ensures that this book will remain useful for some time to come. Along with the toolset you get, these examples really demonstrate the power of the Groovy language and provide an excellent way to learn the language.

While perhaps not as all encompassing a primer on Groovy as GINA, Scott does a good job, in the early part of the book, presenting most of the language's features in sufficient depth that I am comfortable suggesting this as your first book on Groovy. And once the basics are covered, Scott goes on to present some excellent examples of how to use idiomatic Groovy to solve common programming problems, including file I/O, XML processing, Web Services, basic metaprogramming, and a nice introduction to Grails, the web framework built with Groovy. After the Grails intro, Scott presents some nice examples of how to use Grails in a similar manner to his treatment of Groovy.

My approach to the review was to first read the text and understand what concept was being presented, followed by a test of the supplied code. This turned out to be very informative. There are three key ways to dynamically execute groovy code; as a script file from the command line, in the groovy shell, and in the groovy console. It turns out that the shell has slightly different execution behavior than either the console or script file approaches. This has to do with the fact that the shell has a binding that controls the visibility of variables that does not explicitly exist in the other environments. I found this issue and discussed with Scott. I have not seen the final version of the book, but if this is covered, it will represent my key contribution to the book.

Scott (or at least his publisher) must have liked the work I did on the book, because after I completed the review, I was asked to review Venkat's book.

I have had the opportunity to listen to Venkat speak on numerous occasions and at one point, described my self as a Venkat groupie! Watching Venkat speak is a truly humbling experience. Not only does he have an amazing ability to describe complex technical concepts clearly, but he does this while writing code to demonstrate these concepts at the same time. This ability to communicate crosses over to his books and his latest is no exception.

While the book does cover some of the basics of Groovy, Programming Groovy is probably not the best book to use for learning the language. Venkat spends more time covering the interesting "edge cases" and "gotchas" of the language than on the basics. For example, he goes into the details of how Groovy's == operator is not the same as == in Java. For someone who already has a basic understanding of the language, the exploration of these more advanced topics is a great way to move to the next level in mastering the language. But it is Venkat's exploration beyond these topics that make this a critical reference for the developer serious about learning about the paradigm shift that dynamic programming entails.

Venkat spends time comparing the strong type checking in Java to the optional typing that exists in Groovy. He makes the case for why the strong typing, while it provides the semblance of safety for the developer, is really very weak protection. he then carries this topic forward and discusses closures, which, when combined with dynamic typing, form the first building block for dynamic programming. He then proceeds to go into the most important topics in the book, Unit Testing and Metaprogramming. With Metaprogramming, which is the ability of a program to manipulate itself, it is possible to create new domain specific languages (DSLs) and sophisticated builders that simplify other programming tasks. Venkat provides great examples of how to use these techniques in Groovy programs.

Dynamic programming is becoming increasingly relevant to modern software development and Programing Groovy is a great introduction to these concepts along with concrete examples in Groovy.

My approach to reviewing this book was basically the same as for Scott's book. Examine the examples and make sure they make sense and they work. Hopefully, Venkat and his publisher found my comments helpful. I really enjoyed this opportunity and hope I have the chance to do more reviews in the future. It is a great way to learn a new technology, since you really have to focus on the details.