In my previous article, I worked on code quality in NetBeans IDE. In this article, I am using IntelliJ IDEA to improve my Java code.
IDEA has plugins to support PMD and FindBugs and SonarCube but it also comes with its own static analysis capabilities.
The Analyse menu gives you the option to Inspect code (and some other goodies).
You can run a single inspection or create multiple rulesets (I have one for code quality and one for spotting Java8 improvements) and it can be run when you push code as well as while developing code. Hints will appear in the IDE which you can accept. The configuration can be saved and shared with other IDEA users on the team to standardise your code.
The number of options on offer is very comprehensive and covers much more than Java. It will not only find but offer to fix many issues. It will also take into account your settings, such as Java level, and not recommend Java8 fixes if the code level is Java7. Here are some of the Java options. Many are highly configurable and have a detailed explanation.
Here is a nice example of some code which the selected rule has spotted. One rule you will always find holds true on any tool is that mistakes are obvious once pointed out….
It is important not to become over-obsessed with trying to implement all rules and some may have limited value or not be relevant such as the rule to show Annotations.
If you want to apply a rule but ignore it in a specific context, you can suppress the warning for a case, a method or a whole class.
You may also want to gradually implement some rules. If you have lots of very long methods, you might want to choose all methods over 500 lines to start with to catch the worst offenders and gradually reduce the threshold.
I recommend that you start with an empty ruleset and slowly add the rules which make sense in your coding environment. I generally try to add a new Inspection and incrementally reduce the cases found to zero. I then re-run my Inspections every morning to ensure no new cases have appeared (for example unused imports might pick up some imports no longer needed after code changes).
I am very impressed with IDEA’s code analysis capabilities. If you are using it, which Inspection do you rate most highly?
Do you need to solve any of these problems in Java?
Convert PDF to HTML5 | Convert PDF to SVG | View Forms in the browser |
View PDF Documents | Convert PDF to image | Extract Text from PDF |
Read/Write images | Replace ImageIO | Convert Image to PDF |