Why Groovy?

Why should a team already used to Java pay attention to Groovy? Which problems does it solve for you? On this post I hope to show you some of these reasons, at least the ones that matter most to me.

First the pseudo reason: runs on the JVM

We all know the JVM is great but this can’t be considered THE main reason why you should pay attention to Groovy. After all, Java also runs on the JVM. In my opinion this must be seen as a great advantage, but not the main one.

It’s great to know that all your code will be executed by your Groovy code without problems and that you’ll have access to all the Java ecosystem. But it only helps the adoption of the language: does not fully justify it.

(but I must admit that my first contact with Groovy was strongly influenced by this aspect)

Can be executed as a script using all your legacy Java code and your favorite libraries

This is a good reason. Groovy is an awesome language to write maintanence code. I love to write Groovy scripts to execute small maintanence tasks on my systems, reusing all my business logic without having to directly access my database.

I also like to write infrastructure maintanence scripts on my servers, so that I can take advantage of great Java libraries like Apache Commons. For people like me (which are not system managers)  who have not mastered bash yet but knows Java this makes Groovy a great option.

The script format is also very inviting for beginners. There are those cases in which you want your program to do simple things like deal with a bunch of files on a folder. What code you think is easier to understand on these cases? The Java version below?

[code language="java"]

public class ReadFolders {
public static void main(String args[]) {
File folder = new File("/somewhere");
for (File file : folder) {
   // do something
   }
  }
}

[/code]

Or the Groovy version?

[code]

File folder = new File("/somewhere");
for (file in folder) {
 // do something
}

[/code]

Why wrap my kickstart code in a class when it’s simple?

Have some features that are not in Java yet.

If you already know Groovy maybe you feel the same thing: that feeling that Java is becoming more like Groovy in every new iteration. I must admit that Java 8  does not seems like something new to me because I actually have access to all those features since 2007 when learned Groovy. Here is a small list of Groovy features that I really miss in Java:

  • Closures: I know that in Java 8 closures will be a main feature, but Groovy have it since it’s first version.
  • Builders: Groovy allows you to deal with tree like data structures in a really nice and simple way. It’s one of those features that you will only understand when dealing with real problems. So I recomend you to learn more about Swing Builder and Markup Builder.
  • The way we deal with numbers it’s also great. Dealing with BigDecimal in Java is a struggle (I know better days await for Java 8, but today I still have to use 7). Just compare: a.multiply(b) or a * b? If you must deal with BigDecimal numbers, Java never was a good language to start with.
  • GStrings: Groovy have it’s own version of strings. Remember all that concatenation you have to do with Java? In Groovy all I have to do is interpolate it.
  • The fact of Groovy also being a dynamic language also helps a lot. Have the ability to change the behavior of your code at runtime without the need for design patterns often difficult to implement is a real time saver in several cases, like writing mock objects for your tests as an example.
  • Groovy also have special constructors in it’s syntax to deal with the Collections API. Much better than the way we are used to work with Java.
    Instead of
    List<String> words = new ArrayList<String>();
    just use
    def words = []
    And the support for hashes also make Groovy an excelent option to write mock objects. Great for testing!

A nice gateway to the Java platform

At least in Brazil (I’m a brazilian) I observe a huge mass of programmers from languages like Visual Basic, Delphi, PHP, Power Builder and many others that want to code for the JVM but think Java is a difficult to learn programming language. It seems strange to many, but it’s a reality that I deal with every day.

Groovy syntax is very attractive to this crowd. Having semicolons and parentheses as an optional aspect may seem like bullshit to many of you but to a really big audience this is a good reason to try the language. It’s closer to what they are used to work, and as time goes by, people start to learn more and more about the Java ecosystem in a much less traumatic way.

Groovy Console

Not exactly a feature of the language, but it’s a nice tool that I use a lot. Groovy comes with a full REPL interface that, in my opinion, is the best place to experimente the features of the language, learn APIs and even use it to execute some scripts once in a while.

Write a DSL in Groovy is easy (really easy)

I know this is not part of everyday life for most people, but since it is part of my, I feel compelled to mention it. Optional parentheses and semicolons, closures, AST and many other features make writing a DSL in Groovy really much easier than in Java.

There’s a great book just about this called Groovy for Domain Specific Languages which is really great.

Grails

Most people I know who learned Groovy learned it because of Grails, which is the “killer application” of this language. Grails simply saves the Java EE from boredom. I wrote about it four years ago and still think this way. Grails is web development for the Java platform as it always should have been. Simply as that. And as the founder of Grails Brasil, one of the largest users group of the world, I really know what I’m talking about, because I already seen many amazing usages of this framework.

And you?

Now I whould like to know what you think about Groovy? Have you already used it? Did you loved or hate it?

10 thoughts on “Why Groovy?

  1. We have been using Groovy/Grails since 2009. I love working in Groovy, it’s tons better than Java. There are a couple gotchas to watch out for – things like:
    – Groovy Truth can burn you when you need to distinguish a null value from a zero value, just have to remember to do that explicitly.
    – Ranges actually create every item inside the range, so they are really slow for huge ranges
    – You sometimes have to be aware of GStrings vs Strings

    There’s probably others, just a few off the top of my head.

    But the code reduction is fantastic for maintenance, the string manipulation as you mentioned is really great, as are closures and what they allow you to do with collections. Also say goodbye to setters/getters ;-)

    [Reply]

  2. Hey, just a heads up. This line: “Most people I know who learned Groovy learned it because of Groovy, which is the “killer application” of this language.” should say “Most people I know who learned Groovy learned it because of GRAILS, which is…”

    Great article!

    [Reply]

    admin Reply:

    Ops! Thanks! :)
    Just fixed it!

    [Reply]

  3. I’m not a Groovy expert but I find Scala a great competitor which has almost all the features you list out plus the Play Framework for web development. One thing I don’t like about Groovy is that it’s dynamically typed. This could be a good thing for small scripts but not for big apps maintained by many dev.

    [Reply]

  4. I use groovy and grails every day for work and love it. I’ve even started diving into AST transforms, but for those unlike most of groovy, you have to think fourth dimensionally. I’ve also posted some free groovy tutorials on my youtube chanel here:
    http://www.youtube.com/channel/UCHRADKRXZkPB6QVee0Q3GSQ

    [Reply]

    admin Reply:

    Great to know!
    Good to see the community producing material for the beginners!

    [Reply]

  5. I want to build transform part of ETL(just T), I find groovy is a good option for it. Why i choose groovy because it allow me to write custom methods and business mapping in configuration file only. I am in initial analysis phase only so just want to get confirmation from you all if groovy is a way to go or we should i be exploring other such as Scala?
    Is groovy slow? Is Groovy still hot language in the market?

    [Reply]

    admin Reply:

    Yes, it still is a hot language, and is not so slow as it was when introduced. :)

    [Reply]

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>