Vol. I a living index of named laws · est. mmxxvi no ads · no account
The Law Tomeindex of named laws

The Single Responsibility Principle

also known as — SRP, One reason to change
"There should never be more than one reason for a class to change."
In plain English

What does The Single Responsibility Principle mean?

A unit of code should answer to one constituency. If a tax rule and a page layout can each force you to edit the same class, that class serves two masters and every change made for one risks breaking the other.

Reliability

Rated Heuristic: a dependable rule of thumb, not a proven theorem. The other 224.

Lineage
2000COINED
How it works

How does The Single Responsibility Principle work?

The unit of the principle is the reason, not the line count, and a reason traces back to a person or role who can ask for a change. When two reasons live in one module, an edit made on behalf of one of them must be tested against the other, because they share state, share the file and share the deployment. The coupling is organisational before it is technical: two groups now have a shared point of contention that neither chose. Splitting on the axis of who asks for changes puts each group's edits in a place where nobody else's work can be broken by them.

Where you'll see it

What are examples of The Single Responsibility Principle?

The report that also calculates Martin's own framing: a class computing pay and formatting a payslip answers to accounting and to presentation. A layout tweak now needs the payroll tests run.
The model that persists itself An object holding business rules and its own SQL changes both when the rules change and when the schema does: two teams, two release cadences, one file.
The utility bucket A Helpers class accretes functions with nothing in common. It has as many reasons to change as it has functions, and every one of them is a merge conflict.
Why it matters

Why does The Single Responsibility Principle matter?

It is the first and most quoted of the SOLID principles, and the one that gives 'this class does too much' a testable meaning: not too many lines, but too many people who can demand a change to it. Read that way it is a claim about organisations as much as about code, and it is the bridge between design advice and Conway's Law.

Working with it

How do you apply The Single Responsibility Principle?

  • Name the person behind each reason — if you can name two different roles who would ask for changes to a module, it has two responsibilities. If you can only invent hypothetical ones, it has one.
  • Split on change, not on nouns — the axis that matters is what varies together over time, which is discovered from the history of the file rather than from a class diagram.
  • Let cohesion pull back — the principle is a counterweight to modules that do too much, not a mandate for modules that do almost nothing. Two things that always change together should not be separated to satisfy it.
Where it breaks down

What are the limits of The Single Responsibility Principle?

'One reason to change' cannot be decided from the code, only from a forecast of what the organisation will ask for next, and that forecast is exactly what nobody has. The principle also has no natural stopping point: taken to its limit it produces a class per method, a design its own advocates reject but the wording does not exclude. Martin later reformulated it around actors rather than reasons partly because the original phrasing was so widely read as a licence to fragment.

What it doesn't say

What are common misconceptions about The Single Responsibility Principle?

It does not say a class should do only one thing, and it is not a rule about size. A large class with one reason to change satisfies it; a three-line class serving two masters does not. It is also not an argument against classes that coordinate several collaborators — coordination can itself be the single responsibility.

Is it real?

Is The Single Responsibility Principle real?

Real as a rule of thumb, not as a theorem. The Single Responsibility Principle is rated Heuristic here: dependable enough to plan with, and with no proof behind it.

Rated from two sources. 225 of the 1,116 entries carry this rating. How the scale works.

Occasionally printedin printed books: one of 240 names in its band, of 903 measured
25 of 69entries in software & systems carry the same rating

The best-known laws are the least likely to rest on measurement; the full verdict on The Single Responsibility Principle.

Origin

Where did The Single Responsibility Principle come from?

Stated by Robert C. Martin, who collected it with four others in his 2000 paper "Design Principles and Design Patterns" and made it the S of the SOLID set. Martin credits the underlying idea of cohesion to earlier structured-design work, and has since restated the principle in terms of actors: gather together the things that change for the same reason, and separate those that change for different reasons.

How the name spread

When did people start saying “The Single Responsibility Principle”?

1800coined 20002019
How often “Single Responsibility Principle” appears in printed English, which takes hold around 2004 and peaks around 2018. Coined 2000 and in print by about 2004, unusually close together. This still charts the phrase rather than the idea. Source: Google Books Ngrams.
Sources

Sources & further reading

  1. 1Martin, R. C. (2000). "Design Principles and Design Patterns." — the principle as first collected, with the payroll example.primary
  2. 2"SOLID." Wikipedia — the canonical wording, 'there should never be more than one reason for a class to change', and its place in the set.secondary

Everything above traces to web.archive.org and en.wikipedia.org, two sources in all, one of them the original publication rather than an account of it. Nothing is written from memory, and the method is written down. Spot an error or a better source? Suggest a fix.

What we checked

  • 1,116entries in the tomeListed one by one on the browse page.
  • 1,116of them citing a sourceOpen any entry: its sources sit at the foot of the page.
  • 2,489source references in allEach is named, and linked wherever the source is online.