Required Reading: "How Non-Member Functions Improve Encapsulation"

Jonathan M Davis newsgroup.d at jmdavisprog.com
Sun Oct 29 09:12:01 UTC 2017


On Sunday, October 29, 2017 08:45:15 w0rp via Digitalmars-d wrote:
> I've noticed the benefits of writing non member functions in
> Python codebases. Say if you have a User model in a Django ORM,
> and you have a Thing model, and some operation on User and Thing.
> I've noticed that your code is almost always better if you write
> a non member function on User and Thing, instead of a member of
> User or Thing.

Yeah, making functions generic can be a big win. The bigger question is what
to do when it doesn't really make sense to make the function generic, and it
doesn't need access to the private members of the type that it would always
be used with. In that case, it doesn't need to be a member function, but it
could be.

- Jonathan M Davis



More information about the Digitalmars-d mailing list