Coupling is how much software components depend on each other. Think of LEGO bricks: loosely coupled components connect through simple, predictable interfaces; tightly coupled ones are more like bricks glued together. Changing one can force changes in its neighbors. section 3.3 section 2.4.3
Coupling looks between modules; cohesion looks at how well the parts inside a module fit together. Both help assess separation of concerns. Loose coupling limits reliance on internal details, making components easier to change, test, and replace. section 3.3 section 3.11
A report generator and its database
A report generator that creates its own live database connection is hard to test: the test cannot choose what data comes back. Instead, pass the connection into the constructor. A test can supply a fake connection with predictable data, while production supplies a real one. section 2.4.3
Listing 2.5 "After": Loosely Coupled (reportGenerator.js)
class ReportGeneratorAfter { #A
constructor(dbConnection) { #A
// The dependency is "injected" into the constructor. #A
this.dbConnection = dbConnection; #A
} #A
generate(reportName) {
const data = this.dbConnection.getData(reportName);
return `Report '${reportName}' generated with ${data.length} rows.`;
}
}See how the refactored generator receives its database connection instead of creating one. section 2.4.3
Dependency injection supplies a dependency from outside; dependency inversion goes further by making high-level and low-level modules depend on a shared abstraction rather than on concrete details. Stable contracts can also let new behavior plug in without rewriting existing code. section 2.4.3 section 3.4.5 section 3.4.2
To spot tight coupling, inspect two classes that interact frequently. Ask:
- Does the client make a few useful high-level calls, or many low-level calls to finish one job? section 3.3.1
- Does it reach into nested properties or repeat a required sequence of calls? Those details may be leaking out of the service. section 3.3.1
- If the service changes internally, how much client code would need to change? Also ask how easily the client can be unit-tested. section 3.3.1 section 3.4.6
Coupling matters across services, too. Separate deployments do not guarantee independence: a shared database can make several services break after a schema change, while synchronous calls can tie one service’s availability to another’s. A queue can move a noncritical task, such as printing a shipping label, out of the immediate order flow. section 12.2.4 section 6.8.4
Aim flexibility where change is likely, rather than treating every dependency as equally risky. Check the assumptions, testability, and likely impact of change before choosing a boundary. section 3.4.6





















