Mark Stephens Mark Stephens has been working with Java and PDF since 1999 and has diversified into HTML5, SVG and JavaFX. He also enjoys speaking at conferences and has been a Speaker at user groups, Business of Software, Seybold and JavaOne conferences. He has a very dry sense of humor and an MA in Medieval History for which he has not yet found a practical use.

How we are improving our code quality with IDEA in 2018

1 min read

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).

IDEA menu

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.

IDEA inspections

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….

IDEA screenshot

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.

annotations inspection

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.long methods

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?

If you’re a first-time reader, or simply want to be notified when we post new articles and updates, you can keep up to date by social media (Twitter, Facebook and Google+) or the Blog RSS.

Mark Stephens Mark Stephens has been working with Java and PDF since 1999 and has diversified into HTML5, SVG and JavaFX. He also enjoys speaking at conferences and has been a Speaker at user groups, Business of Software, Seybold and JavaOne conferences. He has a very dry sense of humor and an MA in Medieval History for which he has not yet found a practical use.

Leave a Reply

Your email address will not be published. Required fields are marked *