Firstly, not everyone agrees with this advice. I don't think I saw anyone other than Meyers ( edit: and Herb Sutter ) give this advice, and I saw it only in the context of C ++. For example, creating “non-member functions different from one another” in Java or C # is actually not possible since Java and C # do not have free functions, and Ruby developers (for example) prefer “humane interfaces” that intentionally create member functions who do the same as non-members to simply make life easier for subscribers of these features.
And even if you accept Meyers advice, you should prefer functions other than non-member members to member functions (and I think this is good advice, this certainly helped me apply encapsulation to better think about encapsulation of a class implementation, even from its member functions), to consider only one design axis.
The key concept of object-oriented design is that objects do something . An object is not just a bag of setters and getters, to which another code adds. Instead, it must have attached behavior, that is, it must have methods that do something, and it must encapsulate the details of how this happens. If you follow this approach to OO, then bring Meyers' recommendations to the end, since you did encapsulation rather than helping it: you end up exposing all the internal variables of the class implementation through getters and setters, rather than hiding them so that only the class methods (the code responsible for executing the material on behalf of the class, which is the only reason you are starting to start) can get to it.
So, to answer your question about how far to take Meyers advice: it is not necessary to unnecessarily turn functions into member functions if they can be reasonably implemented as functions that are not non-member members using the public interface class, but Do not damage the open interface of the class and break its encapsulation, exposing the implementation simply so as not to make something a member. And make sure that you balance encapsulation against other problems and other approaches (including, if your team decides to take this route, the pros and cons of a full-scale humane interface).
Josh kelley
source share