3 ms·
So, let's suppose you don't create getters or setters, because all that function does is get/set the value. Six months later, you discover that you, in fact, d
by vkou 9y ago
So, let's suppose you don't create getters or setters, because all that function does is get/set the value.
Six months later, you discover that you, in fact, do need to do something besides getting/setting the value.
If you created a getter/setter, you could just change its code. If you, instead, had raw access to the field, you now need to update every single instance of client code that called your getter/setter.
Most of the time, your compiler/VM can optimize the getter/setter call away, and a bunch of trivial get/set logic isn't exactly the worst source of cognitive overhead.
- kjksf 9y agoThere are infinite possible futures but only one now. Write the best possible code needed today. Anything else is premature abstraction. I do get the "what if" temptation, I just think it should be actively resisted.
- osteele 9y agoSee [YAGNI](http://wiki.c2.com/?YouArentGonnaNeedIt http://wiki.c2.com/?YouArentGonnaNeedIt). A helpful (to me) metaphor is that you're paying for an option. (In the finance sense, not the type system sense.) Sometimes it's worth buying an option, sometimes it's not, but don't think it's free.
- astrobe_ 9y agoA more simple analogy: it's an insurance. It's certainly wise to subscribe an insurance for your house, but for every single drinking glass you own...? If it's a tea set you inherited from your great-grand-mother, when you break it the insurance won't un-break it. If your property suddenly needs to be more than a property, chances are that this change will break the LSP or trigger a larger redesign anyway.
- MrBuddyCasino 9y agoThis not one of those instances though. The cost of having getters/setters is essentially zero, the potential downside can be big.
- rakoo 9y agoOk, reality time: out of all the getters/setters you've ever written, how many of them have you ever needed to modify? And for those modifications, how long would it have been to refactor your code base to go from public field access to publi method access ?