Soap and Dishwasher,
Clean Your Code with Lint and Sonarqube!
Cleaning your code has never been easier than ever!
This article was written under the purpose of individual assignment for PPL CSUI 2021, and also to educate readers on the importance of clean code
Introduction
When designing your software, many principles and ideals were enacted to ensure that your software is the best it ever be. One of these principles is clean code, where codes are expected to be simple, direct, and well-written. Readers who have known or even implemented clean code in their project would agree with this statement, but it’s okay if you don’t. By implementing clean code, we can increase code readability and reduce possible harm to our program in the long run such as technical debt. Since a software is not something that is usually made alone, increasing code readability would ease your co-worker to understand your code, which in turns increase team’s productivity (and even if you made your program alone, nobody can guarantee you would still understand your code in the future!). On the other hand, a code that is not clean might be inefficient, tedious, and tiring, which not only brings clutter to your program but could also harm your program’s performance!
The Problem
Well, nobody says that cleaning code is all easy and fun like implementing the KonMari method in cleaning your house. Not even me. Sometimes, implementing clean code can be hard when you’re coding under a programming language that you’re new to. This is not without any reason; when implementing clean code, you’re expected to follow the naming conventions and best practices under the programming language to boost their readability. Nobody would understand your code when you write C++ as how you code in Python!
So, what am I supposed to do Zakiy? Just sit around with my dirty code and suffer?
Gratefully, some services have provided ways for you to understand your code easier, detecting code smells, bugs, and code impurities that you may have missed! This is demonstrated from Lint (e.g. Gitlab CI Lint) or a standalone platform such as SonarQube. Gitlab CI Lint can be learned here and SonarQube can be learned here. By implementing clean code with additional help from Lint and SonarQube, coding your program can be easier, better, and faster!
How to Use Lint and SonarQube in Cleaning Your Code: A True Story
Under PPL, me and my team are tasked to create an android mobile application using Flutter. Flutter uses Dart as their programming language, which clearly means that I’m supposed to be following Dart naming conventions and best practices to implement clean code for my project. However, this is not easy to do since pretty much everyone in my team is new to Flutter. By trying step-by-step to assimilate ourselves with Dart and following code analysis given by Lint and SonarQube, our code can be cleaner and we have an easier time understanding each other codes.
Example of Using Gitlab CI Lint to do Clean Code
In the original commit (#1 picture), we can see that our commit isn’t accepted due to code smells found in it. After noticing code smells in my code, I cleaned my code as suggested by Lint and pushed the correction into my Gitlab project. When implementing clean code, we should always remove unused codes and variables! You might non-activate it for a while (by turning the code into comment block), but codes submitted into staging and deployment must not have unused or commented codes. Thanks to Lint, we can notice this problem and sort it out nicely.
Pro Tip: some application offers add on to analyze your code! For example, installing Dart on your Android Studio offers Dart Analysis that you can use to check on code smells. This way, we don’t have to commit just to use CI Lint, but Lint still acts nicely as a buffer to avoid harmful code smells on our end product.
Example of Using SonarQube to do Clean Code
Compared to previous services mentioned before, SonarQube offers a more intensive and thorough analysis of our code. The good news is, SonarQube can help you clean your code than ever! 😊 The bad news is, you have lots more code smells to handle with 😭. Not only code smells, Sonarqube also analyzes your code under several criteria such as security, bugs, and code coverage. When identifying a mistake, Sonarqube usually shows an “error” message that can be googled under your programming language best practices to fix. Some mistakes seem to exists when cleaning your code under the guidance of SonarQube though, so don’t feel bad if your code smells and bugs still exists after lots of cleanings (e.g. using const shows up as ‘code smells’ under SonarQube, while not using const shows up as ‘bugs’)!
After reading this article, you have learned about clean code and how to use external services to aid you in writing clean code. Don’t forget, clean your code! See you in my next article! 😊