<<Clean Code>> Quotes: 12. Emergence
Getting Clean via Emergent Design
According to Kent (Beck), a design is “simple” if it follows these rules:
- Runs all the tests
- Contains no duplication
- Expresses the intent of the programmer
- Minimizes the number of classes and methods
Simple Design Rule 1: Runs All the Tests
First and foremost, a design must produce as system that acts as intended.
A system that is comprehensively tested and passes all of its tests all of the time is a testable system. Systems that aren’t testable aren’t verifiable. Arguably, a system that cannot be verified should never be deployed.
Tight coupling makes it difficult to write tests.
Simple Design Rule 2–4: Refactoring
Once we have tests, we are empowered to keep our code and classes clean. We do this by incrementally refactoring the code.
The fact that we have these tests eliminates the fear that cleaning up the code will break it!
No Duplication
Duplication is the primary enemy of a well-designed system. It represents additional work, additional risk, and additional unnecessary complexity.
Duplication:
- Lines of code that look exactly alike
- Lines of code that are similar can often be massaged to look even more alike
- duplication of implementation
This “reuse in the small” can cause system complexity to shrink dramatically.
The TEMPLATE METHOD pattern is a common technique for removing higher-level duplication.
Expressive
The majority of the cost of a software project is in long-term maintenance.
Therefore, code should clearly express the intent of its author, The clearer the author can make the code, the less time others will have to spend understanding it. This will reduce defects and shrink the cost of maintenance.
You can express yourself by choosing good names.
You can also express yourself by keeping your functions and classes small.
You can also express yourself by using standard nomenclature. Design patterns, for example, are largely about communication and expressiveness.
Well-written unit tests are also expressive.
But the most important way to be expressive is to try.
Minimal Classes and Methods
High class and method counts are sometimes the result of pointless dogmatism.
Our goal is to keep our overall system small while we are also keeping our functions and classes small.