4 ms·
They are useless until one day they become extremely useful. In other words most of the time there’s minimal advantages. However by following good form you will
by FollowSteph3 9y ago
They are useless until one day they become extremely useful. In other words most of the time there’s minimal advantages. However by following good form you will eventually come across and instance where you have to do something and instead of having to change code in tons and tons of places, places you may not event have access to such as a public API, you don’t realize the importance. Hopefully you never across the need to do this, it’s pretty rare, but when it happens I can’t tell you how valuable it is.
The benefits are all in code maintenance. Again most of the time it’s useless but when you need I can’t tell you how incredibly useful it can be. And once you come across such an instance you never again ask why or refuse to do it ;)
- FollowSteph3 9y agoLet me give a concrete example from my past. We had a public API of POJO domain objects that could be used to render templated files, think string replace. It worked great until one day someone had a special character that caused it all to fail. How do you fix this? Ask all customers to clean their values? Good luck with that!! However from our side we just added the code to clean the output, escape the special character for the templating engine, and everything worked great. The only other option was to have thousands of customers with live systems edit their templates and or data to fix that one bad character. It affected a lot of people due to another unrelated update. Anyways had we not been forcing the use of setters and getters a solution would have been on a whole other scale! And no we couldn’t edit the template engine, etc, because in most cases it was valid.
- qw 9y agoWhat use did you have from the getter? That seems like a situation where you would only change the setter (or a builder if you use that pattern).
- merb 9y agoor the constructor..
- FollowSteph3 9y agoIn fact you may want to edit the getter because who says you’re the one who controls the setter. As in there may be more than one way to set the value ;)