Skip to main content
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 ...
svidgen's user avatar
  • 15.3k
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
  • 136k
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.6k
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. ...
Doc Brown's user avatar
  • 220k
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
  • 220k
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 ...
David Arno's user avatar
  • 39.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
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.6k
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 ...
Greg Burghardt's user avatar
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
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 ...
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

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,778
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
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 ...
Robert Harvey's user avatar
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
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
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 (...
Filip Milovanović's user avatar
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 ...
Doc Brown's user avatar
  • 220k
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 ...
Robert Bräutigam's user avatar
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. ...
svidgen's user avatar
  • 15.3k
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
  • 512
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 ...
Ralf Kleberhoff's user avatar
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 ...
gnasher729's user avatar
  • 49.4k
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
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 ...
Ewan's user avatar
  • 84.4k
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 ...
amon's user avatar
  • 136k
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 ...
gnasher729's user avatar
  • 49.4k
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 ...
JacquesB's user avatar
  • 62.3k

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