Skip to main content
160 votes
Accepted

When to *not* use SOLID principles

My Principle of Applying Principles: Principles, patterns, and practices are not final purposes. The good and proper application of each is therefore inspired and constrained by a superior, more ...
svidgen's user avatar
  • 15.1k
138 votes

Why would the 'final' keyword ever be useful?

final expresses intent. It tells the user of a class, method or variable "This element is not supposed to change, and if you want to change it, you haven't understood the existing design." This is ...
Kilian Foth's user avatar
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.6k
106 votes
Accepted

Additional line in block vs additional parameter in Clean Code

These guidelines are a compass, not a map. They point you in a sensible direction. But they can't really tell you in absolute terms which solution is “best”. At some point, you need to stop walking ...
amon's user avatar
  • 135k
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.4k
97 votes
Accepted

Class that does not represent anything - is it correct?

Classes should do 1 thing and do it well Yes, that is generally a good approach. but from the other hand they should represent real object we work with. No, that is a IMHO common ...
Doc Brown's user avatar
  • 210k
83 votes

Does following SOLID lead to writing a framework on top of the tech stack?

Your observation is correct, the SOLID principles are IMHO made with reusable libraries or framework code in mind. When you just follow all of them blindly, without asking if it makes sense or not, ...
Doc Brown's user avatar
  • 210k
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
  • 15.1k
62 votes

Additional line in block vs additional parameter in Clean Code

The ideal number of arguments for a function is zero (niladic) No! The ideal number of arguments for a function is one. If it's zero, then you are guaranteeing that the function has to access ...
David Arno's user avatar
  • 39.4k
60 votes

Why would the 'final' keyword ever be useful?

It avoids the Fragile Base Class Problem. Every class comes with a set of implicit or explicit guarantees and invariants. The Liskov Substitution Principle mandates that all subtypes of that class ...
amon's user avatar
  • 135k
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
52 votes

Using a "Pass-through (God) Service" is bad, right?

This is not a God object. It seems like it is because there is so much here, but, in a way, it's doing nothing at all. There is no behavior code here. This isn't an omnipotent God that does ...
candied_orange's user avatar
50 votes

Does following SOLID lead to writing a framework on top of the tech stack?

From my experience, when writing an app, you have three choices: Write code solely to fulfil the requirements, Write generic code that anticipates future requirements, as well as fulfilling the ...
David Arno's user avatar
  • 39.4k
49 votes

Which object should have the method?

A user is someone who is registered and able to use the system. A chat room is a place people can chat. What happens when a user joins a chat room? What is that thing that represents a user who has ...
Greg Burghardt's user avatar
43 votes

Are so called "cross-cutting concerns" a valid excuse to break SOLID/DI/IoC?

Yes This is the whole point of the term "cross-cutting concern" - it means something that does not fit neatly in the SOLID principle. This is where idealism meets up with reality. People semi-new ...
Klom Dark's user avatar
  • 588
43 votes

Class that does not represent anything - is it correct?

Doc Brown is spot-on: classes don’t need to represent real-world objects. They just need to be useful. Classes are fundamentally merely additional types, and what does int or string correspond to in ...
Konrad Rudolph's user avatar
42 votes
Accepted

Are so called "cross-cutting concerns" a valid excuse to break SOLID/DI/IoC?

No. SOLID exists as guidelines to account for inevitable change. Are you really never going to change your logging library, or target, or filtering, or formatting, or...? Are you really not going to ...
Telastyn's user avatar
  • 109k
38 votes

Should rectangle inherit from square?

The classic example of square not being able to substitute for rectangle without violating LSP is a bit of a "trick question" and sophistic. The problem arises because of a conflation... i.e. an ...
Bradley Thomas's user avatar
36 votes

Class that does not represent anything - is it correct?

I am just designing my application and I am not sure if I understand SOLID and OOP correctly. Been at this over 20 years and I'm not sure either. Classes should do 1 thing and do it well Hard ...
candied_orange's user avatar
36 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
34 votes

Why would the 'final' keyword ever be useful?

I'm surprised that no one has yet mentioned Effective Java, 2nd Edition by Joshua Bloch (which should be required reading for every Java developer at least). Item 17 in the book discusses this in ...
Daniel Pryden's user avatar
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
33 votes

Stack extending LinkedList. A violation of Liskov Substitution Principle?

Now there is a class Stack that provides functionalities such as push(), pop(), peek() or top(), and to implement these methods it extends the LinkedList class methods. Is this a violation of Liskov ...
Jörg W Mittag's user avatar
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
31 votes

Designing a Class to take whole classes as parameters rather than individual properties

There is absolutely nothing wrong with passing an entire User object as a parameter. In fact, it might help clarify your code, and make it more obvious to programmers what a method takes if the method ...
Greg Burghardt's user avatar
31 votes
Accepted

What would be an example of the Liskov Substitution Principle, if you don't use inheritance?

It's not about inheritance, it's about substitutability of types. In languages that support duck typing (JavaScript, Python, compile-time polymorphism of C++ templates, etc...), or structural typing (...
Filip Milovanović's user avatar
30 votes
Accepted

Do Interactors in "clean architecture" violate the Single Responsibility Principle?

The "duties" of an Interactor in Bob Martin's clean architecture are, per use case: receive requests/inputs from a controller; orchestrate domain entities to fulfil the requests; and prepare the ...
Robert Harvey's user avatar
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.5k
27 votes

Are so called "cross-cutting concerns" a valid excuse to break SOLID/DI/IoC?

For logging I think it is. Logging is pervasive and generally unrelated to the service functionality. It's common and well-understood to use the logging framework singleton patterns. If you don't, you'...
Brian Agnew's user avatar
  • 4,686

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