Groovy is COOL

Of course Groovy is much more than Java, but for Java coders like myself, it can be seen as Java without its time-eating annoyances, the absolute top 3 of which, in my opinion, are:

1. Getters and setters are implicit

2. Lists, Maps and Regex are part of the syntax

3. Configuration is greatly simplified

Item 1 is self explanatory, I hope 🙂 For the remaining two, here are some examples:

Working with collections:

def list = [5, 6, 7, 8]
for (i in list) println i
def map = [name:"Gromit", likes:"cheese", id:1234]
for (i in map) println "map[${i.key}] is ${i.value}"

Pulling Maven dependencies inline with your code, without any other configuration anywhere:

@Grab(group='mysql', module='mysql-connector-java', version='5.1.18')
driver = new com.mysql.jdbc.Driver()
Properties p = new Properties()
conn = driver.connect('jdbc:mysql://localhost/groovy-test', p)
st = conn.createStatement()
for (i in 1..10) {
 st.executeUpdate("insert into test(name,value,version) values('name${i}','value for name${i}',0)")

Of course Groovy is a dynamically typed language, and as such has its downsides. Since the compiler can’t type-check assignments, you get much more run-time errors like:

No signature of method: java.util.ArrayList.get() is applicable for argument types: (java.lang.String)


Cannot cast object ‘x’ with class ‘java.lang.String’ to class ‘int’

Most of these errors are caught when you try out your code for the first time, though, and are easily fixable. It’s just a bit annoying at the beginning for someone used to getting these at compile time rather than at run time.

To me, Groovy is still a clear winner if you want to boost your coding productivity 🙂

Oh yeah, and the performance of Groovy is not significantly worse than that of Java (if you don’t use too many dynamic features), as I confirmed in this post.


0 Responses to “Groovy is COOL”

  1. Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: