I feel clean code is just three things, in order of importance: (big statement, I know :))
- Reuseable (Don't Repeat Yourself - DRY)
- Readable
- Performant
Readable code can be subdivided into, again in order of importance:
- Understandable (Seperation of Concerns - SoC, high cohesion, loose coupling, comments)
- Readability by convention.
- Easy to read syntax/formatting/naming
Motivation:
I believe reusing code (and thus making code reuseable) is a severely underestimated principle. Most people understand the benefits of it, but don't always apply it. To sum up why I feel reuseable code is the most important in one sentence: "Reuseable code can be achieved by making code readable and performant."
To explain why performance is placed last, I quote Bill Harlan: "It is easier to optimize correct code than to correct optimized code."
I feel code should be easy to read, but being able to understand it easily is more important. You always have a learning process to go through when using a new language or API. Some introduced keywords might be vague at first, but very concise once you understand them by reading documentation (comments).
Rant:
Warning, my following opinion of the Clean Code book could be somewhat of a rant, but I'll try to stay constructive. :)
I finished reading Clean Code recently, and found it disappointing in some (but definitly not all) areas. Originally, I wanted to buy The Pragmatic Programmer, which wasn't available, but I figured Clean Code to be similar.
The problem I see with it, is it is written too much from the perspective of Test Driven Development. It explains concrete fixed steps how to refactor code, and somewhere along I feel the intent is lost, or ill-explained. To compare with the priorities I stated earlier, Uncle Bob places 'Testable' on 1, whereas I would probably place it at 3.
To give a concrete example, in his book he specifies the following sample as clean code:
private String getPageNameOrDefault(Request request, String defaultPageName)
{
String pageName = request.getResource();
if (StringUtil.isBlank(pageName))
pageName = defaultPageName;
return pageName;
}
Where I would prefer the following:
String pageName = request.getResource();
pageName = StringUtil.isBlank(pageName) ? defaultPageName : pageName;
This is a concrete example why I find reuseability to be more important than readability. The ternary operator isn't readable per se, but once you understand it, it is definitly powerful.
If you like to read more about this rant (and others), I wrote a blog post about it.