Coupling answers the question “how tightly are the modules connected”: does the payment module know about the internal tables of the orders module, or does it talk to it only through an interface? The looser the coupling, the more independently each module can be changed and tested. Loose coupling is almost always the goal; tight coupling is a source of fragility.
Types of coupling
In the classification of structured design, kinds of coupling are distinguished by what exactly connects the modules, from the least desirable to the most useful:
| Kind | How the modules are connected |
|---|---|
| content coupling | one module reaches into the internal data or code of another |
| common-environment coupling | modules read and write the same global variables or files |
| control coupling | one module passes flags to another that determine its behaviour |
| stamp coupling | modules exchange a whole structure although only individual fields are needed |
| data coupling | modules exchange only small amounts of data through parameters |
Why loose coupling is good
- a change inside one module does not drag edits through its neighbours — one module is changed, not a whole chain;
- a module can be tested on its own by substituting test doubles for its neighbours;
- teams work in parallel: the contract between modules remains stable, while each team changes the internals on its own;
- a module is easier to extract into a separate service when the need arises.
Tight coupling shows itself as a chain reaction of edits: to add a single field to an order, you have to change the orders module, the payment module, the delivery module and the shared database schema. Fowler and Lewis state this as a principle of microservices: services should be as loosely coupled and as highly cohesive as possible.
How coupling is reduced
- communicate through narrow interfaces and contracts rather than shared tables and global variables;
- pass data, not control flags;
- accept narrow structures instead of handing a neighbour the whole object;
- share data between modules through events or copies of the data rather than direct access to the database.
Origin of the term
The term was established by the 1974 article “Structured Design” by Wayne Stevens, Glenford Myers and Larry Constantine on structured design; the same article introduced the paired notion of cohesion. Since then the pair “coupling and cohesion” has been the standard language for discussing modularity.
Sources
the term is written from these pagesVerified 8 Nov