To Hell With Setters and Getters
How Accessor Methods Are a Waste of Time and Often Hinder Encapsulation
At at a former workplace, I asked colleagues: “Why exactly do we bother with setters and getters for our parameter classes?” The reason for the question was that we had classes used for serialization, where every variable had a setter and getter, but there was never any business logic in these classes.
Fortunately I was not put up against the wall and shot for heresy. But nor could anyone actually give a good reason for why we used setters and getters for every single attribute of dumb data objects.
I could of course have asked this question about Java classes, Python classes or almost any other object oriented language. Within software development there are certain beliefs which assume religious conviction.
People will criticize you for not using setters and getters with the argument that: Without setters and getters your class is not properly encapsulated. You make the poor class naked and vulnerable. Let us try to articulate the classic argument in favor of setters and getters:
If you don’t have setters and getters for the properties of your object, then you can’t change the internal data representation of that object without breaking code depending on that object.