Groovy and Grails: Ten Pleasant Surprises

After meeting Graeme Rocher at JavaOne, I decided it was time to give Groovy and Grails a second look. Here are the ten items that jumped out at me, as a Java and Ruby developer learning about Groovy and Grails:

  1. While new languages often introduce gratuitous syntactic differences, Groovy's differences are often appealing. I think I could come to like the syntax for case equality (===), hashes ([x:'y']), and open ranges (1.<10) better than their Ruby counterparts.
  2. I have often wished for the safe dereference operator (e.g. org.person?.name) when writing Ruby code. I am sure people will use this to paper over Demeter violations, but it is nevertheless worth having.
  3. Grails defaults to an in-memory HSQLDB database for development. This is even easier than Rails, setup-wise.
  4. Grails follows Rails' lead in providing a flash scope, and goes a step further in implementing a chain scope. I will be adding this feature to my Rails projects.
  5. Grails has a built-in dynamic scaffold, more like Streamlined than a static scaffold.
  6. Grails generalizes the notion of a builder library with reusable support for building arbitrary nested structures.
  7. Grails' model saving support (save/validate/discard) is more complex than Rails'. On first glance, I feared this was unnecessary complication, but actually it is quite nice. The common case is simple, and parallel to Rails. The other cases are there if you need them.
  8. As applications become more complex, Grails makes nice use of Spring's autowiring to inject supporting classes. (I find that excessive autowiring obfuscates relationships, but the Grails examples I have seen so far look just right.)
  9. The Groovy MetaClass (and its use in Grails) is very powerful. As Graeme points out, the MetaClass brings Groovy much closer to having the whole language there, all the time. Groovy still does not have open classes, but open invocation is a workable alternative for many use cases.
  10. The Definitive Guide to Grails is a very good book. If you are coming from a Java or Ruby background, this book is a perfect jump start for both Groovy and Grails.
Get In Touch