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?
A closure in Groovy is an open, anonymous, block of code that can take arguments, return a value and be assigned to a variable. A closure may reference variables declared in its surrounding scope.
Behaviour of == In Java == means equality of primitive types or identity for objects. In Groovy == translates to a. compareTo(b)==0, if they are Comparable, and a. equals(b) otherwise.
What is the standard coding convention for Groovy? In Java the method naming is done using camel case notation. In Ruby on which Groovy is based, underscores are preferred.
Generally speaking, Groovy will be slower. You can avoid that by switching to Groovy++ which offers most of the features of Groovy, but can be statically compiled and has performance comparable to Java.
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 :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With