Last call to make your voice heard! Our 2022 Developer Survey closes in less than a week. Take survey.

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
1 vote
3 answers
135 views

Is a method named createOrUpdate() violating the single responsibility principle? Should I split into create() and update()?

I work developing software, and very often I find inconsistency in the company's code. The thing is, there is a method named createOrUpdate(), and this method basically does what its name says, ...
user avatar
  • 39
3 votes
2 answers
187 views

Under the single-responsibility principle, should caching data be a separate function from returning the data?

Suppose I have a program that returns data from an API. If its cache of the data is too old, the program downloads more data from the API, caches it, and outputs the new data. Else, the program ...
user avatar
  • 2,011
3 votes
3 answers
231 views

In testing is making asserts in cleanup bad?

As a learning excercise, I decided to take a hand on Test Driven Development. Now I decided that there is a test I want to make; check if database connection doesn't leave any unsaved changes. The two ...
user avatar
  • 133
0 votes
2 answers
136 views

How to refactor code so that a facade class could be decoratable?

I've got a class that is a facade class (encapsulates complex-ish behaviour for reusability). It has a function called manage (the class is called Manager): function manage() { $entityBuilder = '...
user avatar
0 votes
1 answer
124 views

std::string violates Single Responsibility Principle? [closed]

I recently attended a C++ talk on SOLID principles and the presenter provided std::string as an example that violates SRP. I wasn't quite able to comprehend why that was the case. From what I ...
user avatar
  • 199
-2 votes
2 answers
121 views

Guidance on naming methods that check data quality and then executes logic [closed]

I'm writing boolean methods that do a data quality check and then performs logic. such as isRecordValidAndIsUserPrivilaged. these methods checks the record validity, and then checks the user's access )...
user avatar
1 vote
2 answers
113 views

How to maintain cohesion, loose coupling, and SRP when building a reporting tool?

I'm building a reporting tool with three core layers: Extract data from a database Transform this data to compute business metrics Display these metrics in a report (a heavily formatted Excel export ...
user avatar
  • 119
2 votes
2 answers
68 views

Should client application clean the data that a server application can not internally process? (In SOA Systems)

Scenario : For some (comment) text field the server application is internally throwing errors when the field contains contains some specific characters. Where should the removal of characters be ...
user avatar
  • 865
0 votes
1 answer
85 views

Clean code: Register listeners inside class handling events, or outside?

I'm writing a Java program with a custom event manager, which is of course the observer pattern. Should the calls to add event listeners/handlers be in the classes that listens/handles the events ...
user avatar
0 votes
1 answer
57 views

For a spa application should request be follow the "single responsiblity" idea or is it better to combine data?

