When an object changes its state the best solution is to change the attribute, isn't it?
1 - Model states as mathematical set inclusion.
2 - State is accidental, take it away from the object.
- State diagrams
If we want to be extreme, we should consider every setter to be a potential state change. Linters can warn us. But we might end up getting too many false positives.
Performance issues (if a serious benchmark supports it).
Code Smell 20 - Premature Optimization
Maxi Contieri ⭐⭐⭐ ・ Nov 8 '20 ・ 2 min read
This technique is very elegant but can lead to over design. For example changing a visual component its color should be a counterexample to this smell.
We should be aware and very caution like with any other smell.
They are hints and not rigid rules.
Code Smell 28 - Setters
Maxi Contieri ⭐⭐⭐ ・ Nov 19 '20 ・ 2 min read
First make the change easy (warning: this might be hard), then make the easy change.
Top comments (2)
I'd go as far as to say getters and setters are always a sign of bad design in OO code. Their only real purpose in that context is as a hack when business constraints don't allow for a more solid (you can also read that as SOLID) design.
Other than that, they're useful when one needs procedural segments in an otherwise OO codebase, mostly to implement some of the lower levels of the logic. Arrays just don't work well with "tell don't ask".
Both setters and getters should be avoided most of the time.
People use them thinking they are protecting their implementation but is exactly the opposite. Next Sunday's code smell is related to that