News for hackers. Stuff that matters.
Re:Avoid getters and setters whenever possible by nine_k on Friday March 23, 2018 @12:15AM (#16655025)
The problem is that the typical OOP data declaration is a record, and its fields are not completely orthogonal. Its value space may be much larger than the space of valid combinations of field values.

Hence the need to have setters that would check for invariants and prevent invalid mutations.

Here's the next thing: objects are mutable by default. This is why a setter can be useful: it notices the event of state change and runs other code based on the new state.

When you have e.g. algebraic data types, you already have easier time limiting the space of possible combinations, making (most of) the invalid states unrepresentable.

When your data are immutable, any changes in them are explicit events (see react / redux), and running code on them is also explicit and straightforward.

There are no comments on this comment yet.