Supercharge your code quality using SonarLint

Paul ‘Tofunmi O.
Zero To Production
Published in
3 min readApr 15, 2019

Using SonarLint in IntelliJ or your preferred IDE

Photo by Pankaj Patel on Unsplash

In Javascript, you can so many linters from jslint to jshint and eslint. In Java, we also have linters. An example is SonarLint.

SonarLint is a plugin that can be installed into your IDE of your choice. At time of writing this tutorial. It is supported in IntelliJ IDEA and eclipse IDE.

Purpose of Linting

Linters are tools that you can use to help you debug your code. They scan your scripts for common issues and errors, and give you back a report with line numbers that you can use to fix things.

Besides, linters also check if your code adheres to standards, is it performance-oriented? Would there be any memory leak because you forgot to close a stream? Linters do this out of the box. You could also picture linter as your personal code reviewer. Besides, checking adherence to standards, linters help in identifying quality issues in your Java code.

According to the official docs,

SonarLint is an IDE extension that helps you detect and fix quality issues as you write code. Like a spell checker, SonarLint squiggles flaws so they can be fixed before committing code. You can get it directly from the IntelliJ IDEA Plugin Repository, and it will then detect new bugs and quality issues as you code (Java, Kotlin, Ruby, JavaScript, PHP and Python).
If your project is analyzed on SonarQube or on SonarCloud, SonarLint can connect to the server to retrieve the appropriate quality profiles and settings for that project. Java 8 is required to run SonarLint.

How to install SonarLint?

Java 8 is required to run SonarLint plugin.

  1. In your file menu, click on preferences. A dialog must be opened now.
  2. In the left pane, click on plugins.

3. Search for SonarLint and click the green button with text Install.

It will require that you restart your IDE so do that now.

Let’s analyze our code

Right click on the file or package you want to scan, then look for analyze and analyze with Sonarlint.

After this, you should see the result open at the bottom of your IDE where Terminal, Version Control and Todo labels are.

There is an issue with our code. I have clicked on the issue to see the message in the rule pane on the right hand side. The message is “Methods should not be empty.”

Let’s fix our code by adding a comment. See the fixed code below.

We have added a block comment with text: We are doing nothing. After that, we re-scan our code and the issues have gone.

That wraps up out guide.

--

--

Paul ‘Tofunmi O.
Zero To Production

Passionate about business, technology, abundant living — a life of purpose — and a true follower of Jesus