Clean Code: Take care of your program and your friends

“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

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.

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.

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

Implementation of Meaningful Names

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.

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

Register Code
Change Password Code

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.

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

Only Write Comment If It’s Necessary

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.

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.

Unit Tests

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.

Exception Handling

Benefits of Clean code

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.

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.

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.

References

An undergraduate Computer Science Student at University of Indonesia

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store