Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Introducing Groovy to a Java team

 
Thomas Queste
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Ken,

I discussed several years ago with Guillaume Laforge about how to introduce Groovy to a Java team.
I would like your take on it.
How would you do, step by step ? Where one should start ?
My question is about how to convince people, how to demonstrate the ROTI (Return of Time Invested) ?

For me, I think that starting by the test, let's say with using Spock, would make a solid introduction of Groovy.
Then, one could use Groovy SQL to replace custom jdbc statement (via JdbcTemplate or other).

Thanks,
Tom
 
Kenneth A. Kousen
gunslinger & author
Ranch Hand
Posts: 100
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Starting with Spock is a great approach, but most Java developers are won over by seeing how much simpler Groovy code is than the corresponding Java. For example, the native collections are dramatically simpler than using the Java classes directly:


You can sort strings with:

Even little things are simpler, like the safe de-reference operator:

The "?." operator invokes getBoss() on the Department reference only if it's not null, and then invokes getName() on the result only if getBoss() returns something not null. In other words, most of your null checks go away, too. That tiny bit of syntax wins over more Java developers than I ever would have believed.

As you mentioned, the groovy.sql.Sql class is also very simple, and eliminates all those try/catch blocks and closes your connections automatically. You can show them how many helpful methods the Groovy JDK adds, like finally having a reverse() method in String, or findAll in Collection. You can show how closures eliminate any anonymous inner classes you might have.

Finally, did you ever try to make an immutable object in Java? It's not so easy. You have to make the class final, remove all the set methods, wrap collections in immutable collections, and so on. Or you can just write:

The resulting class wraps BigDecimals (and you can still use operators like +, -, and * with them), includes getter methods for x and y as well as a constructor Point(x,y) for setting them, has equals, hashCode, and toString methods, and throws a ReadOnlyPropertyException if you try to modify either property. That's a huge simplification.

Ken
 
Thomas Queste
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, thanks for the point.
I didn't think of Groovy handling collections, but it is a strong point.

Thanks !
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic