DEV Community 👩‍💻👨‍💻

Discussion on: Public vs. Private vs. Protected

lietux profile image
Janne "Lietu" Enberg

The answer is simple: if you ever think of using private you are wrong, and you should change your mind.

Languages with private are a whole lot more annoying to work in, because of people who use private think they're smart enough to make that decision. Then there's a bug, that could easily be fixed by extending the class and calling a protected function, or similar and instead you have to fork their code to fix it, and if the dependency tree is large enough it becomes pretty much impossible. Sometimes you can at least use reflection to call "private" functions regardless.

This has happened countless times over an extended period of time and will never cease to happen, because people will never stop thinking they are smarter than they are.

Simply put, imo private keyword should not exist in any languages, and protected should be changed to mean "you should not normally call this, but call it if you think you really need to".