SpringOne Americas

Private Events

Blogs

View all Blogs >>
  • Andrew Glover

    Co-author of "Continuous Integration"

    more»

  • Brian Goetz

    Author of Java Concurrency in Practice

    more»

  • Scott Leberknight

    Chief Architect at Near Infinity

    In late 2006 Neal Ford wrote about Polyglot Programming and predicted more»

  • Stuart Halloway

    CEO of Relevance

    This article is part of a series describing a port of the samples from Practical Common Lisp more»

  • Michael Nygard

    Agile technology leader and dynamicist

    In my series on dirty data, I made the argument that sometimes incomplete, inaccurate, or inconsistent data was OK. In fact, not only is it... more»

  • Jared Richardson

    Agile coach and co-author of Ship It

    Last week in Washington, D.C. I was teaching a test automation class. The class ended up being composed of more»

  • Kenneth Kousen

    President of Kousen IT, Inc.

    In this entry in my “Making Swing Groovy” series, I want to talk about threading issues. Specifically, more»

  • Ryan Shriver

    Business and Technology Consulting

    more»

  • Alex Miller

    Sr. Engineer with Terracotta Inc.

    Stanley Ho announced today on the JSR 277 mailing more»

  • Mike Levin

    Software Developer specializing in Web2.0 websites

    more»

  • Richard Monson-Haefel

    VP of Developer Relations, Curl Inc.

    more»

  • Matt Raible

    Creator of AppFuse and author of Spring Live

    more»

  • Graeme Rocher

    Project Lead of the Grails Project & CTO of G2One

    The main portal for Sky television has relaunched written in Grails. Sky, also know more»

  • Jason Rudolph

    Author of Getting Started with Grails

    While working on the more»

  • Neal Ford

    Application Architect at ThoughtWorks, Inc.

    Way back in 1968, Edsger Dijkstra almost caused a riot at the ACM conference. His audacious crime? " more»

  • David Bock

    Principal Consultant, CodeSherpas Inc.

    I was driving to work this morning listening to all the doom and gloom on the radio, thinking to myself, "You know, I have survived a major... more»

  • Brian Pontarelli

    Brian Pontarelli - founder of Inversoft

    I might be smokin’ crack, but I think that todays (September 30th, 2008) Java update from Apple finally fixed the command-tab issue. I... more»

  • Pramod Sadalage

    Co-author of "Refactoring Databases:Evolutionary Database Development"

    Recently when our test databases where upgraded new version of Oracle, we started noticing that the order in which some drop down lists were... more»

  • Craig Walls

    Author of Spring in Action

    As you've no doubt heard and as I've already commented on elsewhere on this blog, SpringSource has enacted a new maintenance policy around... more»

  • Erik Doernenburg

    Principal Consultant @ Thoughtworks

    One of my favourite tools to render graphs is Gra phViz Dot and in an more»

  • Venkat Subramaniam

    Founder of Agile Developer, Inc.

    I wrote a four part article for Java World on creating DSLs in Java and Groovy. For your convenience, I decided to list the links to those... more»

  • Jason Harwig

    Senior Software Engineer at Near Infinity

    The most popular entry I've written at Near Infinity has been the more»

  • Nathaniel Schutta

    Author, speaker, software engineer focused on user interface design.

    I spent my formative years on a small hobby farm. In addition to witnessing first hand the whole circle of life thing, I learned just how... more»

  • Ted Neward

    Enterprise, Virtual Machine and Language Wonk

    One of the more interesting logistical problems faced by the people who run the Microsoft Conference Center is that several events are often... more»

  • Pratik Patel

    Enterprise Architect

     Every now and then I read challenges to Frederick Brooks' wisdom. Mr. Brooks is the au more»

  • John Heintz

    Principal Consultant with New Aspects of Software

    In a recent discussion interview questions came up, here's my favorite one.To set some context this question is designed to gauge the abst more»

  • Mark Johnson

    Director of Consulting at CGI

    At the Columbus NFJS show held on July 25-27th during one of the BOF sessions Dave Bock, Scott Davis and I discussed unit tests vs functional... more»

  • Joseph Nusairat

    Author of Beginning JBoss Seam & Co-Author of Beginning Groovy & Grails

    Well i am assuming Apress has the most random site in the world at times.But today only they have our recent book, Beginning Groovy & Grai more»

  • Keith Donald

    Lead of Spring Web and Creator of Spring Web Flow

    I am pleased to announce that Developing Rich Web Applications with Spring, a three-day bootcamp lead by SpringSource engineers on web... more»

  • Vladimir Vivien

    Software Engineer / Consultant

    Judging from the list of features that will be included in NetBeans 6.5, more»

  • Kirk Knoernschild

    Software Developer & Mentor

    I’ve published a summary of the OSGi survey results on the APS blog more»

  • Pete Behrens

    Organizational Agility Coach

    Marti nig & Associates Methods & Tools group recentl more»

  • Brian Sam-Bodden

    Java author, Ruby geek and Open Source Advocate

    In this installment we are going to build the Dashboard page of the Tempo application. T more»

  • Mark Fisher

    Spring Integration Lead

    In my recent post, I had mentio more»

  • Ron Bodkin

    Chief Software Architect, Quantcast

    I'm looking forward to speaking at The Rich Web Experience conference in San Jose next month. The event runs from September 7th through 9th.... more»

  • Mark Goodwin

    Web Application Security Specialist

    We've already looked at one of the two big problems posed by anti DNS pinning on Java applets; because there's rebinding on the applet and... more»

  • Scott Davis

    Author of "Groovy Recipes" & TDD Expert

    Every time I see a live show at the Denver Botanic more»

  • Romain Guy

    Java User Interface expert.

    more»

  • Ramnivas Laddad

    Author of AspectJ in Action, Principal at SpringSource

    InfoQ.com has published my AOP myths and realities talk recorded at a No Fluff Just Stuff conference. InfoQ.com founded by Floyd Marine more»

  • David Geary

    Author of Graphic Java and co-author of Core JSF

    The 2006 NFJS tour kicked off t more»

  • Jeff Brown

    G2One Vice President of Professional Services - Groovy and Grails Developer

    <a href="http://groovy.codehaus.org/"& gt;Groovy</a> 1.0 RC2 was released today. If all goes well over the next few days the 1.0... more»

  • Howard Lewis Ship

    Creator of Tapestry and HiveMind

    <p> Just got back from Europe on Friday, and have been recovering from a nasty cold and jet lag. This week I'm prepping for another... more»

  • Kito Mann

    Editor-in-chief of JSF Central and the author of JSF in Action

    Our current schedule for JSF 2.0 has us handing off the spec artifacts to the JCP on 15 December 2008. That's 62 business days from today. We... more»

  • Jason Hunter

    Author of Java Servlet Programming

    I just posted the JDOM 1.1 release for download. This release includes about 20 improvements and bug fixes. more»

Refactoring Ugly Groovy Code

Posted by: Venkat Subramaniam on 05/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.
  • Currently 4.0/5
  • 1
  • 2
  • 3
  • 4
  • 5
4.0 rating out of 3 votes


About Venkat Subramaniam

Dr. Venkat Subramaniam, founder of Agile Developer, Inc., has trained and mentored thousands of software developers in the US, Canada, Europe, and Asia. Venkat helps his clients effectively apply and succeed with agile practices on their software projects, and speaks frequently at international conferences and user groups. He is author of ".NET Gotchas" (O'Reilly), coauthor of 2007 Jolt Productivity Award winning "Practices of an Agile Developer" (Pragmatic Bookshelf), and author of "Programming Groovy: Dynamic Productivity for the Java Developer" (Pragmatic Bookshelf).