This forum is now a read-only archive. All commenting, posting, registration services have been turned off. Those needing community support and/or wanting to ask questions should refer to the Tag/Forum map, and to http://spring.io/questions for a curated list of stackoverflow tags that Pivotal engineers, and the community, monitor.

groovy/grails coding standards

Oct 28th, 2010, 05:07 AM

Hello,

Groovy programming language has very flexible syntax, a lot of stuff is optional: optional typing, semicolons, etc.
We are investigating Grails as a mainstream development platform for our company and see the need for strict coding standards to be in place for Grails based apps. We are afraid that without this some developers would be adding semicolons some not and after a while code base will become quite inconsistent and messy.
In PHP world ZendFramework has dedicated entire section in documentation to coding standards to avoid these inconsistencies. http://framework.zend.com/manual/en/...-standard.html
People that create new extensions for Zend stick to that and the entire code base has more less the same style.

Is there something similar for Grails framework, official recommendations/coding standards.
This would be valuable for people creating Grails plugins and apps.

There isn't no. The Sun Java style is a good starting point. Other things to consider:

* Whether to use explicit returns or not
* Whether to use explicit arguments in closures
* Whether to declare explicit types for method/closure arguments
* Semi-colons or not
* def or explicit types for local variables

I tend to prefer explicit returns and closure arguments unless the closure is inlined, e.g. list.find {it.hasErrors() }. No semi-colons for me and I tend to use def for all local variables.

Fields I sometimes type, sometimes not. Don't forget that declaring explicit types helps the IDEs a lot, so it can be worth it.