Clean Code: A Handbook of Agile Software Craftsmanship
Clean Code is one of the top 5 recommended reading for Software Developers. The Book is divided into 3 parts.
- principles, practices of writing clean code.
- Case studies (take days to complete).
- Nutshell containing heuristics.
There are two parts to learning:
Learning to Clean Code is hard work. It requires practice.
As the mess builds, the productivity of the team continues to decrease, asymptotically approaching zero.
The time spent on reading vs writing code is 10:1. So, code should be written as clean as possible.
use intention revealing names
The name should tell you why it exists.
Eg. of bad name.
Eg of good name.
Don’t name variable with reserved keyword like
accountList unless it is really a
Make meaningful distinctions.
Programmer make mistakes when they write variable names solely to satisfy the compiler or interpreter. Don’t add noise words or random numbers to make distinct variables.
Number series naming
it is non-informative as it provides no context.
Noise words are words like
table which adds no additional meaning to the variable name.
Use distinct names
Names such as
moneyAccount are difficult to distinguish and creates ambiguity.
Use pronounceable Names
Use names like
generationTimestamp instead of
Use searchable Names
Use searchable names instead of constants.
MAX_CLASSES_PER_STUDENT instead of 7
Avoid member prefixes.
Your class should be small enough to that you don’t need them.
String description instead of
Interface and implementation
IShapeFactory provides too much information.
Its better to name it as
Its better to encode the implementation with either
Avoid mental Mapping
Use single letters i,j,k when the scope is very small. This adds the overhead of mapping the letters to its original context.
Clarity is the king.
Class name should be a noun or noun phrase like
Avoid words like
Processor in the name of a class.
Methods should have verb or verb phrase.
When constructors are overloaded
Complex fulcrumPoint = Complex.FromRealNumber(23.0);
is better than
Complex fulcrumPoint = new Complex(23.0);
so, use static methods for this.
Make constructor private to enforce this.
Don’t use slangs or cultural jokes
Pick one word per concept
- The words
getimply same meaning.
- Pick one word per abstract concept.
use solution Domain Names
- Use names from computer science.
- Most programmer would know about JobQueue or AccountVisitor implying visitor pattern.
Use problem domain names
- Separating solution and problem domain concepts is part of the job of a good programmer.
- Use names from problem domain when there is no programming name for what you are doing.