No brain-dead accessor functions
2 min read
Update (2020-08-07): This is a republish of my third blog post, dated 2005-04-20: No brain-dead accessor functions. Devblog only lets me backdate this to 2015.
I know this is a controversial one that many people disagree with me about, but I don't believe in using accessor functions that simply return a field/variable, or set a field/variable. I see it as a pointless exercise in object-oriented programming that serves to complicate things and barely makes anything easier.
My primary reason for hating these is that it makes for harder debugging of code that contains them. With a GUI debugger, you can click on a simple field that's used within the code and it will print out the value. You cannot, however, click on an accessor function and have it print out the value of the field it accesses.
Another reason for not liking these is that I'd like to think of functions/methods as simplifying a block of code either for ease of reading, or so it can be used by more than one caller. So when I see a function/method call, it implies that something useful is going to go on when it is called. Therefore, if I'm debugging code I don't know like the back of my hand, I may look at the function or at least guess its purpose by its name. An accessor function, then, is simply a distraction as I have to think about it long enough to determine that it's not really doing anything.