3 ms·
Short answer: to avoid side effects from other parts of the code rippling into the data class. Some objects are inherently mutable and exposing a reference to
by BlackFly 9y ago
Short answer: to avoid side effects from other parts of the code rippling into the data class.
Some objects are inherently mutable and exposing a reference to them allows mutability. You might not have control of all the class definitions in your data domain in order to make them immutable. Instead, a getter and setter can create equal copies and return those.
This is assuming you are talking about getters and setters on immutable objects. If you are talking about mutable objects, then they really need getters and setters to ensure that mutations on the object they return or receive do not affect them.