Well in a page in our spa application we need to request some dynamic data from the server. Besides this data for display purposes we also need to request explanations of the columns from the data (...
user avatar
  • 1,021
3 votes
2 answers
262 views

How to use visitor pattern when objects change frequently

It is said that visitor pattern is applicable to problems where objects rarely change but we add actions on those objects more frequently. What if the objects are changing too though? For example we ...
user avatar
1 vote
1 answer
118 views

Abstract Base Class decides which type of child class to create at runtime

I have an Abstract Base Class AbstractModel class AbstractModel { public: struct predictionStructure{}; virtual predictionStructure predict(CompanyLib::Matrix<double> data) = 0; std::...
user avatar
1 vote
3 answers
434 views

Clean architecture, Login UseCase with OAuth breaking Single responsability principle

I'm building a login system using OAuth to integrate with various social networks. The flow is very simple, first, the user is redirected to their social network to grant permission to read the ...
user avatar
1 vote
1 answer
149 views

SRP, command pattern and design problem

I'm refactoring a code base to have something more easier to read and follow SRP. However I'm at the point I'm unsure what the best design is. Currently it looks like this: A thread receives a "...
user avatar
  • 119
0 votes
1 answer
89 views

Why doesn't Laravel actively support Service Classes?

There must be a good reason for this. Laravel's default pattern makes no use of Service Classes... but the Laravel community's best practices is for business logic to be stored in them. Laravel's ...
user avatar
-1 votes
2 answers
92 views

Contemporary Design and One-to-One Relationships: code-first vs data-base first approaches

After reading, Does it ever make sense NOT to condense one to one relationships?, I wanted to take the question further with regards to contemporary design of entities, front-end code and controllers. ...
user avatar
  • 1,339
0 votes
1 answer
67 views

Mixing an API server and a background worker

What are your thoughts on having a service/process be both an API server and a background worker, consuming messages off a queue like SQS? I have typically kept these two separate to keep single ...
user avatar
  • 111
6 votes
6 answers
441 views

Should a Car object be responsible for checking if it is able to run on certain Road objects?

I have 2 kinds of objects in my system, Car and Road. A Car can be of various types and have properties like tire, tire size, engine, etc. A Road can be of multiple types and have properties like ...
user avatar
  • 169
1 vote
1 answer
50 views

Guidelines for using extensions in Swift

The other day, I came across this question on StackOverflow. In short, the user who asked the question wanted to extend a class from a third-party library to implement the Codable protocol, but ...
user avatar
  • 142
0 votes
1 answer
108 views

Expose or consume API REST?

I am preparing the software architecture for my client project based on APIs REST The idea is that any data exchange between my client and his partner will be via API REST call The specification is ...
user avatar
32 votes
10 answers
5k views

Does logging inside a class violate the SRP?

I wrote a class that takes a Logger class as one of its arguments: class QueryHandler: def __init__(self, query: Query, logger: Logger) -> None: self.query = query self.logger =...
user avatar
  • 669
1 vote
1 answer
86 views

What is the granularity of the SRP of functions? [duplicate]

If a function is being used to process incoming results, I believe an SRP violation exists if it contains all the logic: def process_response(response): '''Process the results of the web request''' ...
user avatar
1 vote
3 answers
186 views

Too much parameter in a class' constructor, when trying to conserve the dependency injection principle [duplicate]

I am developing a simulator of a bank software system, where each operation (deposit, withdraw, transfer, ...) is just a string. I have 5 main classes : Bank where bank accounts are stored. ...
user avatar
  • 65
0 votes
2 answers
42 views

Should an Express error handler be used to send HTTP 4xx responses?

In other words, should the raising of an HTTP 4xx code be considered an error, and should the job of sending an HTTP 4xx code to a client be delegated to an error handler? Or is it simpler to just ...
user avatar
  • 109
2 votes
3 answers
155 views

When does a function violate the SRP?

I have a function that was written because several methods used the same sequence of code so an abstraction was useful. The function takes a string representing the path to a registry key and then a ...
user avatar
0 votes
2 answers
239 views

Good design for a class with multiple methods to test but one public method

I've been trying to refactor some existing code which is in essence a giant nested procedural call inside what should otherwise be an object oriented architecture. The entry point to the relevant code ...
user avatar
  • 23
3 votes
1 answer
65 views

How to handle changes to local and global representation of an Entity in a distributed environment?

I'm a developer for a retail enterprise IT department, and am currently stuck on the following scenario: Department A is responsible for a CRUD service that's used for handling stores - so when our ...
user avatar
  • 99
1 vote
1 answer
230 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 ...
user avatar
1 vote
2 answers
124 views

How does one choose where to place logic which converts data between two formats/representations?

Suppose I have two "modules", A, and B (I'm choosing not to use classes, because you generally can't create an instance of a module, which makes this question simpler). These modules contain ...
user avatar
7 votes
3 answers
324 views

Breaking up a class with too many responsibilities

I'm looking for advice on my specific problem, how to break up a class, Basket, that has too many responsibilities. Currently, it does three things: Keeps track of the products in the basket: ...
user avatar
-3 votes
2 answers
109 views

Do classes that set, get and calculate data follow the S in SOLID?

Supposing I live in a country where every car brand has a different tax rate, and I have a base class called Car public class Car{ public string CarType { get; set;} public int Year {get; set;}...
user avatar
2 votes
3 answers
481 views

How to not violate the single responsibility principle?

Let's say I'm writing a class that tracks a single stock ticker. In this simplified example, the StockTracker class holds a string that tells me the trend direction and a variable window that holds x ...
user avatar
  • 139
7 votes
2 answers
420 views

Data handling in SRP (single responsibility principle)

TLDR; Robert C. Martin adviced here: http://blog.cleancoder.com/uncle-bob/2014/05/08/SingleReponsibilityPrinciple.html to split the multi responsibility classes into "single reason to change"...
user avatar
  • 181
0 votes
1 answer
121 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(...
user avatar
0 votes
3 answers
90 views

How to arrange expected result model for test in a cleaner,readable way when the model contains many properties

In one of my projects I saw wrapping the expected results as a static variable in non static class. The reason for doing so is to make the code more readable, so that the massive expected result model ...
user avatar
  • 215
-2 votes
1 answer
223 views

SRP for MVC controllers

I am trying to design an application that will be a REST API following MVC. For now ,I have 2 main features: Projects and Tickets associated to Projects. I would like to apply SOLID principles, and I ...
user avatar
  • 145
0 votes
2 answers
136 views

Avoiding sequential coupling while maintaining separation of concerns

Got a desktop application that interacts with some stateful third party web APIs. Its an interactive application, at some stages it has to halt execution and prompt for user input based on responses ...
user avatar
1 vote
4 answers
260 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!! ...
user avatar
1 vote
1 answer
54 views

aggregate data for complex API call without coupling modules to parameter object

I am working with an API that is used for registering bookings in a ticket system. Some of the calls I have to make take complex XML objects as arguments. Some data points in these objects have to be ...
user avatar
3 votes
4 answers
412 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 ...
user avatar
1 vote
1 answer
87 views

Structuring application to comply with Single responsibility

I have the class AircraftManager that keeps track of a list of Aircraft objects and a list of AircraftListener objects. A DataSource (custom class, not sql related) object retrieves information from ...
user avatar
  • 11
2 votes
5 answers
301 views

Splitting class responsibilities without exposing private data

I'm struggling to find good ways to split up classes without exposing private data. Most articles I read about SRP seem to ignore how the new classes that take on the separated responsibilities access ...
user avatar
2 votes
1 answer
154 views

Is this SRP violation?

Consider I have a class which represents a tree element. An element can be changed, inspected children elements can be added to it (say trough the add() method). But also I have a class which contains ...
user avatar
  • 129
6 votes
3 answers
3k 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? ...
user avatar
  • 429
0 votes
1 answer
78 views

How to detect class responsibilities? [duplicate]

Consider following class: public class Foo { public Foo() {} public void Bar(int input) { Console.WriteLine("Working on input ..."); switch(input) { ...
user avatar
1 vote
3 answers
382 views

Feature Envy vs. Single Responsibility Principle: Data class and a validator for it

Let's say I have a class that's a bit more than a pure Data object, i.e. it holds some data and some basic queries about that data. (In this case, it's a class representing a timetable) Now, I want ...
user avatar
  • 476
2 votes
1 answer
58 views

Should a Mover and Tracker be separate classes to maintain SRP?

I'm doing some Java code practice to better understand the Single Responsibility Principle. Currently, I'm working with a network of data structures, where each structure implements an interface ...
user avatar
1 vote
1 answer
145 views

Controller class for an input dialog

I am learning OOP, concretely Java, by developing a real life business application for aimed for repair shops. I have followed MVC pattern design for GUI elements of my application. I was wondering do ...
user avatar
13 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 ...
user avatar
2 votes
4 answers
992 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 ...
user avatar

1
2 3 4 5 6