Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically check Java files for coding standard compliance

I'm working in Java development. I have recently come into a situation where I have to comply to coding standards: member and method ordering, naming conventions, modifier sequence. I am thinking about methods to either automate checking for compliance, or generate some sort of mechanism that does the reordering.

We're developing with Eclipse, but the technology would be open. One way it might work is to generate an external builder tool and add this to the projects. The disadvantage would be that it would automagically apply to all files, which could run into problems with legacy code, blowing up the error count to a degree where it is no longer a sensible metric of compliance. Also, it makes code reviews much more difficult, which is not wanted.

Another way would be some kind of parser with only informative capabilities. We could run a process inside Jenkins, and that certainly would work, but that would also mean that the code had already passed a review, which is usually a little late for a code compliance check.

Are there suggested or even easy methods to integrate such functionality into either IDE, the source control system (Mercurial) or even Jenkins? How is this enforced elsewhere?

like image 824
0xCAFEBABE Avatar asked Mar 29 '13 16:03

0xCAFEBABE


People also ask

What is the tool used for measuring coding standards compliance for Java?

SonarLint. SonarLint is another free open source Java code review tool that checks the code against standards to evaluate the code quality. This analyzer is adept at locating security vulnerabilities and provides reports to show duplicate code, complexity, and comparison with code standards.

What is code quality standard in Java?

Code quality metrics defines code that is good (high quality) — and code that is bad (low quality). This — quality, good, bad — is all subjective. Different teams may use different definitions, based on context. Code that is considered high quality may mean one thing for an automotive developer.


1 Answers

I would not recommend doing such changes automatically. Even though most of the checkstyle/pmd complies are valid, it happens to me that I need to ignore some of the warnings/errors. Moreover - there is only very small pool of such easy issues. Most of the notifications require more complex operations and probably couldn't be done without human interaction.

I'm using Sonar integration. It contains many external checkers like PMD, CPD, Checkstyle, Findbugs and can integrate with some other useful tools like Cobertura (test coverage statistics). Its almost trivial to bind Sonar build to Jenkins build and trying to avoid major/critical issues might be considered as a good approach.

In developer environment I use Eclipse integration with findbugs. There is also some point of integration with sonar but it requires either submitting the code to server or running server locally, which I personally don't like. However after few cycles of polishing the code after code review in Sonar you will notice that you (and other team members) stick to most of the rules and checking reports on daily basis is enough.

like image 193
Mateusz Chromiński Avatar answered Sep 28 '22 09:09

Mateusz Chromiński