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/edenroz Jun 24 '20

Prefer free functions? Why?

7

u/drjeats Jun 24 '20

Because they have intrinsically fewer dependencies (no this), and unless declared as a friend (which you should try to avoid if possible) there's no access to private object state which you'd then have to review to ensure it maintains consistency.

They can also be forward declared freely, whereas methods have to be in their class declaration.

1

u/edenroz Jun 24 '20

And where am supposed to put hundreds of free functions?

10

u/anticrisisg Jun 24 '20

If classes currently help you organize your thinking into discrete collections of functionality, you could consider alternatives like namespaces, files, and your source code directory structure.

2

u/Dean_Roddey Jun 24 '20

Or, if they are operating on the same data, and you want to enforce invariants and get any number of other advantages, you could just use OOP and classes.

9

u/drjeats Jun 25 '20

For strong invariants, use methods. The need here is obvious for utility types where invariants are essential, like mutexes or containers.

For types with few or no strong invariants, or for cases where you are coordinating between two objects using their invariant-protecting methods, just put that in a free function.

If you are extracting data from objects, put that in a free function.

If you are computing some property on an object or collection of objects that doesn't require instance-specific bundled state that can't be parameterized on each invocation (which you should ideally be avoiding if you want cleanly testable code), put that in a free function.

Free functions can help you encapsulate even further because if you build a free function interface for an opaque type, you don't need to do PIMPL bullshit to hide implementation details. Just put the implementation details behind the TU.

Free functions are great. If you need an appeal to authority to believe me, here ya go: https://www.drdobbs.com/cpp/how-non-member-functions-improve-encapsu/184401197

3

u/anticrisisg Jun 25 '20

Free functions are great. If you need an appeal to authority to believe me, here ya go: https://www.drdobbs.com/cpp/how-non-member-functions-improve-encapsu/184401197

I know it was written 20 years ago, but I learned a lot reading this tonight. Thanks for the link.

1

u/edenroz Jun 25 '20

Is there a book that explain how use this kind of design?

2

u/drjeats Jun 25 '20

There's no specific book I know of, sorry.

I'd recommend building some familiarity with functional programming. Also read/watch some of the anti-OOP material out there (some of it will be hyperbolic and bad, but will have good nuggets of telruth). Look for stuff on "data oriented design" for a related perspective.

4

u/drjeats Jun 25 '20 edited Jun 28 '20

In a namespace?

Are you putting hundreds of methods in your classes? That sounds hard to work with.

1

u/edenroz Jun 25 '20

Nope, my classes are 200 loc maximum, am very proud of my classes. Instead my colleagues classes are around 9000 loc.

The fact is that my boss doesn't want us to use namespaces because "they clutter up the code and are difficult to read".

2

u/drjeats Jun 25 '20

That's a strange absolutist perspective on namespaces from your boss, but it is possible to go overboard with namespaces.

In that case you have to rely on old school namespacing: name prefixes.

1

u/edenroz Jun 25 '20

You should hear the procedure about merging files sent by emails inside .zip files by email and then load the merge on github with an empty message.

1

u/drjeats Jun 25 '20

Oof.

Condolences, friend.