⛴ Coupling and Cohesion
Intro
The story starts few weeks ago when on codereview my colleague asked me to inline class if possible. I, being a SOLID dev, was shocked: why would I increase coupling, break SRP, and thoughts like this.
I did as requested, and found out that nothing get worth: the class was still perfectly testable, the state was also doable, nothing has changed, but I get one less class and one less level of nesting.
I was restless and looking for reasons and consequences, here is my research.
Research
Coupling is:
…the degree of interdependence between software modules; a measure of how closely connected two routines or modules are…
Low coupling is considered a good thing, because the parts of the system are independent from each other, reusable and have a little of state and easily testable.
Cohesion is:
cohesion refers to the degree to which the elements inside a module belong together.
High cohesion is considered something good, because all the related things are grouped together and it simplifies the life of a new developer and improves understandability of the system.
As you can see, these 2 things are opposite.
Outcome
- More senior developer is — better devself keeps the balance between them.
- Smaller class is better — is a wrong statement
- Applying principles in a smart way — is a good thing
- Extracting a class should deliver value: reducing state, improving testability, etc.
- Extracting class just for sake of principle — is a wrong approach