SOLID, coupling and cohesion
Learn what coupling, cohesion and the five SOLID principles really say, and when following them blindly makes code harder to change.
Updated since you finished it. We changed this lesson after you marked it done. Your progress is kept.
What broke
This story is made up, but both halves are common. Kiran's team runs the checkout service of a shoe store. The Checkout class creates an SmtpMailer and calls it to send order emails. Refunds and sign-ups do the same.
Then the company moved to an HTTP email API. The new client took different arguments. Kiran had to edit checkout, refunds and sign-up. Each change needed new tests and a new deploy. Worse, the checkout tests had always opened a real SMTP connection, so they were slow and often failed in CI.
A new tech lead said, "We need SOLID." The team added an interface in front of every class. OrderService got IOrderService. PriceCalculator got IPriceCalculator. Six months later there were 60 interfaces, each with exactly one implementation.
Now a new developer had to jump through two files for every call. The vendor problem was solved, but adding a field to an order touched more files than before.
The first design had too much coupling. The second added layers that hid nothing. Both made change slow.
The idea in simple words
In the deep modules lesson, you learned to hide each hard decision behind a small interface. This lesson gives you the older words for the same goal, and five popular rules built on them.
In 1974, Stevens, Myers and Constantine published "Structured design" in the IBM Systems Journal. They called coupling "the measure of the strength of association established by a connection from one module to another". 1 Strong coupling makes a module harder to understand, change or fix on its own. They advised "the weakest possible coupling between modules". 1
Fewer connections also mean fewer paths for changes and errors to spread. The paper calls these "ripple" effects. 1 Kiran's vendor change was one.
The second idea is cohesion. The paper called it cohesiveness, measured by "binding": how strongly the parts inside one module belong together. 1 You lower coupling in two ways at once: fewer links between modules, and stronger links inside each one. 1 Their weakest kind is "coincidental" binding, where the parts have no real relationship. 1
The key idea. Put things that change together in one place. Keep things that change for different reasons apart. SOLID gives five more specific rules that serve this goal, and each one can be overdone.
SOLID names five principles that Robert C. Martin teaches: single responsibility, open-closed, Liskov substitution, interface segregation and dependency inversion. 5 He links his single responsibility principle back to Parnas and to Constantine's coupling and cohesion. 3
How it works
Step through the vendor change in two designs. Use Next and Back, or the ← and → keys.
- Design A. Checkout creates an SmtpMailer and calls it. The business code depends on a low-level detail, and it knows how SMTP works.
- The company moves to an HTTP email API. The new client takes different arguments, so Checkout must be edited, tested and deployed again.
- Design B. The orders module defines a small interface, OrderNotifier, in its own words. Checkout calls only that. SmtpNotifier, a small class that wraps the mailer, implements it. The arrow from the detail now points up.
- The vendor changes. A new class, EmailApiNotifier, wraps the new client and implements the same interface. Only one line that wires the objects together changes. Checkout does not.
- The same seam lets a test pass in a fake notifier. You can test Checkout without a mail server.
S: single responsibility
Martin states SRP as "each software module should have one and only one reason to change". 3 He says the reasons are people: "It is people who request changes." 3 His short form is: "Gather together the things that change for the same reasons. Separate those things that change for different reasons." 3
class Invoice { // finance owns the maths
total(): Money { /* ... */ }
}
class InvoicePdf { // design owns the layout
render(inv: Invoice): Buffer { /* ... */ }
}
class InvoiceStore { // the database team owns the tables
save(inv: Invoice): void { /* ... */ }
}Misreading: "a class should do one thing", so every method gets its own class. That is not how Martin defines it. Ousterhout calls small classes taken to the extreme classitis. 10 In their debate, he says the related "One Thing" rule for methods "is simply wrong in many cases". 11
O: open-closed
Martin quotes Bertrand Meyer's 1988 book: a module is open if it is "available for extension", and closed if it is "available for use by other modules". 4 Martin reads this as plugins: high-level policy stays apart from low-level detail. 4
interface PriceRule { apply(cart: Cart, price: number): number; }
function finalPrice(cart: Cart, rules: PriceRule[]): number {
return rules.reduce((p, rule) => rule.apply(cart, p), cart.subtotal);
}A new discount is a new PriceRule. You do not edit finalPrice.
Misreading: "never edit old code", or "add plug-in points everywhere". You cannot guess every future change. An extension point that nobody uses is cost with no benefit.
L: Liskov substitution
Liskov and Wing argue that subtyping is about behaviour. Objects of the subtype should behave like the supertype "as far as anyone or any program using supertype objects can tell." 2 Any property you can prove about type T should also hold for a subtype S. 2
Their example: a stack and a queue can both have put and get. The type checker may accept either. But code written for a stack is unlikely to work with a queue. 2
In the code below, PaymentGateway promises that refund returns the money to the customer.
interface PaymentGateway {
charge(orderId: string, cents: number): Promise<void>;
refund(orderId: string, cents: number): Promise<void>; // returns the money
}
class GiftCardGateway implements PaymentGateway {
async charge(orderId: string, cents: number) { /* ... */ }
async refund(orderId: string, cents: number) {
throw new Error("not supported"); // compiles, breaks callers
}
}Misreading: "the subclass compiles, so it is fine". Type checking catches only a small part of correctness. 2 Martin's short form: a program that uses an interface "must not be confused by an implementation of that interface." 5
I: interface segregation
Martin's line: "Keep interfaces small so that users don't end up depending on things they don't need." 5 The gift card fix is two small interfaces:
interface Charger { charge(orderId: string, cents: number): Promise<void>; }
interface Refunder { refund(orderId: string, cents: number): Promise<void>; }
class CardGateway implements Charger, Refunder { /* ... */ }
class GiftCardGateway implements Charger { /* ... */ }Misreading: one method per interface, everywhere. Split by what each caller needs, not by method count.
D: dependency inversion
Martin's line: "High level modules should not depend upon low level details." 5 Schuchert adds: capture low-level dependencies in domain-relevant abstractions. 7
// orders/order-notifier.ts: owned by the orders module
export interface OrderNotifier { orderPlaced(order: Order): Promise<void>; }
export class Checkout {
constructor(private notifier: OrderNotifier) {}
async place(order: Order) { /* save ... */ await this.notifier.orderPlaced(order); }
}
// main.ts: the only line that knows the vendor
const checkout = new Checkout(new EmailApiNotifier(apiClient));The interface speaks the orders language, orderPlaced, not sendSmtp(host, port, mime).
Misreading: "dependency inversion means a DI framework". The last line is dependency injection. Schuchert puts it this way: "DI is about how one object acquires a dependency." In his summary, "DI is about wiring, IoC is about direction, and DIP is about shape." 7 You can inject a concrete class and gain nothing. He also warns that "all abstractions have a cost". 7
Connascence: finer words for coupling
Connascence is "a taxonomy for different types of coupling". 8 It judges each case by strength, degree (how many things are involved) and locality (how far apart they are). 8 For example, connascence of position means two parts "must agree on the order of values". Reading fields by name instead makes the coupling weaker. 9
The critiques
Dan North's CUPID started as a light-hearted challenge to SOLID. At first he wanted to replace each principle with something more useful. 6 He gives an example: splitting a report's content from its format, as SRP is often explained, "creates artificial seams" when both change together. 6 He prefers properties to principles: "Principles are like rules: you are either compliant or you are not." 6
In 2020, Martin answered a reader who cited North's views on SOLID. He wrote that North's slides "completely miss the point" of SRP. 5 He called North's "Just write simple code" good advice. But he added that "simplicity requires disciplines guided by principles." 5 Read both, then judge each rule by one test: does it make the next real change smaller?
Trade-offs
| Choice | Easy to change? | Cost | Use it when |
|---|---|---|---|
| Call a concrete class directly | Yes , while it is stable. | Vendor or I/O changes ripple into callers. | The detail is stable and in your own code. |
| Interface owned by the caller, injected | Yes Swap details in one wiring line. | One more name. A small design effort. | The detail is a vendor, I/O, or needs a fake in tests. |
| Interface for every class | No Each change touches two files. | Shallow layers, more jumping between files. | Rarely. Remove ones with one implementation and no test fake. |
| Split a class by reason to change | Yes Each group's change stays in one place. | More classes to name. | Different people ask for different changes. |
| Split until each class does "one thing" | No Code that changes together is spread out. | Classitis. | Rarely. |
At work: how a senior uses this
- For a class under review, ask: "Who asks for changes to this?" Two different answers suggest a split. One answer suggests keeping it together.
- Before adding an interface, name the second implementation or the test fake. If you cannot, wait.
- Put interfaces next to the code that uses them, in its language.
OrderNotifierbelongs to orders, not to the email package. - In code review, look for
throw new Error("not supported")in an implementation. It usually means a broken substitution or an interface that is too big. See reviewing code. - Keep the wiring of concrete classes in one place, such as
main. Then a vendor change is one line. - When a change touches many files, write down which coupling caused it. Add it to your tech debt list.
With AI
What to ask the agent
"List every place where business code creates or calls this vendor client directly. For each, show the arguments that are specific to the vendor."
"Find interfaces in this repo with exactly one implementation. For each, say whether any test uses a fake for it."
"Find classes that implement an interface but throw 'not supported' or do nothing in some methods. Explain which callers could break."
What to check yourself
- The agent did not add an interface and an
Implclass for every class to "follow SOLID". - New interfaces use the caller's words, not the vendor's method names.
- A suggested split groups code by who asks for changes, not by method count.
- Behaviour did not change. Run the tests before and after. The testing strategy lesson shows where fakes help.
Check yourself
Your progress is saved only in this browser. Export it to keep it safe.
You have finished 5 lessons. Your progress lives only in this browser. Export a copy so you don’t lose it.
Go deeper
Show 11 sources and related lessons
- W. P. Stevens, G. J. Myers and L. L. Constantine, “Structured design”, IBM Systems Journal 13(2), 1974 (reprinted in IBM Systems Journal 38(2&3), 1999, pp. 231–256; DOI 10.1147/sj.382.0231) (opens in a new tab)
The link is an unofficial copy of the 1999 reprint; the official version is behind the DOI. The paper that explained coupling and cohesiveness ("binding"), with the scale of binding and the warning about ripple effects. - Barbara H. Liskov and Jeannette M. Wing, “A Behavioral Notion of Subtyping”, ACM Transactions on Programming Languages and Systems 16(6), November 1994, pp. 1811–1841 (opens in a new tab)
Pages 1811–1812 cover behaviour "as far as anyone … can tell", the subtype requirement, and the stack-versus-queue example. - Robert C. Martin, “The Single Responsibility Principle”, The Clean Code Blog (8 May 2014) (opens in a new tab)
SRP explained as "one reason to change", where the reasons are people, with links back to Parnas and Constantine. - Robert C. Martin, “The Open Closed Principle”, The Clean Code Blog (12 May 2014) (opens in a new tab)
Bertrand Meyer's 1988 wording of open and closed modules, and Martin's plugin reading of it. - Robert C. Martin, “Solid Relevance”, The Clean Code Blog (18 October 2020) (opens in a new tab)
One short line for each principle, and Martin's reply to Dan North's criticism. - Dan North, “CUPID: for joyful coding” (10 February 2022) (opens in a new tab)
Why North wanted to replace SOLID, his critique of SRP, and five properties of code instead of rules. - Brett L. Schuchert, “DIP in the Wild”, martinfowler.com (May 2013) (opens in a new tab)
Dependency inversion versus dependency injection, domain-relevant abstractions, and the cost of every abstraction. - connascence.io, “What is Connascence?” (opens in a new tab)
Connascence as a vocabulary for kinds of coupling, judged by strength, degree and locality. - connascence.io, “Connascence of Position” (opens in a new tab)
Two functions that must agree on the order of values in a list, and how naming the fields weakens the coupling. - John Ousterhout, “Modular Design”, CS 190 lecture notes, Stanford (Winter 2018) (opens in a new tab)
Deep and shallow classes, and classitis. - John Ousterhout and Robert C. Martin, “A Philosophy of Software Design vs Clean Code” (GitHub, 2024–2025) (opens in a new tab)
A written debate on how far to split code, including the "One Thing" rule.
Case studies: Shopify: Choosing a modular monolith over microservices
Related: Complexity and deep modules · Testing strategy that scales · Managing technical debt · Reviewing code written by humans and AI
Before this: complexity and deep modules. Next: testing strategy, where these seams make tests fast. Design patterns that still matter and clean and hexagonal architecture build on this one, and so does low-level and object-oriented design. Related: managing technical debt and reviewing code written by humans and AI.