Setting Measurable Goals - Improving Code Quality with SonarQube

Learn how to set measurable goals for improving code quality with SonarQube, complete with setup instructions.


Introduction

As a lead software engineer, one of my key responsibilities is setting clear and measurable goals for my team. One such goal we've recently tackled is improving software quality. However, transforming this broad objective into a quantifiable target is crucial for tracking progress effectively. In this guide, I'll share how we established measurable goals using SonarQube and provide step-by-step instructions for setting it up locally.

Setting Measurable Goals

During a quarterly goal-setting meeting with my team, we discussed the importance of enhancing software quality. One of our team members expressed interest in exploring tools to aid in this endeavor. Since SonarQube was not currently integrated into our projects, I decided to introduce the team to its capabilities and demonstrate how it could benefit our development process.

Introducing SonarQube

I walked the team through the features of SonarQube, highlighting its ability to analyze code quality metrics and identify areas for improvement. However, without direct integration into our projects, the team was unsure how to leverage SonarQube effectively. That's when I proposed setting it up locally, allowing us to connect it to our project repositories and start reaping the benefits immediately.

Setting Up SonarQube Locally

Let's dive into setting up SonarQube locally to kickstart our journey towards achieving our quality goals.

Step 1: Docker Compose Configuration

Begin by creating a docker-compose.yml file with the following contents:

version: "3.7"
 
services:
  sonarqube:
    image: sonarqube:lts-community
    ports:
      - "9000:9000"
    environment:
      - SONARQUBE_JDBC_URL=jdbc:postgresql://sonarqube-db:5432/sonar
    networks:
      - sonarnet
 
  sonarqube-db:
    image: postgres:alpine
    environment:
      - POSTGRES_USER=sonar
      - POSTGRES_PASSWORD=sonar
    networks:
      - sonarnet
 
networks:
  sonarnet:

Step 2: Run Docker Compose

Execute the following command to start SonarQube using Docker Compose:

docker-compose up

Step 3: Access SonarQube

Once SonarQube is up and running, access it through your web browser using the following URL:

http://localhost:9000

Step 4: Initial Setup

Upon accessing SonarQube, reset the default credentials (admin/admin) and manually create a local repository as prompted. This step ensures that you have full control over your code analysis process.

Step 5: Generate Authentication Token

To perform code analysis, you'll need an authentication token. Generate a token from within the SonarQube interface, which you'll use later when running the Sonar-Scanner.

Step 6: Install Sonar-Scanner

Next, install the Sonar-Scanner application, which is required to execute code analysis. If you're using macOS, you can install it via Homebrew:

brew install sonar-scanner

Step 7: Analyze Your Code

With SonarQube set up and the Sonar-Scanner installed, you're ready to analyze your code. Execute the following Sonar-Scanner command (adjusted as needed for your project) to initiate the analysis:

sonar-scanner \
  -Dsonar.projectKey=test \
  -Dsonar.sources=. \
  -Dsonar.host.url=http://localhost:9000 \
  -Dsonar.login=<YOUR_AUTHENTICATION_TOKEN>

Step 8: Investigate Code Metrics

With SonarQube up and running and connected to your repository, take some time to investigate the code metrics it provides. Explore the various reports and insights generated by SonarQube, including code quality, test coverage, and potential issues such as bugs or code smells. Use these metrics to gain a deeper understanding of your codebase and identify areas for improvement. Regularly monitor these metrics to track progress towards your quality goals and ensure continuous improvement in your software development process.

Conclusion

By following these steps, you've successfully set up SonarQube locally and performed a code analysis on your project. Armed with the insights provided by SonarQube, you're now equipped to make targeted improvements to your codebase and achieve our quantified goal of increasing test coverage and reducing bugs by 10%. With SonarQube guiding our efforts, we're confident in our ability to elevate our software quality to new heights.