Keep your code clean just like you keep your hands clean

Jeremia Delviero
4 min readApr 14, 2020

--

Washing hands is effective against coronavirus | Source: New York Post

In this pandemic situation, we should keep our hands clean by keep washing them regularly with soap and do it 20 seconds minimum to prevent ourselves from coronavirus. In case of developing a program, we should keep our codes clean. In this article I’ll explain what we need to do to keep our codes clean based on my research and experience.

What is Clean Code?

“Clean code is code that is easy to understand and easy to change.”

It sounds really simple and straightforward, right? But what does it really mean?

Easy to understand means everyone can read it well, including the author of code or somebody else. Easy to change means the code is easy to extend or refactor by anyone without causing anxiety about breaking the functionality of the existing code.

What things that must be considered in writing clean code?

Based on my experiences, we should consider these things to maintain our code clean.

1Meaningful Names
Whenever you’re declaring a variable, make sure the variable name defines the content of it just right. No need to put a whole sentences as name of a variable. But also don’t use like a single character as a variable name except for iteration (in for loop we usually use “i” as the name of iteration variable).

Example of uses of variable names | Source: Slideplayer
Example of my code using proper names

2Never define a function twice.
Whenever developing codes, make sure you don’t repeat any same functional block anywhere. This will create redundancy for your code. To prevent it, wrap the lines that have same functional into a new function.

In my case, I always use IDE to develop my work to keep my codes clean. If I have any redundancy in my code, the IDE will automatically warns me that my code have repeated lines of code so I can fix them easily.

3Keep the consistency of your team code style.
Every time you set any code styling for your project in your team, make sure each of member of the team use the same style. Or you’ll be facing conflict in git or your code will be detected as different code so your code will be overwritten by other members on your team. For example:

Difference size of indentation will cause something like this

In order to fix that, we should set code styles that applies to every member of the team to maintain the consistency of the code so that won’t happen again.

In the next section I will explain some tools/frameworks that can help us develop and maintain clean code

How to develop program faster by maintaining the code clean easily

1Beautifier
Theres many plugins for text editor or maybe already installed in IDE like in Jetbrains IDEs

Reformat to beautify code in Datagrip

2Linter
lint, or a linter, is a tool that analyzes source code to flag programming errors, bugs, stylistic errors, and suspicious constructs. In my team project we use ESLint as linter for our Frontend. We use “vue/recommended” plugin so that ESLint will have stricter rules than the default one. We managed to use that plugin to keep our code consistent between each other member.

My project use “plugin:vue/recommended” as the priority of the linter

3SonarQube
SonarQube (formerly Sonar) is an open-source platform developed by SonarSource for continuous inspection of code quality to perform automatic reviews with static analysis of code to detect bugs, code smells, and security vulnerabilities on 20+ programming languages. SonarQube offers reports on duplicated code, coding standards, unit tests, code coverage, code complexity, comments, bugs, and security vulnerabilities.

By using SonarQube, we can easily get reports of our code coverage, bugs, code smells, etc. and its automatically detect them when we’re pushing our work into pipeline.

Conclusion

Clean Code is mandatory for every developer who works in a team or even individually so another team member or his/her future self can still understand the code and don’t need to spend too much time to understand a bad code.

Reference

https://en.wikipedia.org/wiki/SonarQube

--

--