Skip to main content

Questions tagged [solid]

Mnemonics for set of design principles: Single responsibility, Open-closed, Liskov substitution, Interface segregation, Dependency inversion

Filter by
Sorted by
Tagged with
46 votes
8 answers
13k views

What is the real responsibility of a class?

I keep wondering if it is legitimate to use verbs that are based on nouns in OOP. I came across this brilliant article, though I still disagree with the point it makes. To explain the problem a bit ...
Pierre Arlaud's user avatar
153 votes
12 answers
28k views

Is this a violation of the Liskov Substitution Principle?

Say we have a list of Task entities, and a ProjectTask sub type. Tasks can be closed at any time, except ProjectTasks which cannot be closed once they have a status of Started. The UI should ensure ...
Paul T Davies's user avatar
213 votes
16 answers
23k views

When using the Single Responsibility Principle, what constitutes a "responsibility?"

It seems pretty clear that "Single Responsibility Principle" does not mean "only does one thing." That's what methods are for. public Interface CustomerCRUD { public void Create(Customer ...
Robert Harvey's user avatar
124 votes
12 answers
32k views

Why would Square inheriting from Rectangle be problematic if we override the SetWidth and SetHeight methods?

If a Square is a type of Rectangle, than why can't a Square inherit from a Rectangle? Or why is it a bad design? I have heard people say: If you made Square derive from Rectangle, then a Square ...
user793468's user avatar
  • 1,373
46 votes
3 answers
34k views

Programming SOLID Principles

Over time I could understand two parts of SOLID – the “S” and “O”. “O” – I learned Open Closed Principle with the help of Inheritance and Strategy Pattern. “S” – I learned Single Responsibility ...
LCJ's user avatar
  • 987
13 votes
1 answer
4k views

Is wrapping a third party code the only solution to unit test its consumers?

I'm doing unit testing and in one of my classes I need to send a mail from one of the methods, so using constructor injection I inject an instance of Zend_Mail class which is in Zend framework. Now ...
Songo's user avatar
  • 6,598
46 votes
2 answers
18k views

Equivalent of SOLID principles for functional programming

I've found the SOLID principles quite useful when thinking about object-oriented design. Is there a similar / equivalent set of language-agnostic principles tailored for functional programming?
mikera's user avatar
  • 20.7k
29 votes
9 answers
11k views

What can go wrong if the Liskov substitution principle is violated?

I was following this highly voted question on possible violation of Liskov Substitution principle. I know what the Liskov Substitution principle is, but what is still not clear in my mind is what ...
Geek's user avatar
  • 5,197
16 votes
1 answer
3k views

How to verify the Liskov substitution principle in an inheritance hierarchy?

Inspired by this answer: Liskov Substitution Principle requires that Preconditions cannot be strengthened in a subtype. Postconditions cannot be weakened in a subtype. Invariants of ...
Songo's user avatar
  • 6,598
60 votes
5 answers
36k views

What are the design principles that promote testable code? (designing testable code vs driving design through tests)

Most of the projects that I work on consider development and unit testing in isolation which makes writing unit tests at a later instance a nightmare. My objective is to keep testing in mind during ...
CKing's user avatar
  • 1,012
46 votes
5 answers
13k views

How is dependency inversion related to higher-order functions?

Today I've just seen this article which described the relevance of SOLID principle in F# development- F# and Design principles – SOLID And while addressing the last one - "Dependency inversion ...
Gulshan's user avatar
  • 9,462
17 votes
4 answers
6k views

Two contradicting definitions of Interface Segregation Principle – which one is correct?

When reading articles on ISP, there seem to be two contradicting definitions of ISP: According to first definition ( see 1, 2, 3), ISP states that classes implementing the interface shouldn't be ...
EdvRusj's user avatar
  • 623
13 votes
3 answers
3k views

Dependency Inversion Principle: How to define "high-level policy" and "low-level detail" to other people?

I am trying to explain the dependency inversion principle to my (mostly junior) colleagues. How can we define which one is the "high-level policy" and which one is the "low-level detail" in a software?...
Louis Rhys's user avatar
  • 6,142
11 votes
4 answers
2k views

How does thinking on design patterns and OOP practices change in dynamic and weakly-typed languages?

There is a fairly helpful question already along these lines ("Non-OOP Design Patterns?"), but I am more curious about a transitional point of view for someone just getting started with dynamic and ...
Domenic's user avatar
  • 684
9 votes
3 answers
7k views

What does "abstractions should not depend on details" actually mean?

Most readers will be familiar with Bob Martin's famous dependency inversion principle, which states High-level modules should not depend on low-level modules. Both should depend on abstractions ...
Craig Schwarze's user avatar
7 votes
3 answers
4k views

How do you apply Single Responsibility principle to a repository

I am trying to apply "SOLID" whenever I can and try to use common sense and avoid a pattern when I see that a pattern is creating more problems than it's trying to solve. I don't want to apply a ...
developer9969's user avatar
159 votes
6 answers
38k views

