Comparison
EncapsulationvsGetters and setters
Encapsulation
another team reached into the field directly, and now you cannot rename it without breaking them.
Keeping a module's internals private so they stay changeable. The value is not secrecy — it is that whatever you expose becomes a contract you have to honour, and whatever you hide stays yours. Every leaked internal converts a future refactor from a local decision into a negotiation with everyone who reached in, which is the mechanism behind Hyrum's law.
Full entry →Getters and setters
every field is private with a public pair of methods around it, which is the same as public with more typing.
Methods that read and write a field, generated by habit in most object-oriented codebases. A getter that simply exposes a field and a setter that simply assigns one provide no hiding at all; they only preserve the ability to add behaviour later, which is worth something but far less than the ceremony suggests. A blanket set of them is the surest sign of an anaemic domain model, because it means the behaviour that should be inside the object is somewhere else.
Full entry →