Why Groovy?


News: Why Groovy?

  1. Why Groovy? (4 messages)

    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 andMarkup 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!

    More on http://www.itexto.com.br/devkico/en/?p=39

    Threaded Messages (4)

  2. Good article! I find the infrastructure maintenance scripting very useful - especially since I can load groovy scripts into the server at runtime (no restart required).

  3. First, Scala also runs on the JVM.

    Second, if you consider some lines of code without a method / class script, then I think you can write scripts with Scala and run them on the command line without manually compile them. And of course, Scala can use your Java libraries too.

    Third, I believe Scala has a bunch of features not in Java yet.

    So, my point is that those reasons are not persuasive enough to choose Groovy.

  4. Hi Thai,

    You are partially correct. Yes: Scala have some features not yet in Java, it also can run as a script and runs on the JVM. The problem is that this article is not a comparison between Scala and Groovy, and when I asked why Groovy, Groovy was the subject, not scala. This does not mean that Scala is a better option: just that is not on the scope of the post.

    But for a comparison (I don't have much knowledge in Scala so I may say something wrong here) what I notice is this: when you think about bring to the JVM a crowd of programmers used to languages like Visual Basic (pre .net), COBOL, Clipper, Delphi and many others, Groovy syntax is more familiar and is also a great way to introduce this crowd to the Java EE platform. Scala syntax is great, but not as a gateway to this crowd in my opinion. Curiously you didn't metioned this point on your comment. :)

  5. vs[ Go to top ]

    Another point about bash vs Groovy (or any other like minded JVM language) is OS independence. There is a least one PaaS or IaaS that uses Groovy for scripting.


    One issue I have run into with using Groovy in this manner is that it sometimes ti has libraries dependencies that conflict with library dependencies for my code.