Getting Groovy

As we get more into our project at Orange Leap, we’re starting to see a lot of instances where a dynamic language would make our life easier. On all of our entity classes, we associate a map of custom fields. The custom fields are so that clients can customize the entities without us having to change the code each time. The custom field definitions go beyond basic key-value pairs and can include references to other custom fields. For example, spouse is a custom field that relates two entities and is bidirectional. If you open either entity, you’ll see the other in the spouse custom field.

A dynamic language would come into play in that we could access all the attributes of an entity in a consistent way, preferably through dot notation. Since we’re already running on top of Java 6, Groovy came to mind as the best candidate for a dynamic language.

I’ve never really played with Groovy before. I mostly played with Ruby and Rails, which really opened my eyes to the power of a good dynamic language. Some of the developers on my team wanted to get into Groovy while I was at Countrywide, but I kept us out of it for fear of ending up with an unmaintainable monster if one of the experts left. (this is actually the primary motivator in 99% of all enterprise technology decisions — the bus factor)

So this week I finally jumped in with both feet and I have to say I like what I found. Everything cool I liked about Ruby and .NET 3.5 was in Groovy. Metaprogramming, check. Mixins and Closures, check. Full access to all the JVM goodness, check. The recently released Groovy 1.6 even goes beyond Ruby and .NET 3.5.  For example, if you tag a class with the annotation @Singleton, it turns it automagically into a singleton.

@Singleton
class Friend {
    def name
}
f = Friend.instance
f.name = "Tim"
print "Hello, $f.name"

If you try and call new on the Friend class, you get an error that you can’t instantiate a Singleton. All this handled with a simple annotation. For an excellent overview of all the coolness in Groovy 1.6, check out this article on InfoQ.

So I’m going to keep working my way through Programming Groovy and am eager to start applying what I learn. And best of all, my favorite IDE has the industry’s best support for both Groovy and Grails.

Leave a Reply

Your email address will not be published. Required fields are marked *

*