liveBook · Manning

Single responsibility principle

As explained in The Art of Code

The Single Responsibility Principle is an object-oriented design principle stating that a class should have only one reason to change.

The Single Responsibility Principle (SRP) says that a class should have only one reason to change. The same idea works at method level: keep low-level actions tightly scoped while allowing higher-level methods to coordinate broader processes. The useful boundary is not a fixed line count; it is the point where a method remains meaningful, focused, and easy to understand. section 2.3.5 section 9.1.2

Name a complex decision

A dense condition such as `user.isActive() && !user.isExpired() && user.hasRights()` can obscure the surrounding story. Extracting it as `isEligible(user)` gives the decision a name and lets the higher-level code communicate intent. The extraction is worthwhile when it reduces cognitive load; extracting every short line is not. section 2.3.5

Use judgment when deciding where to split a method:

  • Split methods that become too long, but do not shrink code merely to meet a line-count target. section 2.3.5
  • Keep action-level responsibilities narrow, such as validating one field; allow higher-level methods to cover broader workflows. section 2.3.5
  • Extract cognitively heavy Boolean conditions or deep nesting into clearly named methods. section 2.3.5

How The Art of Code covers it

How deep it goes
Defines the principle and develops practical boundaries, but stops short of prescribing a universal responsibility size.
Examples
Uses method length, single-field validation, user registration, dense eligibility conditions, and deeply nested permission logic.
What it assumes
Assumes object-oriented design familiarity while clarifying responsibility from action-level methods through broader processes.
What you can do after
Apply SRP through judgment by splitting code when responsibility, length, or cognitive complexity obscures its story.
How it connects
Connects SRP to SOLID principles, method extraction, readability, cognitive load, and maintainable object-oriented design.

Also explained in

Compare how 7 books explain single responsibility principle →

Related concepts

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