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
0
votes
1 answer

How to define the EF connection string only once and re-use it in other projects?

I am building a web application that makes use of Entity Framework. I have moved the Entity Model and generated classes to a separate project, because it will be used by more than one consumer. But when I try to run the application, Entity…
0
votes
2 answers

Single Responsibility Principle: Should I separate my bibliography class in Reader, Writer and Container class?

cowboy coder needs some help from SO-veterans: I have a given application that uses a bibliography which is read from a file (in reality, it can be different files but let's assume a single file only). I build a new application that should use the…
0
votes
1 answer

Is it bad practice for a controllers actions to have differing authorization levels?

We are developing a website and we have a controller that handles CRUD for a model e.g Country. Only the Administrator is allowed to perform CRUD operations. However we also want the controller to provide a JSON select list of entities to populate a…
GraemeMiller
  • 11,973
  • 8
  • 57
  • 111
-1
votes
1 answer

SOLID priciples - Single Responsible is just about composition or also dependency inversion?

I have seen examples online about the Single Responsible principle. They always use IoC/DI as an example. They move the code from class A to class B and pass class B as reference. See code below: class B {} class A { b; // reference used …
Znar
  • 193
  • 12
-1
votes
2 answers

Does this violate single responsibility principle

Say i have a class like so: class Config { private $configA; private $configB; private $configC; private $configD; public function getConfigA(): string { return $this->confiA; } //... } In one sense this class has a…
-1
votes
3 answers

Refactoring: When to stop Refactoring?

I am so obsessively OC at dividing a class or instance method and move it in its own class, and many times a simple "hello world" method will be divided into many classes. like for example, class MainProgram def hello_world puts "hello world" …
-2
votes
1 answer

Is breaking my class into multiple classes, the right thing to do?

I'm working with Unity and C# and I have a canvas that is about a competition with several milestones. So I have to show a leaderboard and a slider for the player's progress in the competition and the rewards they'v got since now and also some other…
-2
votes
1 answer

How to design Class which uses multiple service classes?

I am working on a springboot project where I am working on a feature which require multiple steps (almost like a algorithm). To follow the SRP I have made a separate service class to take care of each step in the algorithm. Now I have situation…
-3
votes
1 answer

What is the meaning of breaking code while implementing new features?

I was reading a text about SOLID principles and Open/Close principle and it said that : "The main idea of this principle is to keep existing code from breaking when you implement new features" what it means by breaking code?
-5
votes
1 answer

Violating Single Responsibility Principle and static methods

I have the following strange question. Lets say we have a class BlackJackGame and this class contains the BlackJackGame algorithm for electing a winner. Same class though contains the main method for starting the game. This main method in some sense…
Alexander Petrov
  • 9,204
  • 31
  • 70
1 2 3
29
30