Sunday 5 September 2010

"Dirty Code" - who cleans the mess? and how?

During early years my stint as developer, if I found some part of code dirty or messy - I would delete the entire code and rewrite. Of course, not realizing how much extra effort has been wasted in this process. But somehow, I use to get satisfaction of seeing pieces of code readable and understandable.

We, when producing code, produce much of good code alongwith some of dirty code, patches, code non-conforming to standards, designs and unused codes. Over period dirty code heaps up, producing, which Robert C Martin calls Legacy Code.

In this article we will discuss typically how dirty codes are produced, what are the ways to prevent it and what are the ways to clean it. But one thing we need to keep in mind, once the code is dirty, it's difficult to clean it again due to lack of time,budget, willingness and the shear size of clean-up.

The art of producing dirty code

When we write (or change) code , along with clean code we also leave some dirty code. We write code when we add some feature or fix a bug or try to improve the design or optimize resource usage. The structure of the code changes, some functionalities are added/removed from the product. And these gates of introducing changes allows us room for introducing code garbage. These garbage coding could be from inexperienced developer (i.e. developer trying to implement code in a language for first time), lack of conformity of standards in a project and most importantly , lack of sufficient code coverage. We as developers have to understand the producing good quality code is as good as producing good quality test cases.

The art of clean-up

Now that we have produced some garbage code we need to learn how to clean that mess. It is slow, tedious but given sufficient attention and effort it's worthy. To clean-up first task would be to identify garbage code. It could be partly automatic and partly manual. Certain development tools integrated with IDEs and continuous integrations tools can detect anomalies related to code coverage and code reviews.
It would be good to quickly add some tests missing sufficient code coverage. Then it would be good to fix automatic code review comments.
Now comes rather harder parts. Manual code review to find sections of code non-conforming to standards and design principles. The best way is to find some small sections and identify patterns of non-conformance. Then to do refactoring immediately. It's good to do this exercise as "pilot" for a team and slowly roll-out for entire development teams.

Finally, prevention is better than cure...

Now the easier part (or the tougher one as you see). Following simple development guidelines improves code quality significantly. When you change any code (even a single line) ensure it is unit tested. The code is readable without any external documentation. Check that your development IDE has correctly installed plugins for automatic code reviews and rightly configured. Make sure you make your build fail for any code review issues.
If you are new to he project or new to the technology , get your code reviewed by someone more experienced. Well, if you are the first one in your project working in this technology, try to lay out standards. A little precaution would help us to see the garbage clean and appreciate our clean code.


Must read

Book : Clean Code

No comments:

Post a Comment