liveBook · Manning

Coupling

As explained in Grokking Software Architecture

Coupling is the degree of dependency between software components, with tight coupling requiring extensive knowledge of another component's operations or internal structure.

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

How Grokking Software Architecture covers it

How deep it goes
Defines tight and loose coupling, demonstrates a dependency-injection refactor, and provides a four-question audit of class interactions.
Examples
Uses LEGO bricks, a database-backed report generator, and a synchronous order-processing chain to illustrate coupling.
What you can do after
Diagnose tight coupling by inspecting class interactions, change impact, and difficulty substituting dependencies in tests.
How it connects
Connects coupling to cohesion, separation of concerns, dependency injection, dependency inversion, and synchronous communication.

Also explained in

Compare how 22 books explain coupling →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking Software Architecture. The section links point to the parts of the book it draws on.