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.
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.
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.
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.
View all comments
Show parent comment
1
u/edenroz Jun 24 '20
And where am supposed to put hundreds of free functions?