Friday, August 27, 2010

Groovy: Superset of Java

Groovy is a bit like scripting version of Java. Groovy is a superset of Java. It is no way means to replace java, Long time Java
programmers feel at home when building agile applications backed by big architectures. One nice thing about Groovy is that it
reduces the amount of code needed to do common tasks such as parsing XML files and accessing databases. Groovy on Grails is
as the name suggests similar to the Rails framework. For people who don't want to bother with Java's verbosity all the time.

The big advantage is speed, brevity and readability. For instance here is some java code which implements a bag:


public class Bag {

private Map counter = new HashMap();
public void add(Object obj) {
add(obj, 1);

}

public void add(Object obj, Integer count) {
Integer value = counter.get(obj);
if (value == null)
counter.put(obj, count);
else
counter.put(obj, value+count);
}

public Integer getCount(Object obj) {
return counter.containsKey(obj) ? counter.get(obj) : 0;
}

}

And here is the same implementation in Groovy:

class Bag {
def counter = [:]
def add(term, count=1) {
if (counter[term] == null)
counter[term] = count
else
counter[term]+=count
}

def getCount(term) {
return counter[term] != null ? counter[term] : 0;
}

}

So you can see how intuitive the Groovy code is, you don't need to define anything regarding types or default parameter values.

That said, Groovy is essentially Java, just wrapped around a different compiler, class loader and with many added methods and
members. For that reason it is unlikely that it will ever replace Java, not only that but because it IS Java it is bound by the same
limitations and rules and doesn't provide any functionality which is really unique.


Semicolons are optional. Use them if you like (though you must use them to put several statements on one line).
The return keyword is optional.
You can use the this keyword inside static methods (which refers to this class).
Methods and classes are public by default.
Protected in Groovy has the same meaning as protected in Java, i.e. you can have friends in the same package and derived classes can also see protected members.
Inner classes are not supported at the moment. In most cases you can use closures instead.
The throws clause in a method signature is not checked by the Groovy compiler, because there is no difference between checked and unchecked exceptions.
You will not get compile errors like you would in Java for using undefined members or passing arguments of the
wrong type. See Runtime vs Compile time, Static vs Dynamic.
Java can always be made faster, more memory efficient and more robust than Groovy if you are willing to put in the time and
effort.

So where would you use Groovy?

I think typically in places where readability, flexibility and speed of development is more important than performance,
extensibility and a large user base. For instance in deployment you can use the Groovy equivalent of Ant called Gant which
allows better flexibility and better readability. Other places might be in a web application framework such as Grails.

You can use Groovy all the time for utilities, both on the command line and on the web. Often, the utilities use jars/class files
from my project, since it is all on the JVM.

For web utils, take a look at Groovlets. You can come up to speed with Groovlets in a couple of hours. A groovlet is simply a
servlet distilled down to its essence.

Also, it does have integration for other open source framework like Spring framework. Groovy is great for writing unit tests for existing Java code and could save lots of development time...

I hope this brief introduction will help in understanding as what's groovy and how it can help in your project.

No comments:

Post a Comment