Types of Coupling
Coupling is the degree of interdependence between two modules[¹]
You should generally seek to cut coupling as much as possible in software systems. It is one of the primary causes of complexity when developing software, though you’ll see it’s impossible to drop.
Pathological Coupling
Class reaches inside another type and reads (or, perish the thought, changes) its instance variables.
You are pathological and deserve the pain this will cause you.
Global Coupling
Two classes rely on shared global data, a Singleton or a class variable.
When many test files depend on global factory definitions, a change to anyone can ripple through the system.
Control Coupling
You pass in a flag that tells a method what to do.
Remember `save(false)` in ActiveRecord? That boolean argument caused control coupling. An upgrade caused every implementation out there to change to `save(validate: false`
Passing `validate: false` into `save` does not reduce the Coupling. It’s disguised better.
Good OOP lets objects decide what to do based on their state. Objects determine what an object should do from outside it.
Data Coupling
You call a method and pass it a parameter that doesn’t affect its control flow.
This is still coupling, but we’re starting to reach the kind that isn’t so bad. Sometimes you need parameters!
If you wanted to remove all Coupling, you wouldn’t be able to pass data between objects.
Message Coupling
You call a method on an object and send no parameters.
You’re coupled to the name of the message but not any hint of its implementation. This is the loosest type of Coupling and should be your goal.
Notice that this makes methods that take no arguments better than methods that take one (and so on).
Keep an eye out for the nasty types of Coupling in your code, and see if you can refactor it into something further down the ladder.
[¹]: Meilir Page-Jones (1980): The practical guide to structured systems design(https://www.amazon.com/Practical-Guide-Structured-Systems-Design/dp/0136907695/ref=sr_1_2?dchild=1&keywords=the+practical+guide+to+structured+system+design&qid=1627058206&sr=8-2)