162
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 ...
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 ...
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 ...
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 misunderstanding. ...
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, ...
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 external ...
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....
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 ...
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 ...
48
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 ...
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 ...
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 to ...
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;...
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, ...
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 ...
31
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 ...
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 ...
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 ...
30
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 (...
25
votes
Does TDD contradict the open-closed principle?
Frankly, I see at least three huge misconceptions in this question:
what TDD is about, and
what the OCP is about, and
software is developed in a waterfall approach.
Let me start with the OCP. The ...
25
votes
Which object should have the method?
Rule of thumb: Your methods are probably in the right place if they don't need to pull data out of other objects.
This is often called "envy", as in "feature envy". If your method ...
22
votes
What is more important? SOLID or KISS?
TLDR;
Neither is "more important." Developers do not serve principles; principles serve developers. Apply them in the manner and extent to which they achieve your goals and help you deliver.
...
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() {...
21
votes
Liskov Substitution principle - strengthening preconditions
You can't decide whether some code violates the LSP by the code itself. You need to know the contract that each method is to fulfill.
In the example, there is no explicit contract given, so we have ...
21
votes
Accepted
Which object should have the method?
In your case, you have users vs. Chatrooms. If you have methods that concern both, you could put them into either class, not much difference.
However: You will have not only users vs. Chatrooms, you ...
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 ...
20
votes
Additional line in block vs additional parameter in Clean Code
The 'Clean Code' Advice is completely wrong.
Use two or more lines in your loop. Hiding the same two lines in a function makes sense when they are some random maths which needs a description but it ...
20
votes
Accepted
UnsupportedOperationException vs Interface Segregation
It is typically a bad idea to have “optional” methods in an interface that throw an exception when invoked. Yes, the Java standard library happens to do this, but this is widely considered to have ...
19
votes
How can a class have multiple methods without breaking the single responsibility principle
There is a chef in a restaurant. His only responsibility is to cook. Yet he can cook steaks, potatoes, broccoli, and hundred other things. Would you hire one chef per dish on your menu? Or one chef ...
19
votes
Does it violate the Single Responsibility Principle if an object knows how to change itself?
I have come to the conclusion that the "Single Responsibility Principle" is doing more harm than good due to its confusing name.
The principle does not say that a class should only be allowed to do ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
solid × 406object-oriented × 139
design-patterns × 76
c# × 66
design × 58
single-responsibility × 57
object-oriented-design × 56
liskov-substitution × 49
interfaces × 35
open-closed-principle × 26
java × 23
architecture × 21
dependency-injection × 19
php × 18
dependency-inversion × 18
inheritance × 17
c++ × 14
clean-code × 12
class-design × 12
design-principles × 11
unit-testing × 10
domain-driven-design × 10
.net × 10
patterns-and-practices × 8
python × 7