Saturday 12 October 2013

Characteristics of clean code

Writing clean code matters because it not only makes your programming life much easier, also help others to understand it when you are not available. Think about a nice song you hear recently. Think about the happiness while listening that song. Reading clean code is like this. It will fill you with joy because the author of this code has taken lot of care in writing it. He not only thinks about the computer that executes it, but also thinks about the future reader of his code. He has made his intention clear in his code. Martin Fowler has rightly said about clean code in his refactoring article : "Any damn fool can write code that a computer can understand, the trick is to write code that humans can understand."

So what are the characteristics of clean code?

Clean code is simple. It should be easy to read. Anyone can understand what the code does. It has a story to tell. It is not ambiguous. It is easy to maintain and enhance.

Clean code does one thing and does it well. Trying to do too many things is a sign of bad code. It makes your code messy. Clean code does not do this.

Clean code does not have duplication. Duplication increases the chance of introducing bugs in your code. When you have duplicated code and you change one thing then you need to change others as well. There is strong possibility you may forget to do this. Focus on reusability and remember DRY (Don't Repeat Yourself) principle: "Every piece of knowledge must have a single, unambiguous, authoritative representation within a system."

Clean code must have a good set of tests. Code without unit and acceptance test is unclean. So write your tests. Tests are your live documentation. Care must be taken in writing tests.

These are the main characteristics of clean code. But there are many other attributes that make your code clean such as consistent and meaningful naming, good error handling, completeness as per requirement etc.

Bad code increases time to complete your product. Every change in your bad code breaks many other parts of the code. Productivity of the team continues to decrease due to bad code. It hurts your nerve and project is bound to fail. So try to avoid writing bad code and write clean code.

No comments:

Post a Comment