📢 Support OSI’s mission! 📢 Donate and become a full member today to make a difference.

Enhance code readability and maintainability with Apache Groovy’s named parameters

Java Records offer a modern and concise approach to define simple data objects in Java, improving code readability, maintainability and reducing the chance of bugs. There’s been a fair bit of interest this feature introduced in Java 14. Two characteristics of Records that stand out for me are automatic creation of getters and setters and named parameters in constructors.

Apache Groovy has provided these types of low ceremony and readability features since its inception. Moreover, Groovy provides them all across the language, not just in the limited case of something like Java Records.

In the last article in this series, you saw how Apache Groovy streamlines details related to getters and setters in all classes.

Groovy also provides named parameters, similar to Python’s keyword arguments, that give an effect similar to initializing Records in Java. However, in Groovy, named parameters can be used in any class definition, not just in a limited situation such as Java Records (If you haven’t installed Groovy yet, please read the intro to this series.)

Working from the definition of the Circle class from the previous article, you can use named parameters as follows:

 1  class Circle {
 2    double x, y, r
 3    public double getA() {
 4      return Math.PI * this.r * this.r;
 5    }
 6    public double getC() {
 7      return 2.0d * Math.PI * this.r;
 8    }
 9    public void setA(double a) {
10      this.r = Math.sqrt(a / Math.PI);
11    }
12    public void setC(double c) {
13      this.r = c / 2.0d / Math.PI;
14    }
15  }
16  def c = new Circle(x: 2.75d, y: 3.98d, r: 15d)
17  println "c.x ${c.x} c.y ${c.y} c.r ${c.r} c.a ${c.a} c.c ${c.c}"

You don’t need a constructor because the default provides the functionality you need. If you do provide a constructor, it either needs to be a no-argument constructor or take a Map as the first argument; and the Map will expect entries corresponding to the fields declared in the class.

You still need to define the getters and setters for area and circumference as these don’t relate to declared properties of the Circle class and you still use them with dot notation (for example in line 17.)

Note in line 16 where you use the named parameters to the Circle constructor. They’re particularly nice in this application since the program using the Circle class no longer needs to worry about the order of arguments, and their use in conjunction with well-named parameters adds a lot more clarity when compared to a list of three constant values.

The specific notation used ends up creating a Map instance [x: 2.75d, y: 3.98d, r: 15d] behind the scenes that is passed into the constructor.

The Groovy reference documentation provides more useful detail on named parameters.


Named parameters are a nice way to avoid having to remember the order of parameters to any method invocation, not only initializing record constructors.  Moreover, named parameters can be used in conjunction with default values declared for parameters, so that the programmer only needs to specify values different from the default. 

In my opinion, the real value of this capability is to reduce the amount of code that needs to be read and understood; of course, it also implies that the programmer needs to know the default values and accept them!

This post is part of a series on Apache Groovy, stay tuned for more.

Photo by travelnow.or.crylater on Unsplash


Support us

OpenSource.net is supported by the Open Source Initiative, the non-profit organization that defines Open Source.