When to write an explicit return statement in Groovy?
- by Roland Schneider
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
}