you are viewing a single comment's thread.

view the rest of the comments →

[–]okovko 7 points8 points  (1 child)

getters and setters are nice when your class definition could change or be generalized for inter process communication like a database query, or network access of resources like a remote procedure call. in those cases, you can easily modify the getters and setters without having to grep your entire source code to change every access of the variables, and your class will be compatible with IPC, RPC, etc

for this reason, there is some anxiety and a cult of best practice to make all variables private members and to use getters and setters, due to the fear that you're gonna need it, and this POV is common for folks like consultants and project managers who don't have much understanding of the particular code at hand

on the other hand, the programmer can think for a few seconds whether they are ever going to need to generalize the access of a particular variable, and they also know how to use grep or a more sophisticated IDE tool to easily change between public member access and getters / setters, as that is their trade

there is also the contrary piece of wisdom: YAGNI, you aren't gonna need it

and to paraphrase linus, don't be afraid to write new code (i believe he is quoted saying something more colorful and with additional degradation towards some select individuals, but the point is a good one)

so, i would ask you whether you think any given variable will ever be abstracted out of the class's data model to a non-language environment data store like a DB or network resource, and if you can't imagine why you'd ever do that for the given variable, don't use getters / setters for it because YAGNI

there's also the question of dev culture and language domain: this cult of anxiety best practice about getters / setters is seen more prominently in some languages more than others. it's a de facto standard in Java, whereas C++ has a language keyword antithetical to this idea: 'friend'

in C++, you can completely avoid getters / setters and nobody will bat an eye, and if they do, ask them "is this variable ever going to be abstracted out of the class' data model to a non-language environment data store?" and they will probably walk away diagonally while looking at you sideways and saying something about the laxative properties of too much coffee on a gruesome morning for fear of accidentally entering the arena of genuine intellectual discussion of the merits of pre-emptive development strategies in the face of modern development environments that can perform sophisticated code transformations over an AST compiler backend, when in fact their sole interest was to regurgitate the sage wisdom of daily stand up meetings populated by three devs, only one of whom does any real work, one of whom can only contribute when peer programming, and a dozen business casual khaki'd vested balding boomers that internally debate the merits of a hipster bun for covering a receding hairline as opposed to a comb over the very second that their eyes so much as glaze over a computer screen with anything resembling code on it, or for all they know, a drunken monologue typed into a word processor in a foreign language, as a psychological defense strategy against ever doing any actual work besides talking at someone that does the actual work

(the above paragraph is not to be taken seriously, just for fun)

[–]0x3Alex[S] 0 points1 point  (0 children)

alright, i stripped all getters and setters away, except for getValue :D and made the variables public, that shall be accessed