This article is made as a part of the assignment for the PPL 2021 Course.
“Any fool can write code that a computer can understand. Good programmers write code that humans can understand.” ― Martin Fowler
Have you read my other article about the Agile Method using Scrum Framework? Please read my story below because what we will discuss in this article is implementing the Agile Method.
What is a Clean Code?
Suppose you have read my article or at least have an understanding of how Agile works. You should know that one of the agile manifestoes is working software over comprehensive documentation. But, because there is no documentation doesn’t mean you can code as you like, compile it, and then forget its existence.
When using Agile, another developer will read your code to implement a new feature most of the time. Now imagine if your code is unreadable and there is no documentation at all. It will be hard to implement a new feature, and the new feature can make your old code obsolete, or the other developer can make the same function that you have done, but they don’t know it because they can’t read your code.
The rule of thumb to write a good clean code is to make your code like a story where other people can read it and understand it easily without any comment or documentation provided. Now that we know a basic understanding of what clean code is, let’s get into it.
So how can you decide if a program is cleaned or not, or how to make one? Below are characteristics of a program with clean code implementation.
One of the best clean code implementations is when your code has a low coupling. Low coupling means that if there are any program errors, only the problematic part of your program you have to concern yourself with.
In other words, your program's function is not dependent on one another, so change in one function will not make other functions need some adjustment. Have a look at the illustration below to understand low coupling better.
Example of low coupled implementation :
- Add hasil pranen on manajemen_produk app
- Showing the added product
As you can see above the code for adding the hasil_panen commodity and then showing it, is separated on a different app and different function. So if the product is now showing, or the product shown is not correct. We can easily debug it. It also supports the high cohesion theory which will be explained below.
Another implementation of a good clean code is high cohesion. High cohesion means that class on your code only work on one specific job and one only. For example, you want to build a calculator program, and you want your program to have the ability to read an input, calculate (subtraction and addition for now), and then print the result. Then a good cohesion program should have four separate class or function. One function for reading input, one function for subtraction, one faction for addition, and one more function to print the output.
So you can say that low coupling goes hand in hand with high cohesion. An excellent low coupling program usually also implement a high cohesion design.
Start small, small start
Another implementation of clean code is to write a small class or function, one at a time. This also supports high cohesion and low coupling principle as your class or function get smaller. It can only handle one functionality at a time.
DRY — Don’t repeat yourself!
Sometimes repetition is not a good thing, mainly when you write a code. Try to avoid writing the same class or function twice at all cost. Not only this will waste more memory, but it will also make the other developer confused when they read your code. As to why there are two functions or classes that execute the same problem, and is it possible to delete one function, or is there any specific reason for that function to be written twice.
Use a program conventional naming
Take a look example at an example code below.
a = 10#encouraged
initial_land_area = 10
As you can see, if you wrote your variable, function, and class where the name itself is not self-explanatory. It will be hard for you and the other developer to know what exactly that piece of code does. Usually, every programming language has its own naming convention. For python you can check
The rule of thumb for the variable, app and function on python is snake case (as seen above). And pascal case for a class name. For example
#Class Nameclass ThisIsClass():#function namedef this_is_a_function():#variable namethis_is_variable = 1
PEP 8 -- Style Guide for Python Code
The official home of the Python Programming Language
Consistent in all you do
Consistency is an excellent principle to implement in your code. Now that you know the excellent naming convention of your selected programming language. Follow that rule diligently and adherently in all function, variable, and class of your program.
Less comment, more code
In a clean code implementation, less comment is better. Suppose your program can be understood without any comment to explain it. It means that you have implemented a good clean code. For example:
def a(int b): #function for printing final land value
print(b + 10) #land value + increment--------------------------------------------------------------def final_land_value(int initial_land_value)
computed_land_value = initial_land_value + 10
Why is comment discouraged in clean code? Suppose that another developer will implement a new feature that will need some adjustment in your code one day. There is a huge chance that the new developer will forget to change your comment, thus make a comment and the function not synced one another and raising a new problem in the future.
Gupta, S. (2020, April 7). How to write clean code? Lessons learnt from “The Clean Code” — Robert C. Martin. Medium. https://medium.com/mindorks/how-to-write-clean-code-lessons-learnt-from-the-clean-code-robert-c-martin-9ffc7aef870c
Bina Nusantara. (2014, April 12). Clean Code. School of Information Systems. https://sis.binus.ac.id/2014/04/12/clean-code/