Questions tagged [single-responsibility-principle]

For questions about the Single Responsibility Principle in object-oriented programming, one of the SOLID principles coined by Robert C. Martin. It states that a module should have only one reason to change.

Robert Martin was inspired by David Parnas, Edsger Dijkstra (who coined the term Separation of Concerns) and Larry Constantine (who coined the terms Coupling and Cohesion). During the late 1990s, Martin consolidated their ideas into the Single Responsibility Principle.

Martin's definition of the SRP evolved to become,

Gather together the things that change for the same reasons. Separate those things that change for different reasons.

In keeping with the previous authors who inspired him, Martin notes that,

...this is just another way to define cohesion and coupling.

In contrast with the principle, the SRP is focused on people rather than functionality.

As you think about this principle, remember that the reasons for change are people. It is people who request changes. And you don’t want to confuse those people, or yourself, by mixing together the code that many different people care about for different reasons.

The SRP later became the first of Martin's .

445 questions
15
votes
4 answers

Understanding the practical benefits of using the Single Responsibility Principle

I'm trying to understand the SRP but, whilst I understand the reasoning behind how to apply it, I'm not really seeing the benefit of doing so. Consider this example, taken from Robert Martin's SRP PDF: interface IModem { void Dial(string…
John H
  • 14,422
  • 4
  • 41
  • 74
15
votes
3 answers

Does implementing multiple interfaces violate Single Responsibility Principle?

From Wikipedia: Single responsibility principle states that every class should have a single responsibility, and that responsibility should be entirely encapsulated by the class. Does that mean implementing multiple interfaces violates this…
14
votes
2 answers

Rails Model to use API instead of Database

I'm refactoring a rails app that currently uses a database and ActiveRecord so that instead of of the database, it uses a JSON API (Sinatra). What would be the best way to refactor the models? The API will return all the same information as the…
Greg Olsen
  • 912
  • 1
  • 9
  • 14
12
votes
4 answers

string.IsNullOrEmpty(myString) or string.IsNullOrWhiteSpace(myString) is not violating SRP Rule?

As the question shows, As we are using string functions like IsNullOrEmpty or IsNullOrWhiteSpace as the name of functions shows , these are doing more than one job , is it not a violation of SRP? rather should it not be string.isValid(Enum…
TalentTuner
  • 17,262
  • 5
  • 38
  • 63
12
votes
2 answers

Does the traditional use of the controller in MVC lead to a violation of the Single Responsibility Principle?

Wikipedia describes the Single Responsibility Principle this way: The Single Responsibility Principle states that every object should have a single responsibility, and that responsibility should be entirely encapsulated by the class. All its…
12
votes
6 answers

Single Responsibility Principle: do all public methods in a class have to use all class dependencies?

Say I have a class that looks like the following: internal class SomeClass { IDependency _someDependency; ... internal string SomeFunctionality_MakesUseofIDependency() { ... } } And then I want to add functionality that…
jpoh
  • 4,536
  • 4
  • 35
  • 60
11
votes
2 answers

Understanding the Single Responsibility Principle

I am quite confused on how to determine if a single method has one responsibility being done just like the following code from the book Clean Code public Money calculatePay(Employee e) throws InvalidEmployeeType { switch (e.type) { …
anathema
  • 947
  • 2
  • 15
  • 27
11
votes
1 answer

Does Singleton violate the single responsibility principle?

According to the Single responsibility principle: Every class should have responsibility over a single part of the functionality provided by the software A singleton prevents the creation of multiple instances of a class, providing a global…
lysergic-acid
  • 19,570
  • 21
  • 109
  • 218
10
votes
4 answers

ASP.NET MVC: Authorization inside an Action - Suggested Patterns or this is a smell?

I have an ASP.NET MVC application using Authorization Attributes on Controllers and Actions. This has been working well but a new wrinkle has shown up. Object: Shipment Roles: Shipping, Accounting, General User The Shipment moves through a…
10
votes
3 answers

How Single Responsibility Principle relates to anemic/rich domain model?

Currently in doing some code review of stuff taken over from another team and have one doubt about applying SRP and its relation to anemic or rich domain model (as defined by Martin Fowler). Rich domain model concept is to have intelligent object…
10
votes
7 answers

How to avoid creating huge classes

Stackoverflow users, How do you keep yourself from creating large classes with large bodied methods. When deadlines are tight, you end up trying to hack things together and it ends up being a mess that needs refactoring. One way is to start with…
Pieter Germishuys
  • 4,828
  • 1
  • 26
  • 34
10
votes
7 answers

Inheritance and responsibility

When I read about inheritance I'm always confused about a certain example. Usually there's an example similar to the example below. class Shape { public: Shape() {} virtual ~Shape () {} virtual void Draw() = 0; }; class Cube : public…
Tek
  • 2,888
  • 5
  • 45
  • 73
9
votes
5 answers

Single Responsibility Principle in OOP

In my application design, I usually map objects to the important tables in the database. The objects then handle everything relating to that data (including linkage tables). So I for example have built an Activity object, with properties like name…
Rijk
  • 11,032
  • 3
  • 30
  • 45
9
votes
2 answers

Doesn't having more than 1 method break the Single Responsibility Principle?

I am quite confused with the Single Responsibility Principle. The Principle states that there should only be one reason for the class to change. The problem which I am facing is, any change to a method or any logic change in doing things would…
9
votes
5 answers

Write programs that do one thing and do it well

I can grasp the part "do one thing" via encapsulation, Dependency Injection, Principle of Least Knowledge, and You Ain't Gonna Need It; but how do I understand the second part "do it well?" An example given was the notion of completeness, given in…
kirakun
  • 2,770
  • 1
  • 25
  • 41
1
2
3
29 30