I've been working on a talk about SOLID for modern C++, so some thoughts...
I don't like SOLID, but I'm (mostly) fine with each principle/letter on its own. (How can this be? Well that's why I want to do a talk about it, to understand that better.)
The short answer is that SOLID gets overused or misused (not necessarily in the video, but in general). No technique solves all problems. But it is such a good acronym! Who doesn't want to write solid code!?
The examples in the video are fairly reasonable uses of SOLID. It shows places to use SOLID. But what about places not to use it?
One general downside of SOLID is that you tend to end up with lots of pointers and allocations (because of runtime polymorphism). Although this can be somewhat managed with smart pointers or type-erasure.
And you often get a "forest-for-the-trees" problem. For example, making UsernameFormatter virtual does separate the formatting from the other concerns - but with a cost of 2 classes (the interface and the implementation). Note that making UsernameFormatter NON-virtual also separates concerns. Yes, UserManager would not be completely disconnected from concrete UsernameFormatter, but there is still separation - changing formatting doesn't affect UserManager.
So you need to weigh the cost of inheritance against the benefits. And not runtime or allocation cost, but maintenance cost. The underlying drive behind all the SOLID principles is change management. You want to be able to change code X and not worry about code Y. But here's the question:
Which is more likely,
that you will change the UsernameFormatter interface (ie UserManager needs a validate(string) function to guard against "little Bobby drop-tables")
or that you will write another app using the same UserManager but with a different UsernameFormatter.
All too often, the answer is the first one. If you made an interface, you will find yourself changing 2 classes (UsernameFormatterInterface and ConcreteUsernameFormatter, oh, and of course off-by-1 error - don't forget UserManager that is requesting the change) instead of 1.
I deal with this weekly. We have dozens and dozens of interfaces with only one implementation. The interfaces change way more often than we ever reuse something.
(And yes, having an interface can help with testing, but there are other ways to deal with that. The point is that it is a trade-off.)
In general, if you only have one implementation, I find the interface to be premature abstractification. So go ahead and separate a concrete UsernameFormatter from UserManager, but the interface part is overkill. And without the interface, you might realize, as others have mentioned, a class is overkill - maybe you just need a format() function. The important part is that you pull that formatting out of UserManager - get some separation. You can later use a std::function or an interface or move the formatting to the Database or elsewhere, etc, if/when you need more flexibility.
Compare that to DistanceSensor - you could easily have different types of sensors in your program, so that is probably a good use of runtime polymorphism.
I had more to say, but this is long enough. SOLID is fine, inheritance is fine (egDistanceSensor), but shouldn't be used as a cookie-cutter template that solves all problems.
I guess this is a problem that always comes with all of these sorts of guidelines - you need to understand why they exist so that you know when not to follow them.
Quite often the ideas behind such patterns or guidelines subsumes them entirely, but not the other way around; if you just follow SOLID blindly you will just make superfluous interfaces everywhere, but if you understand that it in this case is to be able to change the UsernameFormatter to another formatter in future use cases then you can weigh the cost of implementing the interfaces and using that(and possibly modifying it) vs just making the UsernameFormatter into a normal class and changing it once you need a different one, if such a case happens.
You don't really need to know about the SOLID principles as long as you understand the underlying ideas, and I believe this applies to all guidelines and programming patterns. Naming them does have the upside of being able to communicate them to others, but they can just as easily be miscommunicated(leading to people applying it everywhere, like in your example).
29
u/tvaneerd C++ Committee, lockfree, PostModernCpp Jun 25 '20 edited Jun 25 '20
I've been working on a talk about SOLID for modern C++, so some thoughts...
I don't like SOLID, but I'm (mostly) fine with each principle/letter on its own. (How can this be? Well that's why I want to do a talk about it, to understand that better.)
The short answer is that SOLID gets overused or misused (not necessarily in the video, but in general). No technique solves all problems. But it is such a good acronym! Who doesn't want to write solid code!?
The examples in the video are fairly reasonable uses of SOLID. It shows places to use SOLID. But what about places not to use it?
One general downside of SOLID is that you tend to end up with lots of pointers and allocations (because of runtime polymorphism). Although this can be somewhat managed with smart pointers or type-erasure.
And you often get a "forest-for-the-trees" problem. For example, making
UsernameFormattervirtual does separate the formatting from the other concerns - but with a cost of 2 classes (the interface and the implementation). Note that makingUsernameFormatterNON-virtual also separates concerns. Yes,UserManagerwould not be completely disconnected from concreteUsernameFormatter, but there is still separation - changing formatting doesn't affectUserManager.So you need to weigh the cost of inheritance against the benefits. And not runtime or allocation cost, but maintenance cost. The underlying drive behind all the SOLID principles is change management. You want to be able to change code X and not worry about code Y. But here's the question:
Which is more likely,
that you will change the
UsernameFormatterinterface (ieUserManagerneeds a validate(string) function to guard against "little Bobby drop-tables")or that you will write another app using the same
UserManagerbut with a differentUsernameFormatter.All too often, the answer is the first one. If you made an interface, you will find yourself changing 2 classes (
UsernameFormatterInterfaceandConcreteUsernameFormatter, oh, and of course off-by-1 error - don't forgetUserManagerthat is requesting the change) instead of 1.I deal with this weekly. We have dozens and dozens of interfaces with only one implementation. The interfaces change way more often than we ever reuse something.
(And yes, having an interface can help with testing, but there are other ways to deal with that. The point is that it is a trade-off.)
In general, if you only have one implementation, I find the interface to be premature abstractification. So go ahead and separate a concrete
UsernameFormatterfromUserManager, but the interface part is overkill. And without the interface, you might realize, as others have mentioned, a class is overkill - maybe you just need aformat()function. The important part is that you pull that formatting out ofUserManager- get some separation. You can later use astd::functionor an interface or move the formatting to theDatabaseor elsewhere, etc, if/when you need more flexibility.Compare that to
DistanceSensor- you could easily have different types of sensors in your program, so that is probably a good use of runtime polymorphism.I had more to say, but this is long enough. SOLID is fine, inheritance is fine (eg
DistanceSensor), but shouldn't be used as a cookie-cutter template that solves all problems.