SOLID Principles and code structure

At a recent job interview, I couldn't answer a question about SOLID -- beyond providing the basic meaning of the various principles. It really bugs me. I have done a couple of days worth of digging ...
S-Unit's user avatar
  • 1,397
83 votes
8 answers
18k views

How can a class have multiple methods without breaking the single responsibility principle

The Single responsibility principle is defined on wikipedia as The single responsibility principle is a computer programming principle that states that every module, class, or function should have ...
Goose's user avatar
  • 1,898
62 votes
8 answers
28k views

LSP vs OCP / Liskov Substitution VS Open Close

I am trying to understand the SOLID principles of OOP and I've come to the conclusion that LSP and OCP have some similarities (if not to say more). the open/closed principle states "software ...
Kolyunya's user avatar
  • 965
52 votes
8 answers
14k views

When NOT to apply the Dependency Inversion Principle?

I am currently trying to figure out SOLID. So the Dependency Inversion Principle means that any two classes should communicate via interfaces, not directly. Example: If class A has a method, that ...
Vorac's user avatar
  • 7,129
50 votes
7 answers
12k views

Managing and organizing the massively increased number of classes after switching to SOLID?

Over the last few years, we have been slowly making the switch over to progressively better written code, a few baby steps at a time. We are finally starting to make the switch over to something that ...
JD Davis's user avatar
  • 1,377
46 votes
6 answers
13k views

SOLID principles vs YAGNI

When do the SOLID principles become YAGNI? As programmers we make trade-offs all the time, between complexity, maintainability, time to build and so forth. Amongst others, two of the smartest ...
KeesDijk's user avatar
  • 8,948
19 votes
5 answers
4k views

What is the meaning of "inversion" in Dependency Inversion design principle?

I'm reading about design patterns. I know what this principle does. High-level and low-level classes depend on abstractions. But why we say this is inversion?
Milad Khajavi's user avatar
18 votes
9 answers
5k views

Which object should have the method?

I am trying to create an object model for a user and a chatroom. I'm stuck on where to place certain functionality when the objects collaborate. At the moment all the functionality for the User is ...
r0k1m's user avatar
  • 851
17 votes
5 answers
2k views

Is the semantic contract of an interface (OOP) more informative than a function signature (FP)?

It is said by some that if you take SOLID principles to their extremes, you end up at functional programming. I agree with this article but I think that some semantics are lost in the transition from ...
Alex's user avatar
  • 1,233
12 votes
7 answers
7k views

Should rectangle inherit from square? [duplicate]

So, we're all probably familiar with the example provided in most textbooks of the Liskov substitution problem involving a square inheriting from rectangle. The objection to this approach is that ...
ampsthatgoto11's user avatar
11 votes
5 answers
2k views

Is it a violation of the Open-Closed Principle to update a constant representing a real-world value?

I have a class calculating workers' net yearly income. It has a constant representing a tax percentage. But one day the tax rate has changed, so I need to fix the code. Does the act of fixing this ...
Analysis Paradisys's user avatar
10 votes
4 answers
3k views

Why not apply Interface Segregation Principle to "extreme"

Providing that clients would typically consume just one method, though methods would be conceptually related, why not always apply the Interface Segregation Principle to the extreme and have [many] ...
Den's user avatar
  • 4,847
9 votes
2 answers
853 views

I'm struggling to see how dependency inversion doesn't lead to tighter coupling in lower level modules and less reuseability

I'm refactoring a project I did for my work and I'm trying to apply the SOLID principles to make the architecture cleaner. I've run into an issue with the Dependency Inversion Principle that I can't ...
Ryan Mullin's user avatar
6 votes
3 answers
3k views

Is it poor practice to have a class implement an abstract class without adding any new fields/members/functionality?

