Search code examples
javarubygroovy

Groovy coding conventions?


What is the standard coding convention for Groovy?

In Java the method naming is done using camel case notation.

public void calculateTotal() {}

In Ruby on which Groovy is based, underscores are preferred.

def calculate_total()
end

Which of the above two styles would be considered more Groovyist?

Personally I'm more inclined to use the Ruby style. Is there a standard document/general consensus among the Groovy community as to what is preferable?


Solution

  • The first one. Groovy took a lot from Ruby, but its main focus is to be a dynamic and powerful language that plays well with Java. Therefore, most Java coding conventions and coding styles apply to Groovy as well.

    About naming conventions, while I also prefer the underscore_separated_words style of Ruby/Python and others, I think sticking to convention and being consistent with the surrounding environment is much more important than personal preference in these kind-of-superfluous matters (there are other things, like preferring a functional style over imperative constructs or vice versa, that I think are more interesting to discuss.

    So, always use camelCasing for Groovy methods. The Groovy documentation uses them; all the methods from the standard library use them.

    Finally, here's a nice Groovy coding conventions guide. It doesn't do any mention about naming conventions or formatting, but it introduces many of Groovy's nice syntactic additions and explains when it's preferred to use them