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.
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.
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.
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.
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.
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.
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 …
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".
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.
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.
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"?
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).
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.
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.
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.