Pragmatic Programmer Issues

Groovy and Grails Code Quality

Comments: 3

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


> 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.




@Bartek: Thanks for sharing.

@Chris: Thanks for great tips.
BTW: I’ve just upgraded CodeNarc to 0.15. and it is working with grails 1.4M1.

Chris MairChris Mair

Hi Pedro.

For the FieldName rule, one option is to configure the rule (either within a custom ruleset or within “”) ignore the files or classes containing the DSLs that are causing you problems. See the “Configuring Rules” page on the CodeNarc web site for more info.

The DuplicateNumberRule defaults to ignoring test files (/.*(Test|Tests|TestCase)\.groovy/) but if that is not sufficient, you can configure to ignore specific files or class names. I would also say that the Duplicate*Literal rules may not be appropriate for all codebases/teams depending on coding styles and standards.

For DuplicateStringLiteral, one option is to configure the ‘ignoreStrings’ property of the rule if you have a relatively small set of predefined string values. Otherwise, you can configure the rule to ignore specific files and classes, as well.

The StatelessClass rule is in the “generic” ruleset, which is kind of special. These are rules that are not intended to be applied as-is to your entire codebase. Rather, they should be configured to apply to specific classes or types of classes (e.g. DAOs), and for some of those rules, require certain properties to be configured. The fact that the StatelessClass rule did anything without being configured was a bug which has been fixed in CodeNarc 0.15. I agree that it would be silly to expect all of your classes to be stateless.

In general, you should tailor the ruleset that you use to your own standards and styles. Pick the subset of the rules that make sense for you.

Version 0.15 of CodeNarc was released a couple days ago, and includes a bunch of new rules and other fixes and enhancements.

And feel free to post any questions about CodeNarc on the user mailing list (codenarc-user at lists[dot]sourceforge[dot]net).

Chris Mair


Hudson/Jenkins already supports CodeNarc output parsing via the Violations plugin –