154 votes
Accepted

Does a constructor that validates its arguments violate SRP?

SRP is perhaps the most misunderstood software principle. A software application is built from modules, which are built from modules, which are built from... At the bottom, a single function such as ...
Matthieu M.'s user avatar
  • 14.9k
121 votes
Accepted

When using the Single Responsibility Principle, what constitutes a "responsibility?"

One way to wrap your head around this is to imagine potential requirements changes in future projects and ask yourself what you will need to do to make them happen. For example: New business ...
John Wu's user avatar
  • 26.4k
105 votes

Managing and organizing the massively increased number of classes after switching to SOLID?

Now, to build a simple file saving application you have a class to check if the file already exists, a class to write the metadata, a class to abstract away DateTime.Now so you can inject times for ...
David Arno's user avatar
  • 39.2k
79 votes

When using the Single Responsibility Principle, what constitutes a "responsibility?"

Practically speaking, responsibilities are bounded by those things that are likely to change. Thus, there's no scientific or formulaic way to arrive at what constitutes a responsibility, unfortunately....
svidgen's user avatar
  • 14.6k
71 votes
Accepted

Does this class design violate the single responsibility principle?

Single Responsibility should be understood as an abstraction of logical tasks in your system. A class should have the single responsibility to (do everything necessary in order to) perform one single,...
Mason Wheeler's user avatar
67 votes

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

The concept of the SRP is to stop modules doing 2 different things when doing them individually makes for better maintenance and less spaghetti in time. As the SRP says "one reason to change". In ...
gbjbaanb's user avatar
  • 48.6k
56 votes

How can a class have multiple methods without breaking the single responsibility principle

The key here is scope, or, if you prefer, granularity. A part of functionality represented by a class can be further separated into parts of functionality, each part being a method. Here's an example....
Arseni Mourzenko's user avatar
50 votes

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

Many potential performance concerns are not really a problem in practice. The issue you raise may be one of them. In the vernacular, we call worrying about those problems without proof that they are ...
Robert Harvey's user avatar
46 votes

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

As always, this is a question of degree. The SRP should stop you from writing a method that retrieves a record from an external database, performs a fast Fourier transform on it and updates a global ...
Kilian Foth's user avatar
44 votes

Does a constructor that validates its arguments violate SRP?

Quoting Uncle Bob about the SRP (https://8thlight.com/blog/uncle-bob/2014/05/08/SingleReponsibilityPrinciple.html): The Single Responsibility Principle (SRP) states that each software module should ...
Emerson Cardoso's user avatar
42 votes

Does logging inside a class violate the SRP?

Your colleague is taking the word "Single" too literally and dogmatically. SRP just means a class should have a single conceptual purpose. What constitutes a single conceptual purpose will ...
whatsisname's user avatar
  • 27.7k
41 votes

Does this class design violate the single responsibility principle?

Single Responsibility Principle is only concerned with whether or not a piece of code (in OOP, typically we're talking about classes) has responsibility over one piece of functionality. I think your ...
Frank Bryce's user avatar
38 votes

Does a constructor that validates its arguments violate SRP?

No, this change is not informed by the SRP. Ask yourself why there is no check in your checker for "the object passed in is a stream". The answer is obvious: the language prevents the caller from ...
Eric Lippert's user avatar
  • 46.1k
35 votes
Accepted

How can a class have multiple methods without breaking the single responsibility principle

The single responsibility might not be something that a single function can fulfill. class Location { public int getX() { return x; } public int getY() { return y;...
candied_orange's user avatar
35 votes
Accepted

Is there a conflict between YAGNI and SRP?

YAGNI means to avoid investing effort into code changes for hypothetical requirements which may arrive later, and instead focus on the requirements one has now. But this is not restricted to ...
Doc Brown's user avatar
  • 206k
34 votes

How can a class have multiple methods without breaking the single responsibility principle

A function is a function. A responsibility is a responsibility. A mechanic has the responsibility to fix cars, which will involve diagnostics, some simple maintenance tasks, some actual repair work, ...
Peter's user avatar
  • 3,748
32 votes

When using the Single Responsibility Principle, what constitutes a "responsibility?"

I follow "classes should have only one reason to change". For me, this means thinking of harebrained schemes that my product owner might come up with ("We need to support mobile!", "We need to go to ...
Telastyn's user avatar
  • 109k
30 votes

Managing and organizing the massively increased number of classes after switching to SOLID?

Tasks that used to take 5-10 files can now take 70-100! This is the opposite of the single-responsibility principle (SRP). To get to that point, you must have divided up your functionality in a very ...
John Bollinger's user avatar
29 votes

When using the Single Responsibility Principle, what constitutes a "responsibility?"

No one knows. Or at least, we are unable to agree on one definition. That is what makes SPR (and other SOLID principles) quite controversial. I would argue being able to figure out what is or isn't a ...
Euphoric's user avatar
  • 37.3k
29 votes

Applicability of Single Responsibility Principle

Sending a notification that the persistent data store changed seems like a sensible thing to do when saving. Of course you shouldn't treat Add as a special case - you'd have to fire events for Modify ...
Peter's user avatar
  • 3,748
27 votes

Does a constructor that validates its arguments violate SRP?

Not all responsibilities are created equal. Here are two drawers. They both have one responsibility. They each have names that let you know what belongs in them. One is the silverware drawer. The ...
candied_orange's user avatar
27 votes

Does logging inside a class violate the SRP?

People have gone mad thinking about what constitutes a "single responsibility". The principle is sound, but it is usually stated in a way which cause more confusion than clarity. The ...
JacquesB's user avatar
  • 58.7k
25 votes
Accepted

When following SOLID, are reading and writing files two separate responsibilities?

The reading and writing implementation likely have a high probability of being highly cohesive. If one would change, so would the other. High cohesion is a strong indication of a Single Responsibility ...
Steven's user avatar
  • 561
24 votes

How specific should the Single Responsibility pattern be for classes?

The single responsibility principle can be tricky to understand. What I've found useful is to think of it like how you write sentences. You don't try to cram a lot of ideas into a single sentence. ...
Vaughn Cato's user avatar
  • 1,069
22 votes

Does this class design violate the single responsibility principle?

There are already great answers pointing out that SRP is an abstract concept about logical functionality, but there are subtler points that I think are worth adding. The first two letters in SOLID, ...
JounceCracklePop's user avatar
22 votes

Does a constructor that validates its arguments violate SRP?

When you tie yourself in knots and write weird code in order to conform to an Important Software Principle, usually you've misunderstood the principle (though sometimes the principle is wrong). As ...
Ed Plunkett's user avatar
22 votes

How can a class have multiple methods without breaking the single responsibility principle

Single responsibility does not necessarily mean it only does one thing. Take for example a User service class: class UserService { public User Get(int id) { /* ... */ } public User[] List() {...
Jessie's user avatar
  • 522
21 votes
Accepted

What does it mean for a method or a function to do one thing?

If I tell you to "take out the trash" I've asked you to do one thing. It's one cohesive idea. Does that mean it can't be decomposed into more things? No. Everything can be decomposed into more ...
candied_orange's user avatar
20 votes

Is SRP an ambiguous principle?

I wouldn't use the term ambiguous, but without a doubt there is a significant element of situational judgement. To answer your question up front, no, there is no formal, mathematically verifiable ...
whatsisname's user avatar
  • 27.7k
19 votes

Does this class design violate the single responsibility principle?

To my mind this class could potentially violate SRP if it continued to represent both an Employee and EmployeeHolidays. As it is, and if it came to me for Peer Review, I'd probably let it through. If ...
NikolaiDante's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible