
Clean Code (Photo Credit: Dev Community)
Story
Arman and Rakib are classmates. They both complete their readings and homework regularly. But their approach to finishing the homework is quite different. Although they finish their homework on time, the quality of work is quite different.
For example, Arman is a more qualified student than Rakib. Because he does his homework in the most organized way. He always tries to make notes of his homework so that he can reuse the notes and its knowledge later when this will be applicable. He keeps his homework simple, short, and sweet. That’s why his homework can be felt, understood, and create a happy feeling for others who read his notes and homework later.
On the other hand, Rakib is the full opposite of Arman. He does his homework only for doing it, not for reuse later, and not considering others. He does not make his homework short and sweet so others cannot understand it easily. So when someone tries to understand his works, they do not feel cool, and well, they run their nose. They feel very bad and always say Rakib’s homework is ugly.
Clean Code
In simple words, clean code is something that can be felt. Code that makes you feel better and happy when you read it is clean code. According to Robert C. Martin (aka Uncle Bob), “Clean code is code that is easy to understand and easy to change.”
In the above story, Arman is a clean coder, and his homework is an example of clean code. Because he keeps his works simple and easy to understand for others.
Bad/Smelly Code
Bad/smelly code is the opposite of clean code. That is, code that is hard to understand and hard to change is bad/smelly code. In other words, code that makes you feel ugly and unwell is bad code. As bad code smells bad, so when someone reads this, they wrinkle their nose.
By definition, “A bad code is when a programmer or coder does a program to get things done faster without thinking much about future changes and ignoring the possibility of other developers touching the code.”
In the above example, Rakib is a bad coder, and his work is an example of bad/smelly code. Because his work is hard to understand for others and makes them feel unwell.
Why is Clean Code Important?
- Readability: Clean code is easy to read, which means that anyone - including your future self - can understand it quickly. This reduces the time required to grasp the code’s functionality, leading to faster development and debugging. For example, Arman works’ is easy to read.
- Maintainability: Code is read more often than it is written. When you write clean code, it becomes easier to maintain and extend the application over time. This is crucial in the software development lifecycle, where projects often evolve and grow. For example, Arman works’ can maintain for a long time and easy to use before exam.
- Collaboration: Clean code encourages collaboration. When your code is clean and well-organized, other team members can work on it effectively. This makes it easier to divide tasks and work on different parts of the codebase simultaneously. For example, Everybody in the class prefer Arman works’ as notes than Rakib works'.
- Bug Reduction: Clean code reduces the likelihood of introducing bugs. Code that is difficult to understand is more prone to errors during modifications or enhancements. For example, Arman works’ is more accurate and error free than Rakib’s.
- Efficiency: Clean code is efficient code. It typically runs faster and uses fewer resources because it avoids unnecessary operations and complexity.
Final Word
According to Martin Fowler, “Any fool can write code that a computer can understand. Good programmers write code that humans can understand.”
Insha Allah, in the upcoming parts, I will try to mention how we can write clean code, reason when our code becomes bad, and how can we remove our code smell. Until than, may Allah keep you healthy and happy.