I am writing a wrapper for a REST API and I have run into something I have never had to ask myself before. This API is for E-Commerce transactions, it has SALE and RETURN endpoints (and some other ...
maccettura's user avatar
4 votes
4 answers
935 views

Relationship between inheritance and single responsibility principle

Example 1: Let me assume that I have a base class A. Class B extends Class A (Class B is a derived class). Can I conclude, Class B doesn't obey single responsibility principle since it uses the ...
Dharma Teja Bandaru's user avatar
2 votes
3 answers
587 views

UI Adapter Dependency on a Secondary (Driven) Port - Hexagonal Architecture

I am applying the Hexagonal Architecture (Ports and Adapters) to my system and I have noticed a dependency from my primary (driver) side adapter to the secondary (driven) side port. This doesn't seem ...
Guven's user avatar
  • 924
49 votes
3 answers
8k views

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

My team has developed a new service layer in our application. They created a bunch of services that implement their interfaces (E.g., ICustomerService, IUserService, etc). That's pretty good so far. ...
Vin Shahrdar's user avatar
44 votes
11 answers
6k views

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

My colleagues like to say "logging/caching/etc. is a cross-cutting concern" and then proceed using the corresponding singleton everywhere. Yet they love IoC and DI. Is it really a valid excuse to ...
Den's user avatar
  • 4,847
31 votes
9 answers
11k views

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

Let's say, for example, you have an application with a widely shared class called User. This class exposes all information about the user, their Id, name, levels of access to each module, timezone etc....
Jimbo's user avatar
  • 428
29 votes
10 answers
10k views

How many are too many interfaces on a class? [closed]

I would probably consider it a code smell or even an anti-pattern to have a class that implements 23 interfaces. If it is indeed an anti-pattern, what would you call it? Or is it simply just not ...
Jonas Elfström's user avatar
18 votes
3 answers
7k views

How to adhere to the open-closed principle in practice

I understand the intent of the open-closed principle. It's meant to reduce the risk of breaking something that already works while modifying it, by telling you to try to extend without modifying. ...
Aviv Cohn's user avatar
  • 21.4k
16 votes
4 answers
13k views

“Depend on abstractions, not on concretions” what is the exact meaning of this term

I was reading SOLID principles on a website, in which for D - Dependency Inversion Principle it says: “Depend on abstractions, not on concretions” In other words. we should design our software in ...
kulsin's user avatar
  • 275
15 votes
6 answers
4k views

Can the circle-ellipse problem be solved by reversing the relationship?

Having Circle extend Ellipse breaks the Liskov Substition Principle, because it modifies a postcondition: namely, you can set X and Y independently to draw an ellipse, but X must always equal Y for ...
HorusKol's user avatar
  • 4,131
14 votes
2 answers
4k views

Stack extending LinkedList. A violation of Liskov Substitution Principle?

A class LinkedList exists with functions such as add_first(), add_last(), add_after(), remove_first(), remove_last() and remove() Now there is a class Stack that provides functionalities such as push(...
jxvmiranda's user avatar
8 votes
3 answers
529 views

Is a subclass Liskov Substitutable if it disallows the same invocation of the constructor in the child as in the parent?

Say, I have two classes: class FTPClient: def __init__(self, host, port): self._client = FTPDriver(host, port) ... class FTPSClient(FTPClient): def __init__(self, host, port, ...
Eugene Yarmash's user avatar
8 votes
6 answers
5k views

What is more important? SOLID or KISS?

What is more important? SOLID or KISS? To be consistent with SOLID I would have to inject Decorator and Footer classes and create interfaces for them. public class HelloService { String name; ...
cimlihor's user avatar
8 votes
3 answers
2k views

Do setters and getters always break the Single-Responsibility Principle?

As we know, The SRP states that every class should have single responsibility and that responsibility must be entirely encapsulated by the class. But setters and getters do serve another ...
Yang's user avatar
  • 257
8 votes
4 answers
774 views

Dependency Inversion expands the API, results in unnecessary tests

This question has bothered me for a few days, and it feels like several practices contradict each other. Example Iteration 1 public class FooDao : IFooDao { private IFooConnection fooConnection;...
Chris Wohlert's user avatar
6 votes
2 answers
2k views

How to use SOLID principles in this example

I have reading about SOLID principles last week and decided to try to solve real world problem by using them. So I got the problem of real world ecommerce site managing totals. Problem Statement: ...
siddhesh's user avatar
  • 161
6 votes
2 answers
2k views

Aren't decorators easily breaking the ISP?

"The Interface-Segregation Principle (ISP) states that no client should be forced to depend on methods it does not use." The decorator pattern is a design pattern to decorate a method of a class. For ...
Máthé Endre-Botond's user avatar
5 votes
2 answers
774 views

Single responsibility vs procedural programming [duplicate]

Single responsibility (from SOLID) is like making me create classes with only one public method. But if it's so, it would be possible use static methods, and go back to procedural programming. What's ...
SerG's user avatar
  • 502
5 votes
1 answer
424 views

Choosing between Dependency Inversion and Dependency "Delegation" to a third module

Suppose I have MasterPackage containing a Master class, and BlasterPackage containing Blaster class. Since Master needs a Blaster to work, the higher level MasterPackage depends directly on lower-...
heltonbiker's user avatar
  • 1,048
4 votes
3 answers
2k views

Workflow of Test driven development to follow SOLID principles

I'm a bit confused with TDD+SOLID. 1) I've watched MVA course about test driven development https://mva.microsoft.com/en-US/training-courses/testdriven-development-16458?l=gLxGoEwXC_2306218965 2) I ...
Prokurors's user avatar
  • 191
4 votes
1 answer
448 views

Dealing with a large interface

I'm working on a program that solves a certain type of systems of equations. The main data objects are Equation, Variable, Solution. Then I have this interface, which represents all things that I want ...
Patrik Bak's user avatar