How to fix PMD violations with WalkMod

walkmod
walkmod
Published in
3 min readFeb 28, 2017

Code conventions are programming guidelines to improve the code readability, reduce bugs and thus, reduce maintenance costs. Static code analysis tools such as, PMD, automatically detect those parts of your code that are common bad practices.

Usually, when we run PMD from the command line or from our build tool, we get reports like this:

$pmd pmd -d . -rulesets ruleset.xml/Users/rpau/tests/test/src/main/java/org/walkmod/Bar.java:5: Document empty method body/Users/rpau/tests/test/src/main/java/org/walkmod/Bar.java:22: Useless parentheses./Users/rpau/tests/test/src/main/java/org/walkmod/Bar.java:32: A switch statement does not contain a break
...

Applying quick fixes with WalkMod

Unfortunately, most of the existing static code analysis tools cannot work incrementally, that is, we cannot ask them for just those issues introduced during the last code commit, but always report all the issues present in the project. If we want to know just the introduced violations, we first need to fix existing ones.

In such a situation, we usually become overwhelmed by the time needed to fix issues, which in many cases leads to the decision of not using static code analysis tools. In order to potentiate the usefulness of these tools, we have created software to promote automatic Java quick fixes without being dependent of any IDE.

In small projects with a high test coverage, like micro-service architectures, we can automatically apply quick fixes with a high confidence, using an open source tool called WalkMod. In order to do so, you simply need to run:

walkmod apply -DconfigurationFile=ruleset.xml pmd

where ruleset.xml is your PMD configuration. Then, we will see a similar output which prints those files that have been modified by the tool.

INFO [main] - ** STARTING TRANSFORMATIONS CHAINS **-----------------------------------------------------------INFO [main] - >>com.mycompany.MyClassINFO [main] - >>com.mycompany.dto.Entity....------------------------------------------------------------INFO [main] - TRANSFORMATION CHAIN SUCCESS------------------------------------------------------------INFO [main] - Total time : 0,96 seconds

We do not recommend to apply a bulk of quick fixes for monolithic applications with lots of legacy code, which usually do not have enough test coverage. In these cases, automatic fixings may introduce so many changes to be accepted during the code review.

Automatic code refactorings are generally considered safer than manual changes because these are usually open source and have been already validated by many other projects. However, in order to apply them under supervision, it is necessary to create a separate repository with all the existing issues with their own quick fixes, and incrementally fix those that affect pieces of code that we have recently changed. If this is your case, we recommend to use WalkModHub.

How to contribute with other PMD quick fixes

The list of available PMD quick fixes is in the github.com/walkmod/walkmod-pmd-plugin repository. We encourage developers to to participate in this Github project, sending us pull requests with new ones or creating an issue for those you would like to have.

--

--

walkmod
walkmod
Editor for

Open source technology to fix code style issues and reduce technical debt