Clean Code
Hello hi welcome. In this session, I will tell you about clean code. Clean code is a well-known term in the programming world and it is a really good practice of programming hence making it well-known. So what really is clean code? Does it have to do with the hygiene of my working environment while I code?

No. Clean code isn’t talking about hygiene in real life. It’s about the “cleanliness” of your code, making it more elegant and pleasing to look at. The code must be written in its optimal potential and not causing any ambiguity and useless statements.
The main purpose of clean code is for you and your colleagues to understand the code better. Because maybe, after we wrote lines and lines of code, we took a day off, a vacation perhaps. You get back to work and you completely forgot what did you wrote and how the program works even though it was you who wrote that exact code a week ago. This happens to a lot of programmers, even myself. For your colleague, they didn’t write the code but they need to know what is happening. It would be a total waste of time if your colleague spent the whole day asking what this and that line in your code does every 5 minutes. So we wrote the program in a clean code principle as an “inline guidebook” inside your code to make your colleague's lives easier.
Characteristics of a Clean code:
- It should be elegant. Clean code should be pleasing to read for you and others, especially.
- Clean code is focused on every element. Functions, classes, modules must be focused on its’ own usage and don’t have outside influences.
- Clean code is taken care of. People have been working really hard and committed in order to make the code clean.
- Runs all the test
- It doesn’t contain any duplication. This characteristic is more popular with the term Don’t Repeat Yourself (DRY)
- Minimize the number of entities
How to write Clean code?
Meaningful names
We use intention revealing names. Choosing names that are really what the variable or function or class is for. If the name is pretty much self-explanatory, then we don’t need any comment to justify it.
int distance;
int velocity;Use comments
Code documentation is important. It helps you and your colleagues from the case I stated above. In order to keep the code understandable, we comment before the start of the function to make it more understandable.
# this function gets input n from user and add it to 5 as the output
def addfive(n):
return n + 5Test code
We also should make a clean test where the goal is when the tests fail or succeed, we know what is the purpose of the test. Note that we did not repeat the code on the test file below
Eliminate duplication code
In the characteristics above there is something that is called DRY, an acronym for Don’t Repeat Yourself. The goal is to reduce the repetition of patterns with the goal helps us if we need to make modifications to the following code. If we repeat the code countless time, it will be a pain if we need to modify it, searching and changing the code in many many lines.
Using libraries to optimize clean code
There is a lot of libraries or text editor extension that can track the cleanliness of your code. Since we wrote the code in Typescript, there is an extension that is called Eslint. It is a linter that helps us in practicing clean code. It helps us real-time if we wrote the code in a bad manner or not. Mostly, in the text editor every time when we do it wrong, it will show a red line underneath our code and tells us what is not good on our code.
Mostly the linter extension on the text editor will show a red line if:
- When a variable is not used at all
- Trailing whitespaces
- No blank new line in the very last part of the code
There is also an npm library called lint that helps us in practicing clean code. To do it we simply install the lint extension and run it using npm run lint . The library will analyze the code and mostly doing the same thing with what we have on our extension, but it prints the error on the terminal.

