Dynamic languages like Groovy and Ruby are all about productivity. Not only do they eliminate the need to write tons of boilerplate code, they also contain all sorts of “syntactic sugar” – language features that let you accomplish a tremendous amount of work with very little code.
Most of these features – closures, ultra-convenient collection-oriented operators like the spread-dot (*.), etc., are well-documented. Some, however, are a bit more arcane, and not realizing they exist can actually cost you a lot of time if you stumble into them inadvertently.
The feature I have in mind at the moment is a very convenient but somewhat surprising bit of collection behavior in Groovy. I’ve been working with the language for quite a while, and have dutifully pored over books and language reference documents to become familiar with all of the groovy goodness at my disposal, but somehow I missed this one until someone on my project team found it – quite by accident.
Here’s the magic find. In Groovy, if you have a list of objects, you can access, on the list itself, properties that belong to objects within the list. The result, somewhat intuitively (but somehow quizzical at the same time), is a list of values of that property across all the members in the list. Here’s an example to illustrate what I mean – just copy this and run it as a Groovy script.
class Player { def name def teams public String toString() { "${name} - played for ${teams}" } } def players = [ new Player(name: "Greg Maddux", teams: ["Cubs", "Braves"]), new Player(name: "Ron Santo", teams: ["Cubs", "White Sox"]), new Player(name: "Billy Williams", teams: ["Cubs", "Athletics"]), new Player(name: "Bruce Sutter", teams: ["Cubs", "Cardinals"]), new Player(name: "Lou Brock", teams: ["Cubs", "Cardinals"]), new Player(name: "Gary Gaetti", teams: ["Twins", "Cubs"]), new Player(name: "Ron Cey", teams: ["Dodgers", "Cubs"]), new Player(name: "Ryne Sandberg", teams: ["Cubs"]), new Player(name: "Mark Grace", teams: ["Cubs", "Diamondbacks"]), new Player(name: "Goose Gossage", teams: ["Athletics", "Yankees", "Cubs"]), new Player(name: "Sammy Sosa", teams: ["Rangers", "White Sox", "Cubs"]), new Player(name: "Larry Bowa", teams: ["Phillies", "Cubs"]), new Player(name: "Rick Monday", teams: ["Dodgers", "Cubs"]), new Player(name: "Dave Kingman", teams: ["Rangers", "Mets", "Cubs"]), new Player(name: "Joe Girardi", teams: ["Cubs", "Cardinals", "Yankees"]) ] def playerNames = players.name println "Here, amazingly enough, is a list containing the names of all those players:\n $(playerNames)"
Once you know this behavior exists, it’s pretty easy to find situations where it’s an insanely convenient feature. If you don’t know it exists, you can get some pretty confusing results. In our case, we were running a GORM query to retrieve a domain object using an alternate ID (expecting a single result), and incorrectly used the list()
method (we should have used get()
, which returns the domain object, whereas list()
always returns a list).
In our code, after querying for the domain object, we accessed properties of the object, and were puzzled when instead of getting Strings and Integers and Dates, we were getting single-member lists of Strings and Integers and Dates. Each list contained
the value we expected, inexplicably embedded in a List instance. It took a little head-scratching before we figured out that the root cause was that our query was returning a list containing our domain object, and our code that accessed the properties, which would have failed miserably in Java if attempting to treat a List as though it were a domain object, was happily drilling through the wall of that list and getting at its contents.
Syntactic sugar? Definitely. Violation of the principle of least surprise? Maybe. Will I use it, now that I know it’s a tool at my disposal? Absolutely.
Stay Groovy, my friends…