concept CodeNarc in category griffon

appears as: The CodeNarc, CodeNarc
Griffon in Action

This is an excerpt from Manning's book Griffon in Action.

9.4.2. Reporting Groovy code violations with CodeNarc

The elevator pitch for CodeNarc (http://codenarc.sourceforge.net) is “FindBugs for Groovy code.” Ambitious but true. CodeNarc’s creator, Chris Mair, has done what some thought was a very difficult task: build a static code analysis tool for a dynamic language. What makes CodeNarc special on its own is that it relies on Groovy’s AST transformation framework to walk the Groovy code and discover code violations. Yes, the same AST transformation framework used by @Bindable. Give it a try and see what you get for the Dictionary application:

$ griffon install-plugin codenarc

That should do the trick. This plugin adds a new script that calls CodeNarc’s code processor with a standard configuration. You can change and tweak that configuration at will. Be sure to review the plugin’s documentation page (http://griffon.codehaus.org/Codenarc+Plugin) to learn about those tunable configuration flags. Running Code-Narc as follows against the current application yields the report shown in figure 9.6:

Hmmm. The report looks clean. Is it true that the code is well crafted? Yes, at least for CodeNarc’s standard set of violations. You’ll now change the code a bit and run CodeNarc again, to see if you get a different picture of the codebase. Sprinkle a few declared but unused variables in the code, paired with a couple of unnecessary imports. Make sure you catch an exception in the controller code (you already have a try/finally block that can be used for this negative refactoring). Ready? Re-run CodeNarc, and inspect the generated report. You might get one that looks like figure 9.7.

Figure 9.7. Files in violation as found by CodeNarc. The controller fails three rules, whereas the view fails just one.