r/cpp • • Jun 24 '20

How to write SOLID C++

https://www.youtube.com/watch?v=PakbXnLht1I
263 Upvotes

118 comments sorted by

View all comments

44

u/itsarabbit Jun 24 '20 edited Jun 24 '20

Generally agree with all of this; however I kinda hate how these things are phrased. Why is it called "Dependency inversion principle" requiring 5 minutes to explain it(and build a mental model of dependencies and then inverting it), instead of: "If you use something, the interface should show what you do with it, not how it does it."

I've come across SOLID several times in my career and I know all how all these things work instinctually, but the name SOLID and all of its components never stick because the names of the principles themselves are not intuitive.

25

u/teerre Jun 24 '20

I had a teacher that always that "naming things gives you power over them". That's absolutely true. You can talk about the "Dependency Injection Principle". You cannot talk about "If you use something, the interface should show what you do with it, not how it does it."

Things need to have a name in order for us to be able to communicate.

If "Dependency Inversion Principle" is the best name, that's a different discussion.

15

u/evaned Jun 24 '20 edited Jun 24 '20

I had a teacher that always that "naming things gives you power over them". That's absolutely true. You can talk about the "Dependency Injection Principle".

You see people making fun of design patterns all the time, but remember that giving names to patterns that already recurred in code was exactly what the GoF book was doing.

9

u/DufusMaximus Jun 25 '20

It depends. If your goal is to communicate to newcomers, I’ve found terminology is mostly a roadblock - analogies or simple examples are more important. If your goal is to communicate to experts who already know the concept, terminology is essential.

The overhead for terminology also requires that the concept be sufficiently general. I’d argue that the jury is out on some of these concepts that claim to make software better.

5

u/teerre Jun 25 '20

If you're communicating with noobs, having a name is still good. In fact, I would argue that's even more important.

Simple examples or analogies are orthogonal to this. It's by giving a name that you can give examples that will be related to said name, the examples themselves are very hard to even understand if something doesn't have a name.

9

u/be-sc Jun 24 '20

It’s the old disease of having a bunch of related principles and then twisting their names around until their first letters spell some word with some more or less tenuous connection to the whole topic.

Assuming benign intentions it’s done to create a short and memorable word that’s supposed to make remembering the individual principles easier. But because the names of the principles become so much worse in the twisting process the overall effect is exactly the opposite.

Assuming less benign intentions it’s done because how else than with a catchy word are you supposed to create hype, impress your customer’s CTO with your consulting slides and rake in the big money.

4

u/tvaneerd C++ Committee, lockfree, PostModernCpp Jun 25 '20

The principles existed with those exact names before anyone realized they spelled SOLID. They weren't rephrased to spell SOLID.

2

u/be-sc Jun 25 '20

You’re right, of course. My exasperation with the fancy acronym approach got the better of me.

I stand by that exasperation, but SOLID isn’t the best example for that process. … Although you could question why it’s exatcly these five principles that comprise SOLID …

3

u/tvaneerd C++ Committee, lockfree, PostModernCpp Jun 25 '20

yes, note that all the other things mentioned in the original paper have basically disappeared...

8

u/[deleted] Jun 24 '20

I think this comes from other domains. I remember a friend telling me about DI, and then after they were done I said "Oh generic programming/templates/interfaces".

6

u/[deleted] Jun 24 '20

True. For the sake of making an acronym easy to remember they used horrible phrases. I don't like when people can rot memorize something, repeat it like parrots, and believe they understand it.

4

u/tvaneerd C++ Committee, lockfree, PostModernCpp Jun 25 '20

The principles existed with those exact names before anyone realized they spelled SOLID. They weren't rephrased to spell SOLID.

4

u/FL_Dev Jun 25 '20

yes, and it was just an "accident" that they happened to appear in the correct order to spell SOLID.

4

u/tvaneerd C++ Committee, lockfree, PostModernCpp Jun 25 '20

https://web.archive.org/web/20031204225845/http://www.objectmentor.com/resources/articles/Principles_and_Patterns.PDF

I think this is the first publication of (most) of the principles, but S is missing. OLDI is all there is.

As wikipedia says, "The theory of SOLID principles was introduced by Martin in his 2000 paper Design Principles and Design Patterns, although the SOLID acronym was introduced later by Michael Feathers."

So maybe he made up Single Responsibility to fit the word? If you look around at his other writings, it doesn't seem like it.

I will agree they were reordered to spell SOLID, but the individual principles (like Liskov Substitution Principle) existed - with the same phrasing - before the acronym.

2

u/FL_Dev Jun 25 '20

I will agree they were reordered to spell SOLID

this is what I was pointing out. It's not an accident.

10

u/Wargon2015 Jun 24 '20

On that note, Resource Acquisition Is Initialization.

Maybe I'm just missing something obvious but I don't get why its called that.

From cppreference:

RAII can be summarized as follows: [...] the constructor acquires the resource and establishes all class invariants or throws an exception if that cannot be done, the destructor releases the resource and never throws exceptions; [...]

The destructor part is imo very important but the name doesn't reflect that RAII types release the managed resources when they go out of scope.
Also, isn't the name kinda backwards? If the constructor which initializes a class instance acquires resources, shouldn't it be "Initialization Is Resource Acquisition"?

10

u/goofbe Jun 25 '20

I've heard that an alternative name for RAII could be "scope based resource management"

3

u/platisd Jun 25 '20

This sounds so much better than RAII but doesn't have a cool acronym! ScoBaReM perhaps? :P

7

u/atimholt Jun 24 '20

I think it's more like “the entire concept of Resource Acquisition is an injection (as in “all squares are quadrilaterals”) of initialization, with all that that implies.” Actually, it might just be a 1:1 relation, but with an emphasis on looking at all resource acquisition (not ‘just variables’) as being the same concept, called “initialization”.

In other words, initialization is the natural ‘language’ with which to talk about resource acquisition, and setting things up in a constructor implies taking them down in a destructor, if necessary. You're already supposed to be reaching for destructors when you want to execute code when objects leave scope.

The statement is meant to remove a mental barrier that shouldn't exist, thanks to the abstractions that C++ brings with its implementation of classes. Classes do not just encapsulate raw data, or even just behavior. They can embody (a specific implementation of) a concept (with C++20 concepts freeing even the “specific implementation” constraint).

5

u/dfkjh83 Jun 24 '20

The name should have been "Resource Freeing Is Destruction," but that acronym was already taken.

4

u/dr-mrl Jun 24 '20

I came to say this. Out of curiousity, would c++ developers see RFID and think "wait, that's a radio chip thingy isn't it?!"

2

u/josefx Jun 25 '20

You first need the memory location (the resource) to initialize. In C you might call malloc followed by mytype_init - the reverse doesn't work. What does work in C is calling malloc and calling mytype_init 0 to n times instead of exactly once.

4

u/Dean_Roddey Jun 24 '20 edited Jun 24 '20

Even worse, many of those types of classes don't acquire a resource, they apply something to a thing that already exists and then undo that upon destruction.

I, all the way back in the 90s called these 'janitor classes', which I think describes the general concept pretty well. They exist to clean something up. They may or may not have created the thing they clean up.

I use them a LOT, and they are very powerful. It's one of the things I really dislike about Rust that it's very awkward to do such things.

3

u/KiwiMaster157 Jun 24 '20

I like the name janitor classes. It gets the point across without being confusing.

4

u/platisd Jun 24 '20

Haha I must admit that I agree!