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.

282 questions
213
votes
16 answers

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 customer); public Customer Read(int CustomerID); …
Robert Harvey
  • 198,589
  • 55
  • 464
  • 673
80
votes
8 answers

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 responsibility over a single part of the…
Goose
  • 1,858
  • 2
  • 15
  • 27
73
votes
8 answers

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 situation where he has applied SRP to what was already…
Dean Chalk
  • 798
  • 1
  • 6
  • 9
69
votes
12 answers

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 approach is sound or if there are any severe…
Paul Kertscher
  • 2,434
  • 1
  • 15
  • 20
64
votes
12 answers

Does this class design violate the single responsibility principle?

Today I had an argument with someone. I was explaining the benefits of having a rich domain model as opposed to an anemic domain model. And I demoed my point with a simple class looking like that: public class Employee { public Employee(string…
tobiak777
  • 797
  • 1
  • 5
  • 11
59
votes
8 answers

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 things but I am getting push-back from the teams who…
honestduane
  • 715
  • 1
  • 6
  • 5
50
votes
7 answers

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 at least resembles SOLID, but we're not quite…
JD Davis
  • 1,367
  • 1
  • 15
  • 23
46
votes
8 answers

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 more, the article states that there shouldn't be,…
Pierre Arlaud
  • 1,329
  • 1
  • 13
  • 21
42
votes
10 answers

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(user); /* do some other…
Andre Borges
  • 1,534
  • 1
  • 14
  • 15
39
votes
8 answers

Does adding a return type to an update method violate the "Single Responsibility Principle"?

I have a method that updates employees data in the database. The Employee class is immutable, so "updating" the object means actually to instantiate a new object. I want the Update method to return a new instance of Employee with the updated data,…
Michael Haddad
  • 2,651
  • 3
  • 17
  • 27
38
votes
9 answers

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 related, while classes adhering to SRP have just one…
user1483278
  • 1,121
  • 1
  • 10
  • 14
37
votes
11 answers

Ensure that each class has only one responsibility, why?

According to Microsoft documentation, the Wikipedia SOLID principe article, or most IT architects we must ensure that each class has only one responsibility. I would like to know why, because if everybody seems to agree with this rule nobody seems…
31
votes
10 answers

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 = logger def run(self) -> None: try: …
23
votes
7 answers

Does multiple inheritance violate Single Responsibility Principle?

If you have a class which inherits from two distinct classes, does not this mean that your subclass automatically does (at least) 2 things, one from each superclass? I believe there is no difference if you have multiple interface inheritance. To be…
22
votes
6 answers

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 the implementation should be also able to run without…
Aitch
  • 709
  • 4
  • 13
1
2 3
18 19