top of page

Improving your code by using IntelliJ's Inspections feature

Writing proper code in your IDE is made easier when using IntelliJ, due to its' code analysis features. One option to perform a comprehensive coding issue search is to use the Inspect Code feature IntelliJ provides. From the class you want to check for issues, right click (inside the class, in the editor) and choose Analyze --> Inspect Code.

menu
initialSelection
customScope
customeScope1

In the popup that appears, you can either select an already defined scope, or you can create a new one by clicking the '+' icon at the top.


customScope2
customScope3

customScope4

After you are done with the scope editing, just click it to finish selecting it. Then, in the initial Inspect Code popup, you must choose the profile for running the inspections. A default profile is selected at this time, which is a very comprehensive one.

If you are not sure what sort of 'issues' it looks for, you can click the '...' icon next to this area. You will see a list with checkboxes next to each enabled inspection criteria.

profiles

You can always add some of the items you are interested in, that are not already checked, or remove some that you might not find useful. Once you are finished setting up the scope and profile, just run the inspection.

If you went through the profile configuration step, you probably noticed that some of the inspection categories had also a colored square attached to them. They represent the severity attached to each issue. The benefit of setting up the severity for a particular issue is given by the fast detection of the issue - these are highlighted on the fly, while you are writing the code. Depending on the severity, you either get a colored hint that something is incorrect directly on the code (for example errors have a wave like line displayed in red beneath the text, and also a red square on the right margin of the class editor) or just on the right margin of the class editor (a yellow square, for warnings).

When the search is done, in the lower section of the editor, an 'Inspection' tab is displayed. Within it, on the left side you will see a list of potential issues IntelliJ is suggesting for you to fix.

leftSide

On the right hand side, after selecting an issue, possible fixes are suggested for each item found. You will also see some other information, like a description of the problem.

rightSide

You can apply the suggested fix for an item, by selecting the item in the left side area, then switching to the right side and choosing the solution you find most suitable in the 'Problem resolution' section. You can fix several issues of the same  type with just one move, by performing a multiple selection of the issues in the left hand side (by holding the Ctrl button while selecting the items with your mouse), then right clicking on the selection and choosing the desired fix from the suggestions.

In case you don't want to change anything regarding the code IntelliJ considers broke, you can choose to 'Suppress' the warning. This option will add an annotation to the method or attribute you chose to ignore, meaning that the next time an inspection is run, the annotated component will be excluded from the inspection and will not be suggested as incorrect. Some of the long list of potential issues are described below  (currently the tools supports over 600 types of inspections):

- unused code

- unused imports

- overly complicated expressions (like multi-nested if's)

- improper use of static members

- members that can be declared final

- unused parameters

For the full list of inspections, apart from exploring them within the IDE, you can visit the developer's page.

Cheers!

Recent Posts

See All

Creating an Architecture for Your Automated Tests Writing Automated Tests in Small Increments 4 Times Your Automation Passed While Bugs Were Present Now That You’ve Created Automated Tests, Run Them!

This week my newest article was released, this time on how to use Log4j to log relevant test automation information: https://blog.testproject.io/2021/05/05/logging-test-automation-information-with-log

bottom of page