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.

Filter by
Sorted by
Tagged with
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 ...
user1483278's user avatar
  • 1,131
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 ...
Pierre Arlaud's user avatar
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 ...
honestduane's user avatar
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 ...
Robert Harvey's user avatar
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 ...
Songo's user avatar
  • 6,588
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? ...
KyelJmD's user avatar
  • 981
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? ...
Piovezan's user avatar
  • 471
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 ...
Dean Chalk's user avatar
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 ...
Saeed Neamati's user avatar
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 ...
Enrique's user avatar
  • 281
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 ...
developer9969's user avatar
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 ...
Goose's user avatar
  • 1,878
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 ...
Paul Kertscher's user avatar
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 ...
JD Davis's user avatar
  • 1,367
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 ...
Aitch's user avatar
  • 729
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); ...
Petr's user avatar
  • 331
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 ...
Aviv Cohn's user avatar
  • 21.4k
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, ...
DudeOnRock's user avatar
  • 1,079
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 ...
Andrew Stephens's user avatar
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 ...
Ashwani Mehlem's user avatar
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 ...
Dharma Teja Bandaru's user avatar
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(...
Evan La Fontaine's user avatar
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 ...
SeeNoWeevil's user avatar
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, ...
user1483278's user avatar
  • 1,131
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 ...
Nachiappan Kumarappan's user avatar
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. ...
kwah's user avatar
  • 232
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'...
Paul's user avatar
  • 2,174
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 ...
Boo's user avatar
  • 217
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 ...
Yang's user avatar
  • 257
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 ...
mortalapeman's user avatar
  • 1,613
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 ...
SerG's user avatar
  • 482
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 ...
bckpwrld's user avatar
  • 439
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 ...
Bob's user avatar
  • 151
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 ...
CROSP's user avatar
  • 1,558
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 ...
Patrik Bak's user avatar
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 ...
Kevin Wittek's user avatar
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 ...
sisdog's user avatar
  • 141
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 ...
q126y's user avatar
  • 1,713
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 ...
amon's user avatar
  • 134k
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 ...
Kazark's user avatar
  • 1,810
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 ...
MagerBlutooth's user avatar
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!! ...
flexibleHammer's user avatar
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 ...
Erick de Azevedo Lima's user avatar
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 ...
Robert Brax's user avatar
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 ...
moskalak's user avatar
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 ...
Parham Doustdar's user avatar