This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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).
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.