3 ms·
> I'm not talking about defining an API for other people to use that don't have access to the code, like when you make a library. It's for the same reasons. Wh
by fny 4y ago
> I'm not talking about defining an API for other people to use that don't have access to the code, like when you make a library.
It's for the same reasons. When you write code you are always writing code that some other human will consume through an interface of sorts. Even on small three person projects two truths hold:
- Your project mates are other humans.
- Your future self is an other human.
By defining members as private you're announcing in code to other humans that the private members are important only to the internal implementation of the class and should not be used elsewhere. And yes, your future self with thank you for the distinction.
Now, you don't need to have private methods. In some languages, a work around is to label the methods as "private" by adding a prefix of some sort like an underscore.
Another more functional reason has to do with inheritance. Private methods have different behaviors in different languages when inherited.