Groovy and Grails Code Quality

I’m involved in Grails project. Despite of development, my responsibility is taking care about code quality. I do Code Review, and I was wondering about tool to help me a little bit. In meantime I was reading GroovyMag issue and found out CodeNarc tool.

This tool do static analysis for groovy. Last CodeNarc version is 0.14, which means that it is still in development phase, but even now it has 241 rules. Also we can create our rules and rules set which make this tool really useful. Check this documents:

But good tool without integration do not exists. Fortunately CodeNarc comes with support with: Maven, Ant, Gradle, Grails, Griffon, Sonar and Hudson (coming soon).

I was delighted as IntelliJ user, because there is CodeNerc IntelliJ Plugin. After standard plugin activation we have to enable rules, which are disabled by default. See picture bellow.


Till now I disabled some rules, because from my point of view there was to much false positives:

  • Ruleset naming : FieldName -> it complains a lot about DSL-s
  • Ruleset dry: DuplicateStringLiteral -> we have something like this [save: "POST", update: "POST", delete: "POST"] and I think it is more readable than externalizing string “POST”
  • Ruleset dry: DuplicateNumberLiteral -> false positives in tests
  • Ruleset generic: StatelessClass – every class is by default “stateless” so this rule complains a lot about closure methods, fields etc.

I wonder what do you think about disabling this rules and which rules are useless from your point of view.

Last but not least is grails integration. CodeNerc comes with grail plugin, so everything is easy:

> grails install-plugin codenarc

then:

> grails codenarc
….
CodeNarc completed: (p1=0; p2=0; p3=0) 7673msCodeNarc finished; report(s) generated: [CodeNarcReport.html]

And done, you have nice html file with: package, total files, files with violations, priority 1, priority 2 and priority 3. I cannot attach file so just look at images.

Unfortunately plugin do not work with Grails 1.4M1, I do not check it with 2.0M1, but it is working with 1.3.x and probably next version will support 2.0M1/1.4M1.

Happy quality code.