3 ms·
I disagree with the author, getters and setters are a good tool to have and used in the right place. Setters are great when you want to do some validation and
by simion314 9y ago
I disagree with the author, getters and setters are a good tool to have and used in the right place.
Setters are great when you want to do some validation and add an entry in a log when the setter is used.
There are rules what not to do in getters or in setters so if you use does practices you are ok.
- striking 9y agoThe author admits you indeed cannot avoid using them sometimes and mentions exactly the validation case as one of the times you end up writing something similar to a setter anyway. You agree with the author but haven't realized it, I think.
- jonhohle 9y agoNo admission is necessary. There is an entire class of immutable languages (typically functional), which don’t allow mutation (and therefore don’t need setters).
- chopin 9y agoI try to avoid setters whenever possible in favor of constructors taking the dependency as parameter. That way, reasoning about the lifecycle of dependencies is much easier.
- simion314 9y agoI agree, if you can make the object imutable by setting all it's fields at construction time then it is great, if you have to update the object then is good to use setters or functions that can perform validation, it makes it easy to catch errors earlier.