Lessons in object-oriented design
July 7, 2020•390 words
I will be re-reading the following quotes from Sandi Metz's Practical Object-Oriented Design every morning before I start coding:
Object-oriented design is about managing dependencies. It is a set of coding techniques that arrange dependencies such that objects can tolerate change. In the absence of design, unmanaged dependencies wreak havoc because objects know too much about one another. Changing one object forces change upon its collaborators, which in turn forces change upon its collaborators, ad infinitum. A seemingly insignificant enhancement can cause damage that radiates outward in overlapping concentric circles, ultimately leaving no code untouched.
This makes sense of why dependency injection containers exist, the principle of inversion of control, and how to split up classes into the right responsibilities.
Asserting that code should be easy to change is akin to stating that children should be polite; the statement is impossible to disagree with, yet it in no way helps a parent raise an agreeable child. The idea of easy is too broad; you need concrete definitions of easiness and specific criteria by which to judge code. If you define easy to change as
- changes have no unexpected side effects,
- small changes in requirements require correspondingly small changes in code,
- existing code is easy to reuse, and
- the easiest way to make a change is to add code that in itself is easy to change,
then the code you write should have the following qualities. Code should be
- Transparent The consequences of change should be obvious in the code that is changing and in distant code that relies upon it.
- Reasonable The cost of any change should be proportional to the benefits the change achieves.
- Usable Existing code should be usable in new and unexpected contexts.
- Exemplary The code itself should encourage those who change it to perpetuate these qualities.
Code that is Transparent, Reasonable, Usable, and Exemplary (TRUE) not only meets today’s needs but can also be changed to meet the needs of the future. The first step in creating code that is TRUE is to ensure that each class has a single, well-defined responsibility.
Optimising for unknown future requirements seems like a great way to go about writing code that's usable today as well.