Questions tagged [single-responsibility]
The Single Responsibility Principle states that each module in a system should be responsible for a single feature or functionality, or aggregation of cohesive functionality. Another common way to put it is to say that each module should have only one reason to change.
46
questions
38
votes
9
answers
11k
views
How to determine if a class meets the single responsibility principle?
The Single Responsibility Principle is based on the high cohesion principle. The difference between the two is that a highly cohesive classes features a set of responsibilities that are strongly ...
46
votes
8
answers
13k
views
What is the real responsibility of a class?
I keep wondering if it is legitimate to use verbs that are based on nouns in OOP.
I came across this brilliant article, though I still disagree with the point it makes.
To explain the problem a bit ...
60
votes
8
answers
15k
views
How do I prove or disprove "God objects" are wrong?
Problem Summary:
Long story short, I inherited a code base and a development team I am not allowed to replace and the use of God Objects is a big issue. Going forward, I want to have us re-factor ...
213
votes
16
answers
23k
views
When using the Single Responsibility Principle, what constitutes a "responsibility?"
It seems pretty clear that "Single Responsibility Principle" does not mean "only does one thing." That's what methods are for.
public Interface CustomerCRUD
{
public void Create(Customer ...
12
votes
6
answers
4k
views
What are the practical ways to implement the SRP?
Simply what are the practical techniques people use to check if a class violates the single responsibility principle?
I know that a class should have only one reason to change, but that sentence is ...
14
votes
3
answers
16k
views
Should a method do one thing and be good at it? [duplicate]
"Extract Till You Drop" is someting I've read in Uncle Bob's blog, meaning that a method should do one thing alone be good at it.
What is that one thing? When should you stop extracting methods?
...
8
votes
3
answers
7k
views
What does it mean for a method or a function to do one thing? [duplicate]
What does it mean when you say that a method or a function should do only one thing?
Can it do a few things as long as it has a cohesive name?
Should we avoid routines with the word "and" in them?
...
73
votes
8
answers
25k
views
Single Responsibility Principle - How Can I Avoid Code Fragmentation?
I'm working on a team where the team leader is a virulent advocate of SOLID development principles. However, he lacks a lot of experience in getting complex software out of the door.
We have a ...
17
votes
5
answers
1k
views
Is SRP (Single Responsibility Principle) objective?
Consider two UI designers who want to design "user attractive" designs. "User attraction" is a concept that is not objective and only resides in the mind of designers. Thus designer A could for ...
17
votes
4
answers
10k
views
Is the Single Responsibility Principle applicable to functions?
According to Robert C. Martin, the SRP states that:
There should never be more than one reason for a class to change.
However, in his book Clean Code, chapter 3: Functions, he shows the following ...
7
votes
3
answers
4k
views
How do you apply Single Responsibility principle to a repository
I am trying to apply "SOLID" whenever I can and try to use common sense and avoid a pattern when I see that a pattern is creating more problems than it's trying to solve. I don't want to apply a ...
82
votes
8
answers
18k
views
How can a class have multiple methods without breaking the single responsibility principle
The Single responsibility principle is defined on wikipedia as
The single responsibility principle is a computer programming principle that states that every module, class, or function should have ...
69
votes
12
answers
15k
views
Does a constructor that validates its arguments violate SRP?
I am trying to adhere to the Single Responsibility Principle (SRP) as much as possible and got used to a certain pattern (for the SRP on methods) heavily relying on delegates. I'd like to know if this ...
50
votes
7
answers
12k
views
Managing and organizing the massively increased number of classes after switching to SOLID?
Over the last few years, we have been slowly making the switch over to progressively better written code, a few baby steps at a time. We are finally starting to make the switch over to something that ...
23
votes
6
answers
6k
views
Is logging next to an implementation a SRP violation?
When thinking of agile software development and all the principles (SRP, OCP, ...) I ask myself how to treat logging.
Is logging next to an implementation a SRP violation?
I would say yes because ...
21
votes
12
answers
4k
views
When does the "Do One Thing" paradigm become harmful?
For the sake of argument here's a sample function that prints contents of a given file line-by-line.
Version 1:
void printFile(const string & filePath) {
fstream file(filePath, ios::in);
...
20
votes
5
answers
10k
views
MVC: Does the Controller break the Single Responsibility Principle?
The Single Responsibility Principle states that "a class should have one reason the change".
In the MVC pattern, the Controller's job is to mediate between the View and the Model. It offers an ...
15
votes
7
answers
2k
views
Should I refactor large functions that mostly consist of one regex? [closed]
I just wrote a function that spans approximately 100 lines. Hearing that, you are probably tempted to tell me about single responsibilities and urge me to refactor. This is my gut instinct as well, ...
9
votes
3
answers
3k
views
OO - are large classes acceptable? [duplicate]
Despite many years in IT, I still struggle with OO design. One particular problem I seem to keep ending up with is large classes, often containing many hundreds of lines of code.
The OO world talks a ...
7
votes
1
answer
2k
views
Clean OOP-Design: How to implement single responsibility and no procedural programming
I am currently trying to refactor a piece of C# code that is somewhat procedurally written. I want to make the design clean, object oriented and using classes with single responsibilities.
The code ...
3
votes
4
answers
866
views
Relationship between inheritance and single responsibility principle
Example 1:
Let me assume that I have a base class A. Class B extends Class A (Class B is a derived class).
Can I conclude, Class B doesn't obey single responsibility principle since it uses the ...
0
votes
1
answer
212
views
How to maintain SRP with a chain of async functions?
For code maintainability I understand that it is generally better to have code structured like this:
void abc() {
a();
b();
c();
d();
}
Instead of like:
void abcd() {
a();
bcd(...
21
votes
10
answers
4k
views
Can/should the Single Responsibility Principle be applied to new code?
The principle is defined as modules having one reason to change. My question is, surely these reasons to change are not known until the code actually starts to change?? Pretty much every piece of code ...
20
votes
4
answers
10k
views
What is the difference between Single Responsibility Principle and Separation of Concerns
a)What is the difference between SRP and SoC? Perhaps that SRP is applied at class level, while SoC can be applied at system, subsystem, module, class or function levels.
b) If answer to a) is yes, ...
14
votes
3
answers
3k
views
Is there a conflict between YAGNI and SRP?
I my job, I am tasked with the responsibility of improving the code quality. To meet this responsibility I often pair program with developers and conduct sessions on design principles and design ...
10
votes
2
answers
7k
views
Standard practices for access control (design pattern)
I'm looking at my interface design and I am struggling to decide which is the most "correct" way to implement role-based access control, given a user and a subject that the user would like to access.
...
10
votes
2
answers
880
views
Single-responsibility and custom data types
In the past months I've asked for people here on SE and on other sites offer me some constructive criticism regarding my code. There's one thing that kept popping out almost every time and I still don'...
10
votes
1
answer
986
views
Injecting data processing logic into class
I want find more elegant and appreciate way to inject processors into CommandProcessorDispatcher class. Or it can be another one solution (the goal is separate each command processing logic to ...
8
votes
3
answers
2k
views
Do setters and getters always break the Single-Responsibility Principle?
As we know, The SRP states that every class should have single responsibility and that responsibility must be entirely encapsulated by the class.
But setters and getters do serve another ...
6
votes
2
answers
1k
views
Does increasing the number of classes increase code complexity? [duplicate]
To illustrate the question, let's say we have two programmers of comparable skill that both solve the same problem. The code they turn out has roughly the same lines of code, but one programmer uses 5 ...
5
votes
2
answers
769
views
Single responsibility vs procedural programming [duplicate]
Single responsibility (from SOLID) is like making me create classes with only one public method. But if it's so, it would be possible use static methods, and go back to procedural programming. What's ...
5
votes
3
answers
4k
views
How is DRY principle ( applied at class level ) related to SRP?
In other words, is DRY (don't repeat yourself) applied at a class level a subset of SRP (single responsibilty principle)?
What I mean is that while SRP states that each class should have only a ...
4
votes
3
answers
1k
views
How to apply the single responsibility principle if the program should do a lot of things?
If I need to write a program that
Reads a json file
Converts its data to yaml
Writes it to yaml file
and adhering to the single responsibility principle I create classes like
FileReader
...
4
votes
2
answers
4k
views
Class becoming God Object what pattern to use
I am developing Android app and my MainActivity is becoming God Object.
By the way native Activity class implementation is some sort of God Object already.
The problem is that my activity class is ...
4
votes
1
answer
445
views
Dealing with a large interface
I'm working on a program that solves a certain type of systems of equations. The main data objects are Equation, Variable, Solution. Then I have this interface, which represents all things that I want ...
4
votes
2
answers
758
views
SRP and Getters/Encapsulation
I recently watched this presentation of Robert C. Martin about the Single Responsibility Principle:
https://www.youtube.com/watch?v=Gt0M_OHKhQE
He presents an Employee class with multiple ...
4
votes
4
answers
834
views
Are Compiler Directives an Antipattern?
I'm working on a legacy system that has a helper class that is symbolically linked into many different .Net projects within a solution. The logic is riddled with compiler directives that change it's ...
3
votes
1
answer
575
views
Single Responsibility Principle : Proxy Pattern for refactoring
However, in cases where the tests did not force the separation, and the smells of Rigidity and Fragility become strong, the design should be refactored using
the Facade or Proxy patterns to separate ...
3
votes
1
answer
525
views
Discovering functionality from parallel class hierarchy
I have an abstract syntax tree which I want to compile down to different representations. I am now struggling to arrange the classes in a way that new representations can be added easily.
The easiest ...
2
votes
1
answer
743
views
Design patterns to avoiding breaking the SRP while performing heavy data logging
A class that performs both computations and data logging* seems to have at least two responsibilities. Given a system for which the specifications require heavy data logging, what kind of design ...
2
votes
4
answers
2k
views
Separating data and behavior in Java
I am in the process of refining my code to adhere more to my current understanding of the Single Responsibility Principle (SRP). Originally, I had a class called Animal that had a set of methods and ...
1
vote
4
answers
383
views
Is there multiple definitions for responsibility of class?
In the book UML 2 and the Unified Process from Arlow and Neustadt has been told:
Analysis classes should have 3 to 5 responsibilities
But as you know we have the SRP that tells us something else!!
...
1
vote
1
answer
523
views
In which layer should I load views on a Clean(ish) architecture?
I am trying to organize my architecture following the clean architecture while using some common practices/frameworks. I'm writing in PHP, but have no problem with other languages. The flow of my app ...
1
vote
2
answers
253
views
Object oriented Classes and single responsibility [duplicate]
I'm reading a book that explain that it is a good thing that classes have a single responsibility, that is, that they do a single thing.
I can understand how to implement this in some cases I ...
0
votes
2
answers
654
views
Should a class validate argument using external method/service with unit testing?
I have a class that runs as a service and returns html code of a website when supplied with a URL.
The code:
public interface IHtmlDownloader
{
IWebProxy Proxy { get; set; }
string UserAgent ...
0
votes
3
answers
2k
views
Doesn’t active record violate SRP and OCP?
I have watched the active record classes in our project grow into large, do-it-all classes. When you need anything about a user, for example, you should go to the Person class. While this makes ...