in Action, ISBN 1-930110-97-9, by Jack Herrington
January 2004, send
to the web site
(print), $22.50 (ebook)
is fun. It's all about solving interesting problems
and inventing new algorithms. It's about
getting computers to do new things, to do them better,
to do them faster. Or so the story goes. In reality,
in most large software projects, a sizeable portion
of programmers' time is spent implementing the infrastructure
around the juicy bits - the more mundane task of
creating what is usually referred to as "the
plumbing". Much of this plumbing usually revolves
around database access code, test code, reports from
data sets, forms, and other repetitive aspects of
larger software applications.
It's somewhat surprising how programmers tend to
resist delegating those tasks to the computer itself.
After all, don't computers specialize in handling
repetitive, boring tasks? Code Generation comes to
the rescue. Code generation is any process designed
to create source code, typically from some metadata.
This metadata might be a database schema, in the
case of data access code for instance, or it can
even be the code itself, using embedded markup to
generate the repetitive portions.
Code Generation in Action endeavors to introduce
programmers to the varied techniques used in generating
source code. The book is divided into two main parts.
First, fundamental principles of code generation
are explained, and various generator types explored.
The second part of the book, titled Code Generation
Solutions presents a handful of cases studies in
each of eight categories of problems, and provides
different techniques for solving each generation
The overall layout of the first part is very effective.
Each section builds on the previous one in a natural flow.
Several generator types of increasing complexity are explained
in detail and sample implementations are provided and annotated.
The code annotations are particularly handy, despite some
slightly annoying editorial mistakes; some variable names
in the code don't match the text describing what they do,
for instance. Hopefully, those will get fixed in the second
One initial surprise was the author's choice of language
for the generators themselves. The sample generators in
the book target several different languages with which
most programmers will be sufficiently familiar to easily
grasp the intent. However, the generators themselves are
all written in Ruby, a relative newcomer to the world of
scripting languages. I was initially wondering why author
Jack Herrington hadn't chosen Perl or Python. Herrington
anticipated this question and covers it fairly well in
chapter 2. I wasn't familiar with Ruby before reading this
book and I must admit I was a quick convert. The expressiveness
of its syntax makes it an elegant solution for most any
scripting need. For the reader's convenience, chapter 3
includes a small crash course, and appendix A offers a
more meaty introduction. The Web is also a plentiful source
of documentation and tutorials on using Ruby. At any rate,
the Ruby coverage of the book should be quite sufficient
for understanding the code samples, and I'm willing to
bet you'll be curious enough to fire up your web browser
and kick the Ruby wheels yourself.
Part 2 of the book is meant to expose the reader to more
realistic case studies and offers several solutions for
each problem. The first sections cover each generator and
technique in depth. As the book progresses, the problems
become somewhat repetitive. The text starts to address
the particularity of each problem and glances over aspects
already covered in previous sections. Despite the author's
efforts to keep it lively, it does get a little monotonous
towards the end. Regardless, Part 1 and much of Part 2
amply justify the purchase price, and you can easily look
up any particular problem in Part 2 when the need arises.
A few recurring topics throughout the book are worth mentioning.
The importance of unit testing human-created as well as
machine-generated source code can't be overstressed. Herrington
offers sound advice on implementing unit testing strategies,
including - you guessed it - unit test generators. There's
also a handy regular expressions primer in the Appendix.
As is now pretty standard procedure, the source code of
all examples is available online.
All in all, this is a great read and should provide you
with a good reference. If you're involved in code generation
or if you're simply curious about the subject, this book
should be on your shopping list. Recommended.
Letters to the Editor are welcome and occasionally abused in public. Send e-mail to: firstname.lastname@example.org