aspose file tools*
The moose likes Groovy and the fly likes Introducing Groovy to a Java team Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Languages » Groovy
Bookmark "Introducing Groovy to a Java team" Watch "Introducing Groovy to a Java team" New topic
Author

Introducing Groovy to a Java team

Thomas Queste
Greenhorn

Joined: Mar 08, 2006
Posts: 15
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

Joined: Sep 18, 2002
Posts: 89
    
    5
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


Kenneth A. Kousen, Ph.D. (assorted certs), President, Kousen IT, Inc.
Author of Making Java Groovy - http://www.kousenit.com
Thomas Queste
Greenhorn

Joined: Mar 08, 2006
Posts: 15
Yes, thanks for the point.
I didn't think of Groovy handling collections, but it is a strong point.

Thanks !
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Introducing Groovy to a Java team