Hot answers tagged

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 ...
user avatar
  • 8,640
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 ...
user avatar
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 ...
user avatar
  • 24.7k
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 ...
user avatar
  • 124k
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 ...
user avatar
  • 27.2k
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 ...
user avatar
  • 38.2k
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 ...
user avatar
  • 185k
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, ...
user avatar
  • 185k
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....
user avatar
  • 13.2k
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 ...
user avatar
  • 38.2k
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 ...
user avatar
  • 124k
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....
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 ...
user avatar
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 ...
user avatar
  • 38.2k
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 ...
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 ...
user avatar
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 ...
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 ...
user avatar
  • 107k
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 ...
user avatar
  • 578
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 ...
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 ...
user avatar
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 ...
user avatar
  • 6,773
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;...
user avatar
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 ...
user avatar
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, ...
user avatar
  • 3,678
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 ...
user avatar
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 ...
user avatar
  • 427
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 ...
user avatar
  • 107k
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 ...
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 (...
user avatar

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