It is always very hard problem to begin respecting code style on a big old project, especially if there are a lot of developers and the initial code quality is not so good. However, I believe that even in this case it is needed to pay attention on code quality to improve at least some parts of project. The main idea, all developers should start writing good code despite the rest classes still contain horrible code. In this post I’d like to tell about the system that helps improving style in a huge project and share some points about codenarc and its grails plugin.
I decided to use Codenarc grails plugin for checking style. Actually I hadn’t any choice for grails project. It generates pretty html report with sorted style violations. When I launched it on the project it returned me thousands of errors. It is useful information when you want to get know some statistics about whole project, but it is absolutely uncomfortable for a single developer, who wants just to check if his or hew own code is valid. Of course, it is possible to find edited classes using browser search in html report, but it is very uncomfortable - unfortunately, sometimes commits are rather big.
To solve this problem I took command line codenarc version. It does the same what grails plugin does, but using command line. The idea is to get a list of modified files by git and check them with codenarc and, as a result, generate a simple small report only for files that were modified by developer. It is expected, that reading this report developer will fix some errors, which can be fixed without any additional risk.
How to use codenarc commandline
There is only one small page in official site that describes using command line app. Actually, it is rather more complicated. My codenarc.xml configuration is following:
It is important to publish it, because different rules demand different dependencies and you are responsible for providing it. It is said on project’s site, that following resources must be included in classpath
- The Groovy jar
- The CodeNarc jar
- The Log4J jar
- The directories containing (or relative to) CodeNarc config files such as “codenarc.properties” or ruleset files.
Actually, you may be asked to add more resources, otherwise ClassNotFoundException will be thrown. I’ve added additionally GMetrics-0.3.jar and …one more groovy:) It seems, that different rules uses different groovy versions. When I changed version to 1, I got one ClassNotFoundException error, else other class was not found with groovy version 2. Fortunately, adding two groovy jars solved the problem, however it is very risky action, I’m lucky that it is working without any problems. (Of course this approach is applicable in limited set of situations, actually we don’t risk if something goes wrong with codestyle checking. I’m still finding better solution when I have free time)
After downloading necessary dependencies, this script will be able to generate a report:
Git integration
This script below collects all files in a string and then checks them using “includes” option of command line application
It is assumed following file structure (project is the root dir of grails project):
check.sh
libs
groovy-all-1.7.10.jar
groovy-all-2.1.0.jar
CodeNarc-0.27.0.jar
log4j-1.2.17.jar
GMetrics-0.3.jar
project
config
codenarc
codenarc.xml