Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

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.

3
votes
1answer
136 views

Way to hide logic behind class for better readability of method and refactor class to follow SRP

I have an algorithm to create a version for an entity and then I save that version against below 2 entity: 1) Variant 2) Category interface IEntityVersion { string GetVersion(); } public class ...
2
votes
1answer
162 views

Should gravity sources pull objects or objects pull themselves to gravity?

I'm doing a project in unity where I turn off the general gravity and implement it myself. What I'm trying to acheive is to have GameObjects be affected by several gravity sources on every tick. I got ...
2
votes
2answers
120 views

How to avoid duplication of types in MVVM

I'm learning the MVVM pattern and something that comes up often is a duplication of data-types. Say I have a Person datatype. Intuitively I want it to look like this: public class Person { ...
12
votes
3answers
2k views

Working through the single responsibility principle (SRP) in Python when calls are expensive

Some base points: Python method calls are "expensive" due to its interpreted nature. In theory, if your code is simple enough, breaking down Python code has negative impact besides readability and ...
3
votes
5answers
426 views

Problem understanding the Single Responsibility Principle

The SRP seens fairly reasonable when you first look at it. Every class should have one reason to change. Every class should take care of one thing. Right. But let's see this class: Employee { ...
0
votes
1answer
124 views

Is a Factory calling a Factory an anti-pattern or an extension of the Single Responsibility Principle?

I'm busy playing around with some training material to help teach some of the juniors the factory pattern. Whilst writing the example, I can't help but think this might be an anti-pattern. Lets say ...
40
votes
10answers
5k views

Applicability of Single Responsibility Principle

