|We often hear software developers talking about "clean code", but what exactly does it mean? How can code be clean? Presumably you don't take your code to the shower for a wash, so how can code be said to be clean?
There are a great number of articles and blogs on this subject, and there is even a book on the subject, so clean code is obviously an important topic in the world of software development. During interviews it is something that I try to look for (by asking the candidate to write some code, by checking their on-line source code repositories e.g. Github etc).
There is no single definition of the term, as it encompasses many different elements, and there will also likely be disagreement and discussion as to its definition amongst software developers. This list is therefore my own definition. If you have any comments please feel free to leave a comment below.
Clean code should adhere to the following constraints:
- Variables, functions, events and properties should have meaningful names so that their intention is unambiguous and obvious e.g. calling a variable x or y is not allowed.
- Proper use of comments. I know that this single statement will have opened up a can of worms by itself (software developers do love their religious wars) but what that means is to leave comments where they clarify and add useful information to aid in the understanding of a piece of code. How you define that is up to you (but please do so consistently).
- Low coupling and high cohesion. I won't explain what these are, as I am assuming that the reader already has an understanding of software development principles. All functions within an application should be independent and not rely on any global state. All dependencies should be passed via parameters and they should perform one and only one task, and perform that task well.
- Clean code is simple and well engineered as opposed to complicated and over-engineered. Whilst it is great to understand design patterns, object mappers etc. you probably wouldn't use them in a simple console app for importing your payroll data. Understanding good design is important, but of greater importance is knowing "when" to use them. Cluttering a very simple application with design patterns, object mappers and the like just makes the whole thing difficult to comprehend.
- Good levels of re-factoring. If you see the same code repeated all over the place then it is clearly breaking the DRY principle (Don't Repeat Yourself). If you find yourself writing the same code more than twice (although some would keep this at one) then you need to refactor the code into a function and call that function instead.
- Another developer should be able to quickly (relatively speaking) understand what the code is doing and make modifications. If the code is so incomprensible that it takes your average developer an increasingly long time to make sense of the code, then clearly the code is not intuitive. In the same way that we strive to make our user-interfaces as intuitive and self evident as possible, so we ought to strive to reach the same ideals with our code.
Writing good code takes practice and skill. It takes effort and attention to detail. This final quote from Michael Feathers best sums up the definition of clean code.
Quote:Clean code is a code that is written by someone who cares.
"There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult." - C.A.R. Hoare