Re:Ask HN: Why do you make class members private?
by Jtsummers on Monday July 25, 2022 @02:15PM
(#32228587)
Suppose all fields are public, but the legal value of a field is dependent upon others. Then every time you change it you also have to include all the invariant preserving logic. This is duplication of the insane sort, totally unnecessary and gratuitous and gloriously error prone (at scale). The sane option is to make it private and have one place that checks that the input is valid and have everything make use of that.
[ Parent
| on HN ]
There are no comments on this comment yet.