Skip to content

Groovy Tips and Hints

Emanuelle Garduno edited this page May 24, 2013 · 1 revision

Groovy is a dynamic language, with some powerful programming features not available in Java. This style guide is tries to formalize the coding style for this project. It takes it bases from the Groovy Style Guide

No semicolons

Semicolons are optional in Groovy, omit them.

print 'Hello World'

No return keyword

Return keyword is optional. Don't use return keyword

String toString() { 'a server' }

Def and type

Don't use both def and type.

Bad

class Person {
    def String name

    /** Default Constructor */
    def Person() {
    }
}

Good

class Person {
    String name
    
    /** Default Constructor */
    Person() {
    }
}

def is not void

From Groovy website, Scoping+and the Semantics of "def"

"def" is a replacement for a type name. In variable definitions it is used to indicate that you don't care about the type

"def" can also replace "void" as the return type in a method definiton.

While it can also replace void. For the following code.

def hello(String msg) {
   println "hello $msg"
}

The compiler will generate something like this.

public java.lang.Object hello(java.lang.Object msg) {
    return this.print("$msg")
}

While with the following code.

void hello(String msg) {
   println "hello $msg"
}
public void hello(java.lang.Object msg) {
    this.print("$msg")
}

For public methods always specify a return type. Private methods can use def. See Classes for more information.

Public by default

By default, Groovy considers classes and methods public. For package scope use @PackageScope annotation.

class PersonService {
    @PackageScope Repository repository
}

Omit Parenthesis

Always omit parenthesis in top-level expressions.

print 'Hello World'
method a, b

Closures

Omit parenthesis when closures are the last parameter.

Bad

coll.each( { println it } )
coll.each(){ println it }

Good

coll.each { println it }

Closures it

Always use implicit argument it for closures with one parameter.

Bad

coll.each { item -> println item }

Good

coll.each { println it }

Classes

Always specify a type to all public methods and properties.

Bad

class Person {
    def name

    def sayHello() {
        "Hello my name is $name"
    }

    def toString() {
        "$name"
    }
}

Good

class Person {
    String name

    Person(String name) {
        this.name = name
    }

    String sayHello() {
        "Hello my name is $name"
    }

    String toString() {
        "$name"
    }
}

For protected and private modifiers don't use def.

Bad

protected def add(x, y) {
    x + y
}

Good

protected add(x, y) {
    x + y
}

protected int multiply(int x, int y) {
    x * y
}

Prefer def when the type can be inferred or for local variables. Always think about others reading your code, types could make more noise than to help. Or else, help to understand better.

// Person
def person = new Person()
// int
def sum = 100 + 200
// boolean
def empty = coll.isEmpty()
// etc

Always provide constructors for public classes.

class Person {
    String name

    Person(String name) {
        this.name = name
    }
}

String vs Groovy String

Use ' for Strings. Use " for GStrings.

String sayHi() { 'hello' }
String sayHi(String name) { "hello $name" }

String interpolation

For variable substitution don't use curly braces.

String sayHi(String name) { "hello $name" }

For expressions use curly braces.

String toString() { "the sum of x + y is ${x + y}" }
String toString() { "my name is ${fullName()}" }