“Clean code always looks like it was written by someone who cares.”
- Robert C. Martin, Clean Code: A Handbook of Agile Software Craftsmanship
All programmers can code. But not every programmer cares what he writes. Programmers who care about their programmers always try to write good code. Good code? What is it like? Clean code. In this article, I will discuss clean code and its implementation.
Definition of Clean Code
The definition of clean code is very broad. If I have to summarize all the definitions of clean code, clean code is code that is easy to understand, develop, and modify.
Easy to understand means that the code written is easy to read by both the coder and others. By writing code that is easy to understand, other people do not need to guess the code being written. This can reduce the chance of misunderstanding.
Meanwhile, easy to develop and modify means that the code written is easy to extend and easy to refactor. It’s common for developers to re-read old code for further enhancement. Implementing clean code can help developers re-read their code and make enhancements from existing code. Additionally, implementing clean code can help developers with debugging.
Characteristics of Clean Code
Focus: Single Responsibility Principle (SRP)
The Single Responsibility Principle should be applied at every level of abstractions in the code that we write, such as methods, classes, packages, and modules so that the code being written focuses on solving specific problems and does not contain anything else that has nothing to do with the problem you want to solve.
Simple: Keep it Simple (KISS)
Code should be designed and implemented as simply as possible. Code that is too complex has a higher tendency of errors and is difficult to read and maintain.
Not Redundant: Don’t Repeat Yourself (DRY)
If there is an implementation of the same code in several different places, it is better if the code is wrapped in a function so that the part that needs the code only needs to call the function. This can make it easier for developers when there is a change to the same code. By wrapping it in a function, if there is a change the developers only need to make changes to the function.
Code should be easy to test. This helps to maintain the baseline behavior of our code so that we can confidently enhance the code without breaking anything.
Implementation of Clean Code
Variable, functions, classes, arguments, etc should represent their intentions such as “what it does, what it does, why it exists and how it is used”. It makes it easier for us and other developers to read and understand what we write.
The following is an example of implementing meaningful names in the code that I wrote
The function that I wrote is called see_bucketlist because this function is used to view the user’s bucket list. In it, I declare variables named user, bucket lists, and bucketlists_serializer. As you can guess, the user variable will hold the user object from the request. Meanwhile, the bucket lists variable will store the array containing the user’s bucket lists. Then the bucket lists will be serialized and stored in a variable named bucketlists_serializer.
Single Responsibility Code
Implementation of clean code that is multi-tasked should be avoided because it will confuse other developers to find out the intent of the code. This can make it difficult for us to make enhancements to the program. Therefore, the code written should be small and focus on one job only.
The following is an example of implementing the single responsibility principle in the code that I wrote
Even though changing passwords and registers will involve the same database, namely User, I still separate classes for Register and Change Password because they both have different purposes. It can be seen from the implementation that the register only focuses on entering user data into the database. Meanwhile, change password focuses on updating user password data.
Writing comments as necessary
Modern programming languages generally use English syntax which is human-readable enough to minimize the use of comments. Comments can be written for codes that require further explanation.
The following is an example of writing a comment in the code that I wrote
I wrote a comment to explain that in the set_password function, the password will be hashed so that the password data is not stored in raw.
Write Unit Tests
Having a unit test can convince the developers that the changes made do not change the expected behavior. Additionally, tests can aid in the debugging process. To know more about the benefits of writing tests, please see my article on TDD.
Exception handling is a mechanism for dealing with runtime errors. Exceptions must be overcome so they will not cause unexpected behavior. The following is an example of Exception Handling to deal with the possibility that the Object does not exist.
Benefits of Clean code
Easy to maintain
Program development is cyclical, which means the program will always evolve. While the program is still being developed, there may be changes that require easy maintenance. Clean code can help developers make changes and maintenance. Code that is applied clean code must be easy to understand when re-read.
Easy to debug
When an error occurs, of course, we want to immediately resolve the error so that the program can run as it should. Implementing clean code can help the debugging process. The tests that we write can be the first reference in finding bugs. In addition, the code that implements clean code also ensures human readability, simplicity, and single responsibility. This is of course very helpful in the debugging process.
Easy to transfer
Usually, a program is developed collaboratively, which means that the code we write is very likely to be continued by other programmers to be developed. Therefore, implementing clean code is very helpful in ensuring that the code we write can be understood by others.