Groovy, an alternative to Java

  • #Frameworks & development
  • #Technical & Entreprise Architecture

Published on Updated on

Amongst the many programming languages, some rely on Java and its JVM. For the past months, the use of some of them is in full boom. Recently, we’ve been hearing a lot about Scala (language which combines object-oriented and functional programming) and Groovy is the most familiar and widespread other language of the JVM.

png_medium-3ddfe

History

Groovy was initially developed in 2003 by James Strachan (who will later deny this) and Guillaume Laforge took over as the project manager. Far from just being a language developed by fans, with an unclear future, Groovy was standardized by the JCP in the JSR-241 and is nowadays under the umbrella of VMWare after the company purchasedSpringsource.

Unlike Scala, Groovy remains under a purely object-orientated paradigm. It is also a script language. However, it also brings its own innovations which could well get you to give Java up.

Groovy’s leitmotiv is simplifying the life of developers: “it’s a language created by developers, made for developers”. Many things have been installed to break away from Java’s restrictiveness when you wish to develop rapidly and efficiently: dynamically generated accessors, adding of closures, operator overload…

Features

But before we go through all the language assets (and there sure is a lot of them), we need to explain its foundations.

Groovy is above all a dynamic language, and when I say dynamic I don’t only mean a simple declaration of variables in a script language such as PHP. No, classes and objects can be extended in Groovy, a bit like in Objective-C. In other words, after the compilation and the execution of your code, you can still extend your objects’ functionalities by adding a new property, but also your classes to add new methods. Groovy still uses this dynamics to extend some classes of JDK.

It is possible to write the following code in Groovy:

List elements = [1, 2, 3]
elements += 4

This is valid thanks to the operator overload that Groovy adds on to the class

java.util.Collection.

Another of Groovy’s advantages is the possibility to create closures. A closure is a function, independent from the call scope, that you can use like a variable and therefore add it on to a method or another closure. e.g. : closures allow you to work more easily on some base types that Groovy overloads, such as lists. You can therefore pair each element of a list with a treatment defined by a closure and obtain a list containing results. But instead of a definition, you’re better off with an example:

<code class="java">List serie = [1, 2, 3]
List serieInc = serie.collect { entier ->
entier+1
}
assert serieInc == [2, 3, 4]

In this example, we have added 1 to each element of the serie list by creating a new listserieInc.

And unlike Scala, in order to slowly acclimate to the syntax and to the possibilities for Groovy, you may carry on writing pure Java code. The Java code is entirely understandable by the Groovy compiler.

Ecosystem

Groovy’s greatest strength is certainly its ecosystem. Many projects have used Groovy as the main programming language:

  • Grails: RAD framework for web applications. It is the mirror image of RubyOnRailsand of Symfony for Groovy. It is probably the most renoun Groovy project. It is one of the solutions given by VMWare/SpringSource. And it also relies on Spring and Hibernate as a guarantee of quality.
  • Griffon: It is the equivalent of Grails for Swing applications.
  • Gradle: If Maven puts you off, you can try this tool to automate and manage your projects. Groovy and Grails will move on to this tool for the coming versions.
  • Spock: It’s a test framework based on Groovy’s capacity to create DSLs (Domain Specific Languages) to generate its binary files.

Apart from the projects that are based on the possibility this language has to offer to create new tools, others give you the possibility to use it as an alternative to Java. This is the case for Liferay, which allows you to create your portlets entirely in Groovy (groovy.jar and which is included in the core libraries).

Conclusion

Thanks to, amongst other things, alternative languages like Groovy, the Java world is catching up on the backlog with lighter technologies such as PHP or Ruby. Java is finally free from encumbrances which were complexity and verbosity and which were major brakes for:

  • web development which requires great responsiveness and agility
  • web developers on their productivity

The language is mature and so are the frameworks! It is now time to think Java development differently and to stop using tools from the past.