Groovy Goodness: Turn Method Parameters Into Named Map Arguments With IntelliJ IDEA
A very useful feature in Groovy is the use of named arguments. Instead of a list of arguments for a method or constructor we can use a Map
argument. If the argument is the first in the list of arguments then Groovy allows use to use named arguments when we invoke the method or constructor. This means all key/value arguments are gathered together and assigned to the Map
argument. Inside our method or constructor we can then access the Map
argument and get the values for the keys. This leads to better readable code and that is very useful. IntelliJ IDEA has a Groovy intention to turn method parameters into a Map
parameter for named arguments with a few mouse clicks.
Suppose we have the following source code with a simple
method definition, 2 arguments, and the invocation of the method: