Refactoring Ugly Groovy Code - No Fluff Just Stuff

Refactoring Ugly Groovy Code

Posted by: Venkat Subramaniam on May 12, 2008

A few weeks ago I was writing a method in Groovy that needed to return three different results, two strings and one array. As I was writing it, I said to myself, "this code is ugly, I wish I could return multiple values from methods in Groovy."

Here is an example similar to the method I had written:

def details(full_name, address, contact_numbers)
{
  // ignoring stuff to process data
  full_name.append('Venkat Subramaniam')
  address.append("Venkat's address")
  contact_numbers << 'phone_number_1'
  contact_numbers << 'phone_number_2'
  contact_numbers << 'phone_number_3'
}

To use the above method I had to write something like:

def full_name = new StringBuilder()
def address = new StringBuilder()
def contact_numbers = []

details(full_name, address, contact_numbers)

Today, I got around to refactoring my code, thanks to the Groovy 1.6 (beta 1) facility to return multiple results from a method. The above example,
refactored looks like below:

def details()
{
  // ignoring stuff to process data
  ['Venkat Subramaniam', "Venkat's address", ['phone_number_1', 'phone_number_2', 'phone_number_3']]
}

And I can call it as follows:

def full_name, address, contact_numbers

[full_name, address, contact_numbers] = details()

Simple, less noisy, elegant, and my refactored code does not look (that) ugly anymore.

In Groovy 1.6 (beta1), if a method returns an ArrayList, you can assign it to an ArrayList made up of variables you'd like to assign the values to.

If there are fewer elements on the lhs, the extra values in the result are ignored. If there are more elements, the extra elements in the lhs are assigned null.

I think this is nice step in the right direction. I hope there is a way to capture the extra elements on the rhs into an array.
Venkat Subramaniam

About Venkat Subramaniam

Dr. Venkat Subramaniam is an award-winning author, founder of Agile Developer, Inc., creator of agilelearner.com, and an instructional professor at the University of Houston.

He has trained and mentored thousands of software developers in the US, Canada, Europe, and Asia, and is a regularly-invited speaker at several international conferences. Venkat helps his clients effectively apply and succeed with sustainable agile practices on their software projects.

Venkat is a (co)author of multiple technical books, including the 2007 Jolt Productivity award winning book Practices of an Agile Developer. You can find a list of his books at agiledeveloper.com. You can reach him by email at venkats@agiledeveloper.com or on twitter at @venkat_s.

Why Attend the NFJS Tour?

  • » Cutting-Edge Technologies
  • » Agile Practices
  • » Peer Exchange

Current Topics:

  • Languages on the JVM: Scala, Groovy, Clojure
  • Enterprise Java
  • Core Java, Java 8
  • Agility
  • Testing: Geb, Spock, Easyb
  • REST
  • NoSQL: MongoDB, Cassandra
  • Hadoop
  • Spring 4
  • Cloud
  • Automation Tools: Gradle, Git, Jenkins, Sonar
  • HTML5, CSS3, AngularJS, jQuery, Usability
  • Mobile Apps - iPhone and Android
  • More...
Learn More »