DEV Community

Discussion on: Who of you use getters & setters and when?

Collapse
 
kspeakman profile image
Kasey Speakman

Often getters and setters are a code smell called Inappropriate Intimacy in Object-oriented Programming. Another reference here.

Our legacy code makes extensive use of getters and setters, but that is really just procedural code operating on shared data. That's part of the reason it is considered legacy... because things are too tightly coupled and hard to change.

I sometimes will use objects with only getters and setters for "data buckets." These are temporary objects to hold raw data until I can validate it and convert it into a real object with behavior.

I often find that UI frameworks require the use of getters and setters for configuration / display purposes. This eventually leads me to create completely separate "data bucket" objects specifically for UI views.

Full disclosure: I don't use OOP paradigm much anymore. But I wish past me had considered getters and setters a code smell.