Catch up on stories from the past week (and beyond) at the Slashdot story archive

 



Forgot your password?
typodupeerror
×
Image

Groovy For Domain-Specific Languages 81

RickJWagner writes "This is a book targeted primarily towards the Java programmer who doesn't yet know Groovy. Groovy is a dynamic scripting language that runs on the JVM and offers easy integration with the Java language. Groovy also offers dynamic language features that lend themselves to the creation of DSLs (Domain-Specific Languages). This book attempts to simultaneously teach the reader how to use Groovy and how to build a DSL." Keep reading for the rest of RickJWagner's review.
Groovy for Domain-Specific Languages
author Fergal Dearle
pages 312
publisher Packt Publishing
rating 7/10
reviewer RickJWagner
ISBN 184719690X
summary a practical tutorial, walking the reader through examples of building DSLs with Groovy covering meta-programming with Groovy
DSLs are something of a hot topic these days. A DSL is a 'Little Language' or a language written just for one business domain. As programmers, we're familiar with DSLs whether we realize it or not-- each time you use 'Make' to build a C project, or use 'Ant' to make a Java project, you're using a DSL. A DSL is a purpose-built language, with it's own verbs, rules, syntax, etc. It's made for one purpose only, and is meant to make things as easy as possible for the intended user. If the user has a need outside what the DSL can provide-- they're just tough out of luck! General purpose languages are for doing whatever the user needs-- DSLs are for doing only one task, but making that one task exceedingly easy for the user.

The book does a good job of making Groovy understandable for a Java coder. Programmers of other stripes would be able to use the book, but the author aimed squarely for the Java sweet spot and I think did a good job of hitting it. Where language integration was to be shown, the JDK was the reciprocal part in the examples, and the classes singled out for interfacing were ones that would be well-known to a Java coder.

Writing DSLs has been done for many years, but was largely an undocumented process until just recently. I think the rise of dynamic languages has something to do with that. Dynamic languages offer some key features that make DSL creation a little easier. A few of these features the book covers are closures and meta-programming. Closures are anonymous methods, packaged in such a way that they can be passed as parameters to other methods. Meta-programming is using code to write other code, and can be used to perform some neat tricks like allowing the user to call methods that don't have a previously written definition. (If that sounds really strange to you, I agree. But it turns out this is something very handy if you're writing a DSL!)

The book is written in a way that thoroughly the breadth of features offered by the Groovy language, but not a lot of depth in all of them. For this reason, I found myself sometimes referring to external sources when I was encountering some language construct that seemed a little less familiar than most. It wasn't a problem-- the book did a fine job of letting me know of the existence of some language feature and how it might be used in writing a DSL. If I didn't immediately grasp the use of the feature after the typical single example of seeing it used in sample code, I'd just pop out to the internet and get a few more examples. The book is nearly 300 pages as it is, and very little of that is wasted. I give the book good grades for technical content.

Style-wise, I found the book reasonable to read but not great. As well as the author does spelling out the nuances of the Groovy language, the DSL parts are interspersed among the chapters and sometimes could be hard to find unless you read a lot of surrounding technical content. Maybe this is the only way to cover two meaty topics at once, I don't know. But I found myself doing quite a bit of paging when I wanted to refer back to DSL-specific advice, sometimes falling back to the chapter-by-chapter summary in the front of the book to get myself in the right neighborhood.

The book has plenty of sample code, which I found well constructed and easy to read. The author adapted the practice of writing small snippets of code, then used assertions to demonstrate the behavior the reader would expect if the code were to be executed. I really liked that. The code itself was always cleanly formatted and minimal in length, which made it very easy to read.

So, did the book hit it's target? I believe it did. The strengths of using Groovy for Domain Specific Languages are drawn out, and the Groovy language is given a thorough overview. Java coders will find this book a reference that will let them start using dynamic features while still staying on the JVM. (By the way, Groovy compiles to Java, so all your existing Java code is usable from Groovy. So you might write an application that's 95% Java and 5% Groovy, and that's just fine with Groovy.) I think this book covered a lot of ground and did it well.

