If you open your computer to start on something new and find that you still have dozens of windows and documents open from the thing you were working on yesterday, that will slow you down.
Just like if you go to the kitchen to make dinner and find that the kitchen is clogged up with old dishes and leftovers from yesterday- What a perfect analogy used by Henrik Kniberg for a messy code in his
Seriously, codes are developed like potboilers these days. The treasured adage “Prevention is better than cure” can be applied to the maintenance of codes as well. Instead of spending on the maintenance of software, it’s better to avoid that maintenance. The code has to be so intelligible that it needs least maintenance.
Programmers & managers usually forget that poor quality code leads to higher maintenance cost. In the book “Clean Code, A Handbook of Agile Software Craftsmanship”, Robert Martin says, in software, 80% or more of what we do is strangely called “maintenance”: the act of repair. Out of this 80% of maintenance, some 40% to 60% is devoted to understanding the software to be modified, as is noted in the Guide to the Software Engineering Body of Knowledge. So just imagine, by writing clean code you can save around 40-60% of your maintenance cost.
Not writing clean code produces code smells which eventually becomes Technical Debt for your project. You may be able to release the product early but the technical debt will force you to pay the interest later in terms of maintenance. What will you prefer? To pay principal upfront by refactoring your code into clean code or paying huge maintenance cost?
Apart from maintenance cost there are many other side effects of bad code, like
- Loss of Productivity – Programmers will be spending much time in understanding the code rather than actually implementing new features.
- Fragile & Inseparable Systems– Fragile systems are those which can break by smallest of change in it. It might happen that you fix a small thing in one module which breaks something else in another module.
- Lack of re-usability– Lack of reusable code will lead to more efforts and in turn lack of productivity.
- Frustrated programmers– Spending lot of time in understanding the code leads to frustration. That is the reason why many programmers don’t want to work on maintenance projects.
Clean code is not easy to write, initially it will take some efforts to write it. The best way is to write bad code first supported by good set of tests, identify the code smells and then refactor to solve the code smells. When you have a good set of tests, it is really easy to refactor your code. As a programmer, it is really important to know whether my code is clean or bad. So best way to find the bad code is to look for code smells. Code smells indicate that something may be wrong with your code. You can find out different code smells at Click here now
I hope, I have you all with me now and I am able to sell you the importance of Clean Code. So let’s try to understand what is Clean Code? It is the art of writing code which is easily readable, understandable & modifiable by other developers. In short, clean code is writing code for humans.
Here is a very interesting quote by Martin Fowler:
“Any fool can write code that a computer can understand.
Good programmers write code that humans can understand.”
Clean Code also builds trust among team members as all can understand each other’s code. This in turn builds a strong team.
Strong Team has always been the most important feature of any industry. In fact, the success of any business directly hinges on the interdependence of its team members. Clean Code practices are really helpful and must be followed by the entire team. Some programmers might not follow these practices because of multiple reasons, like laziness, ignorance, lack of knowledge, strict deadlines etc. But as a Manager, you will have to ensure that your team follows these practices. Here are some of the techniques that will help your team follow clean code.
- Code Analysis Tools – Nowadays there are different tools available for checking code quality. Visual Studio helps you find out code complexity, duplicate code and different code paths. Plugins like Resharper for Visual Studio makes it more intelligent and in turn makes refactoring very easy. Some of my friends are using SonarSource and they find it really useful. TeamCity also has Code Quality Tool which checks for duplicate codes, code coverage and performance issues etc.
- Code Reviews or Peer Reviews– Code reviews is another way of improving code quality. You can perform frequent code reviews or you can make it compulsory for developer to get his / her code reviewed before the work is called done.
- Pair Programming– Pair Programming is a technique of continuous review & learning. By making pairs of programmers who have knowledge of clean code and who don’t, we can make sure that the code produced is a clean code.
- Ask developer to follow Boy’s Scott rule – The rule says that “Always leave the campground cleaner than you found it.” If you find a mess on the ground, you clean it up regardless of who might have made the mess. You intentionally improve the environment for the next group of campers. The same rule applies in software, “Always leave the code cleaner than you found it”.
What about certain situations where you need to quickly deliver something. Well to answer these, let me quote Joel Helbing, from his blog “What’s So Wrong with Quick and Dirty Code?”. He says:
- Could we not save a minute or so by not tying our shoes in the morning?
- Isn’t the kitchen more efficient if we never bother washing the dishes?
- Who has time for car maintenance? Isn’t it faster if we just proceed to work riding on bare rims, with black smoke belching forth from under the hood?
There are multiple practices for achieving clean code and improve the code quality. Each practice is a separate topic of discussion which we will be discussing in upcoming blog. Below is the list of clean code practices for your reference.
- Meaningful Names
- Don’t Repeat Yourself
- Single Responsibility Principle
- Command Query Separation
- Open Close Principle
- Liskov’s Substitution Principle
- Interface Segregation
- Dependency Inversion
- Law of Demeter
- TDD/ Unit Testing
Remember, writing clean code is not easy, it only comes through practice. But as Joel Helbling says, “So what do you do if you’re new to software development, and clean code doesn’t come naturally? It seems clear to me that until we know enough to take the time to tie our shoes, we are not ready for the Big Time.”
So what are your thoughts on clean code? Do share your feedback below.
Editor's Note: The blog was originally posted on August 2015 and recently updated on March 2023 for accuracy.