1

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.

Gregory Fenn
  • 460
  • 2
  • 13
  • 1
    SOLID is just one charismatic person's well marketed perspective. I enjoyed looking at SOLID in the context of other principles, for example at http://principles-wiki.net/collections:solid -- When you dive deeper, you see that SRP is a specialization of high cohesion, etc. Design is a really hard problem because of all the forces at play and their interaction (they really can be opposing). I find discussing relationships to not be as useful (it generates heat) as looking at real problems (to see the light). – Fuhrmanator Oct 21 '21 at 20:10
  • @Fuhrmanator I didn't want to get too deep into opinion in my answer, but I believe that the mnemonic has played a not-insignificant part in the popularity of SOLID. – joelmdev Oct 22 '21 at 16:07
  • @Fuhrmanator, to be fair, Bob Martin did not invent the five principles all by himself. The OCP comes from Bertrand Meyer. The LSP comes from Barbara Liskov. The SRP is a combination of older principles that Martin credits to at least five previous authors. And the DIP he describes as a result of applying the OCP and the LSP together. So the only principle that Martin really invented is the ISP, and he has called it the least important of the five. "Just one person's perspective" is selling SOLID a bit short. It would be more fair to call it "collected wisdom" from several (brilliant) people. – jaco0646 Oct 29 '21 at 13:22
  • @jaco0646 The wiki I cited supports your comment 110%. You'll find references to all those people's works and more. I call these "marketed" perspectives, because it means people have published books and given passionate talks about their [collections](http://principles-wiki.net/collections:start): GRASP (Larman), GoF, OOSC (Meyer), Pragmatic Programmer (Hunt, Thomas), Unix Philosophy (Raymond). – Fuhrmanator Oct 29 '21 at 15:04
  • `it seems like they overlap a bit`: yep, I completely agree. – Refael Sheinker Feb 12 '22 at 19:10

2 Answers2

1

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.

jaco0646
  • 15,303
  • 7
  • 59
  • 83
0

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.

joelmdev
  • 11,083
  • 10
  • 65
  • 89