Let’s Clean Our Code!

Gafirazi Irfandi
3 min readApr 5, 2021

You must have experience one time when reading a code, you are confused because there is a source code that you can’t understand and you are a good programmer. Usually, this happens because the source code is not orchestrated well. To avoid that let’s learn about Clean Code to avoid things like that.

What is Clean Code

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

Clean code is code that is easy for others to understand and easy to replace (refactor) by oneself or others. Because it is easily understood so that other people can also continue the work of the code.

Why should we care about Clean Code?

You should care because code is (almost) never written just once and then forgotten. Most of the time you, or someone else, need to work on the code. And to be able to work on it efficiently you need to understand the code. For example, if you are working in a team, and your teammate have to work based on your code. If you don’t implement Clean Code, then your teammate will have a hard time understanding your code, thus the working process will be delayed.

Implementing Clean Code

To implement clean code, we must understand the rules in clean code to make our code easy for others to understand and reuse. Below are the rules in clean code:

  1. Naming meaningful variables, functions, classes, and file names

Above is a code example with bad and good examples of the implementation of meaningful names. Meaningful names themselves can be achieved by naming variables, functions, classes, and file names with names that describe their function. In the bad example above, it is difficult for us to understand what the function of the x and y variables is, as well as what the function f is for. Therefore, we have to implement meaningful names like the good example above, by naming email and password variables, and a function called signIn.

2. Functions have only one goal (Single Responsibility Principle)

The above is a good example for implementing the Single Responsibility Principle, because each function only has one goal, and does not do anything else that is not related to that goal.

3. Creating exception handling for errors

The above is a good example of an exception handling implementation to properly implement clean code. In the example above, the code tries to post data to sign in. However, if a timeout occurs it will be caught by the exception that has been created and will print the error.

4. One assert per test

The example above is an example that implements the use of assert only once per test function. Clean code requires us to create a test function with only one assert so that it is clear what that one test function does.

5. Implementing TDD

The last example of how to implement clean code is by implementing TDD. By implementing TDD, your code is going to be more efficient and less error-prone. Because, in TDD you do testing first, and then afterward you implement the test.

There are so many rules in clean code that you can implement as well. But for this article, I will only describe the major rules from clean code.

Conclusion

In conclusion, we have to do clean code every time we do coding. Because, if we do that we can help ourselves in the future to read the code. Moreover, if you have a teammate or somebody else that will be going to read your code, it will help all of them. Because if the code is clean, then the productivity will increase because it is easy to understand.

References:

https://medium.com/dekowarehouse-project/clean-code-mengapa-dan-bagaimana-c2be87069beb

--

--