Qualitative code analysis with SonarQube
Code quality sometimes gets little discussion. We often take it to mean simply "code that works." But is that really enough?

Editorial translation by Impulso.
Introduction
Code quality sometimes gets little discussion. We often take it to mean simply "code that works". But is that really enough?
What exactly is code quality?
There are many definitions of good code. I recently read the excellent book Clean Code (Robert Martin, 2009), which offers several definitions. But the authors who contributed to the book agree on one point: good code is simple and efficient. To achieve that goal, programmers have come up with dozens of rules and design patterns over the years.
SonarQube
SonarQube is an open-source platform developed by SonarSource to continuously inspect code quality. It uses static code analysis to automatically review code and detect bugs, code smells, and security vulnerabilities in 20+ programming languages.
It reports on duplicated code, coding standards, unit tests, code coverage, code complexity, comments, bugs, and security vulnerabilities. (Source: Wikipedia)
Let's build a sample project...
Now let's create a sample project. You can find the source code at https://gitlab.com/orlandoburli/calculadora-de-impostos.

Fig. 1 – Source code for the application we'll analyze.
It's a simple project: a tax calculator. A single class takes a gross amount, calculates ICMS and IPI, and returns the net amount.
Setting up the environment
To keep things quick and simple, we'll use a Docker container to run SonarQube. Just run this command:
docker run -d --name sonarqube -p 9000:9000 sonarqube
SonarQube is now running on your machine. To access it, open http://localhost:9000 in your browser and log in with the username admin and password admin.

Fig. 2 – SonarQube home screen
Running the analysis
Now let's analyze the code. Go to the project directory and run this Maven command: mvn sonar:sonar

Fig. 3 – Output from the SonarQube analysis.
Analysis results
Now let's look at the results. The analysis highlighted a few important things about our project, as shown below:

Fig. 4 – SonarQube home screen showing the project results
The first thing we see is our project's grade: A. That means it meets the quality gates, since it's a small project.
Still, it flags a few issues, so there's room for improvement. It says we have 3 "code smells" (roughly speaking, poorly written code). It also says we have 0% test coverage.
Let's take a closer look at what it flags as "code smells":

Fig. 5 – Details of the "code smells" flagged by SonarQube.
Now let's go through the 3 items one by one:
Move the array designator from the variable to the type
This item refers to this line of code:
public static void main(final String args[]) {
It's saying that the brackets in args[] should go with the String type rather than the variable name, so the code would look like this:
public static void main(final String[] args) {
Replace this use of System.out or System.err by a logger
This item refers to this line of code:
System.out.println(new Calculadora().calcularValorLiquido(100.00));
We're printing directly to the console, which it flags as bad practice in projects. It suggests using a logging library to write to the console.
Immediately return this expression instead of assigning it to the temporary variable "valorLiquido".
This item refers to this line of code:
final double valorLiquido = valorBruto - icms - ipi;
Basically, it's saying I don't need to create the variable valorLiquido and should immediately return the value of the expression valorBruto - icms - ipi.
So this snippet would become:
public double calcularValorLiquido(final double valorBruto) {
final double icms = valorBruto * 0.12;
final double ipi = valorBruto * 0.03;
Here's what the complete code looks like in the end:
package br.com.orlandoburli.calculadora;
import java.util.logging.Logger;
public class Calculadora {
private static Logger logger = Logger.getLogger("calculadora");
public static void main(final String[] args) {
logger.info(String.format("Valor líquido: %1$,.2f", new Calculadora().calcularValorLiquido(100.00)));
}
public double calcularValorLiquido(final double valorBruto) {
final double icms = valorBruto * 0.12;
final double ipi = valorBruto * 0.03;
Rerunning the analysis
Now that we've made the suggested changes, let's run the analysis again with the command mvn sonar:sonar.

Fig. 6 – Results of the new analysis

Fig. 7 – Detail from the new analysis results
Notice that we now have just 1 code smell, related to our new implementation using a logger. It says to call this method conditionally so the expression in the logging call is only evaluated when the INFO log level is enabled. That's a good practice that improves performance. You can keep refactoring your code until you're happy with it or until you've eliminated all the "code smells".
Conclusion
SonarQube is an excellent tool that keeps evolving. It's well worth using in your projects to help you write code that doesn't just work, but works well and follows a whole set of best practices.


