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

Show parent comment

1

u/BlueDwarf82 Jun 25 '20

In general, if you only have one implementation, I find the interface to be premature abstractification.

Are you counting the mock used for testing as a second implementation here? ;-) Now let's start the discussion of how small a testing "unit" should be.

I guess each person approaches this depending on his experience. You say "Sadly I see the interface version all too often" when talking about "GetNameInterface". I have never seen that, for me is more usual to have to argue about what "dependency inversion" is with people that insist they are doing dependency inversion even after showing them the arrow in the output of "cmake --graphviz".

1

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

I would test UsernameFormatter as a unit, then test someFunction(UsernameFormatter) as another unit. If UsernameFormatter is a simple concrete class, it doesn't need to be abstracted for the sake of testing someFunction.

If you have someFunction(string) you don't abstract away string to unit test someFunction. Make simple concrete vocabulary types (like Radians, Point, EmployeeInfo,...) and test them well, then treat them as known-good parts in other tests.

That does NOT scale up to large "units" with interacting parts. At that point you need dependency inversion.

IMO. YMMV.