ArticleslgStudy

computer science

Single-responsibility principle

Single-responsibility principle is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Single-responsibility principle rather than just read about it. In short: In computer programming, the single-responsibility principle (SRP) is a software design maxim stating that "a module should be responsible to one, and only one, actor." The term "actor" refers to a group (consisting of one or more stakeholders or users) that requires a change in the module. Software engineer Robert C.

Key takeaways

  • Single-responsibility principle belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Single-responsibility principle to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Single-responsibility principle from memory before moving on to harder problems.

Reference excerpt

In computer programming, the single-responsibility principle (SRP) is a software design maxim stating that "a module should be responsible to one, and only one, actor." The term "actor" refers to a group (consisting of one or more stakeholders or users) that requires a change in the module. Software engineer Robert C. Martin, the originator of the term, expresses the principle as such: "A class should have only one reason to change". Because of confusion around the word "reason", he later clarified his meaning in a blog post titled "The Single Responsibility Principle", in which he mentioned Separation of Concerns and stated that "Another wording for the Single Responsibility Principle is: Gather together the things that change for the same reasons. Separate those things that change for different reasons." In some of his talks, he also argues that the principle is, in particular, about roles or actors. For example, while they might be the same person, the role of an accountant is different from a database administrator. Hence, each module should be responsible for each role.

History The term was introduced by Robert C. Martin in his article "The Principles of OOD" as part of his Principles of Object Oriented Design, made popular by his 2003 book Agile Software Development, Principles, Patterns, and Practices. Martin described it as being based on the principle of cohesion, as described by Tom DeMarco in his book Structured Analysis and System Specification, and Meilir Page-Jones in The Practical Guide to Structured Systems Design. In 2014 Martin published a blog post titled "The Single Responsibility Principle" with a goal to clarify what was meant by the phrase "reason for change."

Example Martin defines a responsibility as a reason to change, and concludes that a class or module should have one, and only one, reason to be changed (e.g. rewritten). As an example, consider a module that compiles and prints a report. Imagine such a module can be changed for two reasons. First, the content of the report could change. Second, the format of the report could change. These two things change for different causes. The single responsibility principle says that these two aspects of the problem are really two separate responsibilities, and should, therefore, be in separate classes or modules. It would be a bad design to couple two things that change for different reasons at different times. The reason it is important to keep a class focused on a single concern is that it makes the class more robust. Continuing with the foregoing example, if there is a change to the report compilation process, there is a greater danger that the printing code will break if it is part of the same class.

See also Chain-of-responsibility pattern Coupling (computer programming) GRASP (object-oriented design) Information hiding SOLID—the "S" in "SOLID" represents the single responsibility principle Separation of concerns

References

External links "The Principles of OOD" by Robert Martin "The Single Responsibility Principle (2007)" by Robert Martin "The Single Responsibility Principle (2014)" by Robert Martin

Worked examples

Example 1 — a first encounter with Single-responsibility principle

Start with the simplest possible case. Write down what Single-responsibility principle claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Single-responsibility principle before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Single-responsibility principle ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Single-responsibility principle

In research
Single-responsibility principle appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Single-responsibility principle in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Single-responsibility principle is common in secondary-school and first-year university syllabi. It links to neighbouring topics Programming principles, Software design, so understanding it makes those chapters shorter.
In everyday life
Look for Single-responsibility principle outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Single-responsibility principle” →

How to study Single-responsibility principle in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Single-responsibility principle means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Single-responsibility principle out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Single-responsibility principle in simple terms?

In computer programming, the single-responsibility principle (SRP) is a software design maxim stating that "a module should be responsible to one, and only one, actor." The term "actor" refers to a group (consisting of one or more stakeholders or users) that requires a change in the module. Softwar…

Why does Single-responsibility principle matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Single-responsibility principle?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Single-responsibility principle.

Tags

  • Programming principles
  • Software design

Keep exploring