199
votes
Accepted
Why would Square inheriting from Rectangle be problematic if we override the SetWidth and SetHeight methods?
Basically we want things to behave sensibly.
Consider the following problem:
I am given a group of rectangles and I want to increase their area by 10%. So what I do is I set the length of the ...
137
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 ...
119
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 ...
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 ...
104
votes
Accepted
When NOT to apply the Dependency Inversion Principle?
In many cartoons or other media, the forces of good and evil are often illustrated by an angel and a demon sitting on the character's shoulders. In our story here, instead of good and evil, we have ...
104
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 ...
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, ...
78
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....
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 ...
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 ...
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 ...
49
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 ...
47
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 ...
42
votes
Accepted
Can the circle-ellipse problem be solved by reversing the relationship?
But isn't the problem here caused by having Circle be the subtype of an Ellipse? Couldn't we reverse the relationship?
The problem with this (and the square/rectangle problem) is falsely assuming a ...
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 ...
42
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 ...
37
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 ...
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 ...
35
votes
Accepted
IOC Containers break OOP Principles
I'll go through your points numerically, but first, there's something you should be very careful of: don't conflate how a consumer uses a library with how the library is implemented. Good examples of ...
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;...
33
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 ...
33
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, ...
32
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
Why would Square inheriting from Rectangle be problematic if we override the SetWidth and SetHeight methods?
If all your objects are immutable, there is no problem. Every Square is also a Rectangle. All the properties of a Rectangle are also properties of a Square.
The problem begins when you add the ...
31
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 ...
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 ...
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 (...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
solid × 376object-oriented × 130
design-patterns × 72
c# × 59
single-responsibility × 53
design × 52
object-oriented-design × 51
liskov-substitution × 42
interfaces × 32
java × 21
architecture × 20
php × 17
dependency-injection × 17
dependency-inversion × 17
open-closed-principle × 17
c++ × 15
inheritance × 14
clean-code × 13
class-design × 12
unit-testing × 10
design-principles × 10
.net × 9
domain-driven-design × 9
open-close × 9
patterns-and-practices × 8