Is defining get/set methods for every field overkill?

thebluepandabear therealbluepandabear at protonmail.com
Sat Nov 19 04:37:51 UTC 2022


On Saturday, 19 November 2022 at 04:27:14 UTC, []() {}() wrote:
> On Saturday, 19 November 2022 at 04:19:01 UTC, thebluepandabear 
> wrote:
>>>
>>> oh. so i get it now. you have to refactor your class (change 
>>> member variable names and also do it in all places where they 
>>> are used througout the class. then add new methods, 
>>> overloading them in this way and that way, all because you're 
>>> initial design never factored in the possibility of change 
>>> (or even some validation of the vale being returned to the 
>>> client, or validation of value coming from the client).
>>>
>>> after 10 years of doing all that, you may well come to the 
>>> conclusion that public member variables are not such a great 
>>> idea afterall ;-)
>>
>> These days with modern IDEs it takes a second to change the 
>> name of a variable globally. In production level code, it may 
>> take more time, but I doubt by a lot.
>>
>> Think about it, if you have a class with 20 different 
>> variables that don't need any special rules to access, think 
>> about the amount of code you would have to add for 
>> getters/setters. Now in production level code you will have 
>> thousands of these classes, and as such you will have a good 
>> chunk of code that is practically useless and doing nothing.
>
>
> By making your class member variables public, it is not clear 
> whether you forgot that you needed to validate incoming and 
> outgoing values, or whether you don't need to do this (not 
> ever).
>
> If you did it because of the former, you're fired ;-)
>
> If you did it because of the latter, you're also fired ;-)

Thankfully I only code in D as a hobby, so I don't need to use 
getters/setters! Thanks.


More information about the Digitalmars-d-learn mailing list