I recently came by a seemingly trivial architectural problem. I had a simple repository in my code that was called like this (code is in C#): var user = /* create user somehow */; _userRepository.Add(...
2
votes
1answer
148 views

User class: How do I handle different user “subjects” and still keep my single responsability principal?

I have a User class, this class handles things like: login (UserAuthenticateModel) Handle user sessions: user login tokens, fetching user from session if set in session, ect' (UserSessionsModel) ...
0
votes
3answers
179 views

Designing a class whose dependancies require “starting”/initializing step

I have Facade class which requires an Endpoint instance to function (it uses it in most of the functions). Endpoint is attached to Platform that is passed in Endpoint's constructor and has to be ...
1
vote
3answers
249 views

Responsibility of a Factory

I am trying to refactor a huge chunk of our software, and it requires to set up a factory. However, I am not sure where lies its responsibility. (PS: The code below is shown simply as an illustration ...
2
votes
3answers
339 views

Confusion on Single Responsibility Principle (SRP) with modem example?

I am currently reading through Robert Martins book "Agile Software Development" book and I am struggling to see how his modem example provides any benefit. He says that he has an interface that ...
4
votes
1answer
152 views

Should I comply with SRP when designing relational tables?

As far as I know, the single responsibility principle concerns functions, classes and modules. Now I am designing a database and I wonder if this principle should be applied to relational tables. ...
0
votes
3answers
228 views

Should a method decide whether to execute or not? [closed]

I couldn't find a question on SE but it has probably been asked elsewhere already (in case, please mark it). A method containing code to run just with specific external conditions can: decide ...
1
vote
0answers
206 views

Designing a multithreaded TCP/UDP server for audio sharing

As briefly introduced in the question title, I am trying to design and implement a server application able to let clients share audio contents between themselves. In order to achieve that, I decided ...
2
votes
2answers
266 views

How to use single responsibility with functions when I need to reuse procedures?

I've just read the book called Clean Code. The author (Robert C. Martin) talks about a single responsibility that a function should have in a program. It should only do one thing. Now, I would like ...
3
votes
2answers
237 views

Does method argument self-sanity checking violate the SRP?

I have a method login() which essentially sends a login request to a remote server. As I do not want to waste server resources on processing invalid data, I decided to consider sanity checking the ...
2
votes
2answers
197 views

Single responsibility principle - importer

I'm writing an importer, it should fetch some data from the database and put that data into appropriate places. Now the question is, should the importer itself fetch that data, or should that data(to ...
1
vote
2answers
98 views

Apply Single Responsibility Refactoring in Form App

I really don't understand the SRP applied to a "real" project (really not so real but can help me to know your approach) I find very difficult to divide and assign responsibility to my classes. So I ...
7
votes
7answers
985 views

Separation of concerns: When is it “too much” separation?

I really love clean code and I always want to code my code in the best possible way. But there was always one thing, I didn't really understand: When is it too much of "separation of concerns" ...
1
vote
1answer
399 views

Entity Framework - extending OR injecting services into DB Context

I fear I'm asking a somewhat silly question, but I'm now completely lost as to which principle I should follow. To my understanding - in terms of Single Responsibility Principle it's best we don't ...
4
votes
3answers
1k views

Designing a REST API with resource relationships?

I'm working to design a REST API to be consumed by a React SPA. The client-side of the SPA queries data about a relationship between two entities: Team and Player where Teams have many Players and ...
2
votes
3answers
186 views

Does SRP feed tight coupling?

I designed a function which, afterwards, I noticed it is clearly doing 2 very different tasks in the same time. So now I am thinking to split it into 2 different functions, each one of them doing ...
2
votes
1answer
73 views

Refactoring of a client calling a Rest Service

I have a class that is responsible for calling a service and returning a response. The contract is like this interface ServiceClient { ServiceResponse process(ServiceSubmissionParams params); } ...
4
votes
2answers
228 views

Is it violation of single responsibilty to add a method to an existing API

Say i have a Rest API that has a POST and GET method. If i want to overwrite a resource in the API i can call the GET method to get the original item and then call the POST method to replace that ...
3
votes
4answers
439 views

Is Abstracting your code too much a bad use of SOLID Principles?

After an unresolved argument with a friend I decided to ask the stack overflow community. Is there something like abstracting your code too much? Which of the following is the better option. We come ...
-1
votes
2answers
103 views

How to choose the most suitable solution for a problem given some choices?

To ilustrate my main concern let's start by considering a "trivial" typical problem, data filtering & parsing coming from a process and dumping the information onto something {gui console, file, ...
7
votes
6answers
2k views

How to know when to use dependency injection?

The question might sound a little weird, and I guess it is. I'm came up with the question while browsing through some design patterns. I came to the notorious state / strategy pattern and I came up ...
1
vote
5answers
287 views

Single Responsability actor or use case?

I've been struggling to grasp my head around the SRP pattern, because I dont know if it refers to a actor o to the use case. In the following case: To don't violate the SRP, would you need to create ...
0
votes
2answers
210 views

OOP, SRP, and Checkers

I have just started oop and decided to program a checker's engine. I have few concerns about the way that I have structured my code and I am not sure if I am failing to understand the principals ...
0
votes
3answers
118 views

Separated implementations or one with all functionalities

I am currently creating Excel import modules for some complex data. I didn't plan it well and I have met code reuse issues. I have made first modules and I realized that next modules will need some ...
-5
votes
1answer
53 views

Naming abstract class for object creation and update views

How can I name an abstract class that has a common part of view of my two panels? One of them is for creating an object and the other one is for updating it. I want to keep these view in different ...
2
votes
2answers
129 views

Is replacing the call to mutual exclusive methods by an injected strategy overengineering?

I recently gave advice to refactor some code along the lines of class Validator<T> : IValidator { public ValidationResult Validate(Validatable thingToValidate) { if(typeof(T)....
0
votes
1answer
40 views

Object Responsibility and Calculations

Trying to work out where certain responsibilities lie with the following example. We have a Project object and a Project can have Time entries booked against it. Each Time entry will have a no. of ...
0
votes
0answers
85 views

Understanding Single Responsibility Pattern (SRP) [duplicate]

According to the Single Responsibility Pattern (SRP) a method or class should have one responsibility. I have read a couple of sources and viewed some videos and I would like to understand it by ...
1
vote
0answers
125 views

How to decouple a message broker and its message handlers?

Project description: I have a nodejs project where I try to build a bluetooth mesh like network, each node consists roughly of 4 parts the frontend part, the message broker, a bluetooth client and a ...
3
votes
2answers
106 views

Collection calculating too much?

Let say I've abstracted away a data structure such as a list; should this new object perform all the calculations or just provide minimal calculation functions and then allow a new object such as ...
12
votes
5answers
642 views

Is it useful to mini-refactor code in hopes of improving quality, or is it just “moving code around” without much of a benefit?

Example I came across monolithic code that does "everything" in one place - loading data from database, showing HTML markup, acting as a router/controller/action. I began applying SRP moving ...
0
votes
2answers
192 views

Where should I put the database access method in a treeview environment

I have written a class to represent 'Treeview-like' data, which can be simplified as: public class Item { public string Name { get; set; } public KPI AssociatedKPI { get; set; } public ...
4
votes
4answers
465 views

Is SRP an ambiguous principle? [closed]

I've been aware of SOLID for many years now and I've always though about "OLID" was a good set of design principles to follow... problem is I've always found difficulties with the "S", I've always ...
4
votes
4answers
459 views

Please explain why we need SRP in “Plain English” despite its drawbacks?

Can you in "plain English" explain why we need Single Responsibility Principle? Especially that it moves the what I call "bloat" into other places. See below for more info. By bloat I mean in my ...
9
votes
7answers
924 views

Am I making my classes too granular? How should Single Responsibility Principle be applied?

I write lots of code that involves three basic steps. Get data from somewhere. Transform that data. Put that data somewhere. I typically end up using three types of classes - inspired by their ...
2
votes
4answers
687 views

Single responsibility of a function

I would like to find out if I understand single responsibility principle correctly. The function below is suppose to return user ID store in database by using the SAM account name pass into it. Look ...
0
votes
1answer
113 views

ItemManager class has too many responsibilities (load items, give items, has a reference to ItemsList)

So my manager class does 2 things, load items from file and give one item to a thread that asks for it, it also holds the List that will contain all the items. using System; using System.Collections....
0
votes
1answer
555 views

Should I separate into own class when creating private methods in service layer?

I would like my project to adhere to the SOLID principles and write clean code, but I am still not very clear on when I should separate from the service layer. I have a Web Api 2.0 Controller called ...
66
votes
12answers
13k 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 ...
3
votes
3answers
157 views

SRP processing file line-by-line

I'm writing a class that read lines from a file, process them, store them in an accumulator and when it reaches a threshold bulk inserts in a SqlServer database. Like this: class ...
1
vote
1answer
166 views

Encapsulation and SRP

Suppose I have the following requirements: We organize books by author first name, this might change in the future. When ever an Author we carry at our store releases a new book, the Authors previous ...
3
votes
1answer
945 views

Domain Objects and How they Access the Database

Suppose I have the following class: public class Course { // data public string Name { get; } public List<Student> Students {get;} //... // logic public int ...
0
votes
2answers
165 views

Does Template pattern violate Single Responsibility principle?

Assume we have the following class: class Foo { public: void func() { _func1(); _func2(); } private: virtual void _func1(); virtual void _func2(); }; This class, from one ...
0
votes
1answer
67 views

Api and Auth tightly coupled circular dependancy

I am programming an application and am having trouble finding a good architecture for some of its components. As part of the front-end app, I have a user authentication system and an API that allows ...