IntelliJ IDEA Java

Introduction to Code Analysis in IntelliJ IDEA

Static code analysis refers to the practice of scanning code for potential problems without actually running the code. Inspections in IntelliJ IDEA can detect potential problems in your project before you compile it. The IDE can highlight various problems, locate dead code, find probable bugs and spelling problems, and improve the overall code structure.

This series of blog posts will cover several ways in which code analysis in IntelliJ IDEA can help you find and fix problems in your code. In this first installment, we’ll start by looking at how the IDE can help you prevent problems while working with code.

Find and fix problems in your code

When you write code in IntelliJ IDEA, the IDE will let you know if there are areas in your code that need more attention and highlight them for you. Let’s look at some examples.

If your code will not compile, IntelliJ IDEA will let you know – without requiring you to compile it first. For example, the code below will not compile because FileReader may throw a FileNotFoundException, which is a checked exception that should be declared in the method. You’ll see that this code is highlighted and when you hover over it, IntelliJ IDEA will tell you what the problem is. It will also suggest how you can fix it – by adding the exception to the method signature. 

Unhandled Exception

IntelliJ IDEA will also let you know when code is redundant. In the following example, new File is shown in gray. This is because it is redundant and can be replaced with a file name.

Redundant Code

Quickly see what’s wrong in your code…

As you’ve seen above, IntelliJ IDEA will highlight potential problems in your code. If you’d like more information about the problems, you can move your cursor over the highlighted code in the editor, hover over the error stripes in the right gutter, or click the lightbulb icon on the left.

The Inspection widget in the top right-hand corner of the editor pane will show you the number of problems in the current file and their severity. You can click the Inspection widget to open the list of problems, which will then be displayed in the File tab of the Problems tool window.

Inspection Widget

…and automatically fix it

IntelliJ IDEA offers quick-fixes for these inspections to help you fix your code on the fly. Use ⌥⏎ (macOS) or Alt+Enter (Windows/Linux) to invoke Show Context Actions and select the desired action from the list to apply it to your code.

If there are multiple errors and warnings in the code, use F2 to navigate to the next problem, or Shift+F2 to navigate to the previous problem. This way, you can fix each problem one by one.

Fix Problems

Improve the quality of your code

Even if there are no problems in your code, IntelliJ IDEA might still have some suggestions on how you can improve your code. Use Show Context Actions (⌥⏎ on macOS or Alt+Enter on Windows/Linux) to apply suggestions to your code. For example, you can try new idioms, like a forEach instead of a for loop, or a stream instead of a while loop, or even use a try-with-resources statement instead of closing the BufferedReader yourself.

Suggestions

Fix all occurrences of a specific problem throughout the file

If the same problem occurs multiple times in the same file, you don’t need to fix each occurrence individually! IntelliJ IDEA can also fix a particular problem in multiple places in the file. When using Show Context Actions (⌥⏎ on macOS) or Alt+Enter on Windows/Linux), click the three dots to the right of a suggestion to apply that suggestion throughout the file.

Fix All Problems In File

Conclusion

IntelliJ IDEA can help you prevent problems by highlighting code that needs more attention as you’re writing it and help you find areas of improvement for the code you’re working on. 

IntelliJ IDEA can also help you resolve problems throughout your project, not just in the file you’re currently working on. In the next part of this series, we’ll look at analyzing the code for your project.

Subscribe to our blog if you don’t want to miss it.

image description