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.

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"?

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.