News for hackers. Stuff that matters.
Re:Ask HN: Why do you make class members private? by thefz on Monday July 25, 2022 @02:51PM (#32229181)
Encapsulation is a founding principle of OOP. You should not be able to directly tool with a class' internals, but use its methods to interact with it.

The practical reason is that this way you have limited paths (methods) with which the inner workings of a class can be modified with, whereas if anything would be public, any other obejct would be able to change a class' status rendering debugging incredibly more complicaed.

This is also a part of the Open-Closed principle: "software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification" https://en.wikipedia.org/wiki/Open%E2%80%93closed_principle

Re:Ask HN: Why do you make class members private? by sshine on Monday July 25, 2022 @04:39PM (#32230595)
> Encapsulation is a founding principle of OOP.

The more fundamental principle that predates OOP is: Abstraction

https://en.wikipedia.org/wiki/Abstraction_(computer_science)