When to write an explicit return statement in Groovy?

Posted by Roland Schneider on Programmers See other posts from Programmers or by Roland Schneider
Published on 2012-06-11T17:38:51Z Indexed on 2012/06/11 22:46 UTC
Read the original article Hit count: 376

Filed under:
|

At the moment I am working on a Groovy/Grails project (which I'm quite new in) and I wonder whether it is good practice to omit the return keyword in Groovy methods. As far as I know you have to explicitly insert the keyword i.e. for guard clauses, so should one use it also everywhere else? In my opinion the additional return keyword increases readability. Or is it something you just have to get used to? What is your experience with that topic?

Some examples:

def foo(boolean bar) {
    // Not consistent
    if (bar) {
        return positiveBar()
    }
    negativeBar()
}

def foo2() {
    // Special Grails example
    def entitiy = new Entity(foo: 'Foo', bar: 'Bar')
    entity.save flush: true
    // Looks strange to me this way
    entity
}

© Programmers or respective owner

Related posts about coding-style

Related posts about groovy