|Groovy for Domain-Specific Languages is an excellent introduction to the fairly difficult subject of creating DSLs. The book is short enough to make it a weekend or 3-day project and the knowledge gained will be useful.|
One of the more widely held beliefs among software developers is that
if we can just make the software simple enough to use, we won't have to
do quite so much technical support and documentation. Another, and
probably more valid, belief is that by writing more flexible
applications and frameworks, we can get more bang for the buck for our
time. Unfortunately, someone still has to configure, specify or
otherwise control our applications and frameworks. To this end has
arisen the Domain Specific Language, or DSL. DSLs come in a variety of
types, but one such is the "executable language", where an application
or framework makes an execution environment for the DSL. This is to
distinguish it from a generated parser type of DSL . Groovy, being a
scripted language with strong metaobject protocol support built in,
lends itself well to writing executable DSLs. But how does one learn
to write DSLs in Groovy? There are resources on the groovy website, but they lack extensive examples. A book dedicated to the subject like this one can help expand your knowledge of this frequently difficult subject.
Like many technical books, the first chapter was an introduction to the concepts of DSLs and the next couple are absorbed with general Groovy information best learned elsewhere. Despite the protestations of the author, it's highly unlikely that someone completely unfamiliar with Groovy would dive right into writing a Groovy-based DSL. For general Groovy information, get Groovy in Action from Manning.
Once past the intro to Groovy, however, the subject matter starts to take on serious weight. Starting with details on the implementation of a fairly simple DSL, the reader is taken along a path of increasingly more complex and more "DSL-like" implementations of the examples. Solid explanations of DSL-specific subjects surrounding the the meta-object protocol for Groovy was followed by excellent descriptions of several existing Groovy-based DSLs, like GAnt and GSpec. A substational number of pages were dedicated to GORM, the Grails glue to hibernate, but didn't really provide a lot of value beyond the existing GORM documentation. The sections with the greatest value, past the elements of metaprogramming, involved implementation of a rules DSL that would be very easy to re-purpose for your own needs. The work closed with a chapter on integration. This section was brief but seemed like an afterthought. However, given the solidity of the interior of this relatively short book, that wasn't really a detraction.
As is common with Packt publications, when reading this book it seems best to do so from front to back as a series of lessons rather than trying to use it as a reference manual or skipping around. The knowledge builds across the flow of the book, so starting in the middle can leave the reader feeling somewhat lost.
As previously stated, this is very solid instruction in the basics of generating a DSL in Groovy. The style tends to be generally succinct without a lot of fluff. The author seemed occupied with providing real working knowledge to solve real problems. The examples (nearly always) worked and were usually very clear.
With only 9 chapters, dedicating 3 of them to relatively tertiary subjects made the book seem even shorter. Also, a few of the examples in the downloadable source code were clearly not meant to be run from the command line, as they failed when tried but passed in the Groovy console.
Overall, I would strongly recommend this book for anyone trying to implement any sort of DSL in a JVM-based environment. Groovy's seamless integration into running Java code makes it a clear choice, and this book's excellent presentation and solid examples provide a good foundation to build upon.
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)