Home > Mobile >  What are the relationships between the five SOLID principles?
What are the relationships between the five SOLID principles?

Time:10-21

This has been bugging me for ages. SOLID is a set of five principles for maintainable and extensible development, often favoured for large projects in C# etc.

But are there really five principles? In other words, is it possible to write code that satisfies 4 but not 5 or the principles? And is this true for any choice of 4 principles?

The reason I ask is I struggle to fully separate the logic of all of them and it seems like they overlap a bit, which makes me doubt whether all five rules are needed, or whether SOLID could be described more succinctly with, say, three principles.

CodePudding user response:

Think of the SOLID principles as a complementary toolkit, not a single conceptual unit. Martin has even ranked the principles in order of importance (podcast link). They are distinct principles with distinct purposes, but the larger an application gets the more likely it is that you're going to have opportunities to leverage all five.

Also keep in mind that there are tons of other design patterns, practices, and principles out there that will help you write better code and design better systems- SOLID is just the tip of the iceberg.

CodePudding user response:

You certainly can conceive of different sets of principles, and people have: for example, GRASP and CUPID.

From Robert Martin's perspective, SOLID are distinct and any four can be applied without the other. On a bit of a tangent, the most closely related two are probably OCP and DIP. In particular, the DIP is an extension of other principles (in Martin's own words) but obviously an extension that he felt worthwhile calling out; and even the DIP we can pick apart from the OCP. The others are relatively easy to isolate.

That is not to say there is no overlap. Indeed, it would be odd if foundational programming principles had no relationship to each other. But clearly the idea is that each one contributes something unique.

Of course you could condense the five down to something less if you tried, but I think the SRP is a cautionary tale here. It is already a combination of several older principles including coupling, cohesion, and separation of concerns. Condensing these principles into one (presumably more general principle) has, in my opinion, resulted in the most confusing aspect of SOLID already.

Imagine if there was only one principle. Wouldn't programming be easy then? Alas, that principle would be so vague and generic as to be inapplicable. Consider the opposite: SOLID could be expanded into a dozen principles, or a hundred. It would no longer fit on a bumper sticker, so not great for marketing and selling books, but it would more realistically capture the complexity of software design.

I don't think any of the authors of these collections of principles would insist that their collection is exhaustive; but I do think you can learn something from each principle.

CodePudding user response:

it seems like they overlap a bit, yep, I completely agree.

  • Related