DEV Community

Discussion on: Masturbatory Code in OOP

Collapse
 
efpage profile image
Eckehard

Hy Adam,

It´s true, you can write Masturbatory Code in any language, but why should you do that? A=A ist also useless. I assume, it´s not THAT funny.

Getters and setters are just a language feature you can use or not. In OOP it´s generally not the best style to expose a class variable to the public, so a getter and setter can give you more control. You could add a value check or initiate any action before or after the value was changed. So, your class can handle a state change while the user of the class justs sets a variable.

I assume you will find useless code very often. Maybe someone just added a setter and getter out of routine and did not think about. Or he wanted to add more code later, or... . So, this is not a bug, but useless code. But does this mean, setters and getters are useless? I suppose, that´s not what you ment.