Feb 22 2006
Getters are Evil?
Yikes.
Consistently behind the curve, I just only recently started reading Martin Fowler’s Bliki.
This morning’s article on GetterEradicators confused me. He’s defending the use of getter methods from those that think that getters violate OO encapsulation.
Huh? They do?
I operate at a pretty basic OO level, I admit to not being the world’s greatest object designer, but getters violate OO? That’s news to me. I’m glad MF is defending the use of getters, as I’m pretty fond of them.
He does point to a bad pattern that I’m relatively guilty of, objects that contain only data. I do this a lot. Then other objects manipulate them. I got into the habit of building SQLObject classes to encapsulate my data, and then building other objects with has-a relationships to those SQLObjects.
That habit came from my inability to figure out how to use SQLObject as a true Object, rather than a wrapper around a database. I’m still a little fuzzy on how you can treat a SQLObject object as a full blown object, complete with methods that manipulate self.whatever.
ActiveRecord objects let me throw in as many methods as I please, with access to self. It’s cut down on my data only objects pretty significantly, which is a good thing (according to MF).
I’m gearing up to do some more SQLObject based work, after a several month hiatus. I think I need to spend some time figuring out how (if it’s possible) to build SQLObject’s that include data manipulators as well as just data. Otherwise, I’ll fall back on my has-a pattern, which I’m not “against”, but feels less clean than having a single object that knows my data, how to manipulate it, and persists it in a database nicely.