You can purchase Groovy for Domain-Specific Languages from amazon.com. Slashdot welcomes readers' book reviews -- to see your own review here, read the book review guidelines, then visit the submission page.

*

This discussion has been archived. No new comments can be posted.

Groovy For Domain-Specific Languages

Comments Filter:
  • by Anonymous Coward on Thursday July 08, 2010 @01:40PM (#32842432)

    Exactly - lex and yacc have been around since the 1970's, and the process is quite well understood and documented.

    The article was probably written by someone without a formal CS degree, who was unaware of these things. Nothing wrong with ad-hoc learning of course, it works great for many folks, it's just that you don't get exposed to certain things unless you manage to discover that you should go invest in learning about them.

  • Parser generators (Score:3, Interesting)

    by Animats ( 122034 ) on Thursday July 08, 2010 @02:55PM (#32843328) Homepage

    Parser generators are a well-studied problem, and there are good solutions. Unfortunately, the "dynamic scripting language" people don't seem to be much into parsing theory. Trying to get a complex grammar to go through yacc or bison isn't easy, but at least once the parser generator will accept it, you know you've hammered out the ambiguities. It's much easier to get parsers from systems like "pyparsing" to sort of work, but you have to run many test cases to get them to work right.

    (I'm currently writing a parser with "pyparsing" for US postal addresses. There's a trivial one available, but it doesn't handle the hard cases, and I had to essentially rewrite it. This is not fun. Some current fails: "969 Edgewater G370", "32545B Golden Lantern 147", "3853 7 Trees Blvd". It's not too hard to get to 98% success; then there's a proliferation of special cases.)

  • Re:What about FORTH? (Score:3, Interesting)

    by digitig ( 1056110 ) on Thursday July 08, 2010 @02:59PM (#32843362)
    No, FORTH is general purpose, even though it was developed for a particular domain (real-time control). The language extensibility means it's great for writing DSLs as long as security isn't an issue and you don't mind them being reverse Polish (in other words, DSLs for geeks working in god mode anyway), but it's hard to avoid whatever DSL you write being embedded within the general purpose language.
  • by mcvos ( 645701 ) on Thursday July 08, 2010 @06:15PM (#32845272)

    It looks like the reviewer is fairly new to this programming thing, and this was the first insightful book he read on the subject, but he didn't understand everything completely.

    By the way, Groovy is compiled. Sort of, in a dynamic way. And it's bloody annoying. When I run Grails and change class A, groovy needs some time to recompile, and then complains it can't cast class A to class A. I never had that problem with Ruby (although Ruby has its own share of reliability issues).

  • by mcvos ( 645701 ) on Thursday July 08, 2010 @06:27PM (#32845384)

    It's a shame. Superficially it looks a lot like Java with all the good stuff from Ruby in it, but Groovy is really hampered by its compatibility with Java.

    0 evaluating as false is really incredibly annoying, and a major cause of bugs.

  • Re:Do we need this? (Score:3, Interesting)

    by DuckDodgers ( 541817 ) <keeper_of_the_wo ... inus threevowels> on Thursday July 08, 2010 @11:10PM (#32847282)
    I don't use Groovy, but I understand it was originally built to satisfy your second requirement. It fits in the existing setup of Java servlets and the JVM, but is a scripting language. Using Groovy to cut out the need to recompile and reload your Java web application after most changes often speeds development time dramatically.

    Over the past few years a few other methods for loading changes to your Java web app without reloading the entire application have appeared, and I don't see a need for Groovy. But I understand why it was created, and I think its existence probably helped other people to brainstorm cleaner ways to have their applications reload changes more quickly.

He has not acquired a fortune; the fortune has acquired him. -- Bion

Working...