Getters are an antipattern?

When you realize that property setters is an antipattern, because you want to protect the status of your entity from direct external manipulation, the next step is starting to believe that even Getters can be considered an Antipattern.

This is some sort of extreme object oriented thinking, because getters does not modify the status of an object, so there is nothing evil in them… or not? If you look for the definition of Encapsulation from Wikipedia you can find this:

encapsulation means that the internal representation of an object is generally hidden from view outside of the object’s definition. Typically, only the object’s own methods can directly inspect or manipulate its fields.

If you read it carefully you can find that a well encapsulated object completely hides his status from the external world. One of the most interesting reason why you should avoid external world to read your internal status with getter is avoiding other object to do logic that belong to you. Whenever an object expose, even in read-only, part of its internal status, there is the risk that some other object uses that status to implement some logic and this is bad for many reasons.

First of all this is a symptom that logic is probably not in the right place. If you need to know the internal status of another object to implement something, probably that logic should be in other object.

Another drawback is that objects become to be too much entangled, when you modify the status of an object, if it is completely hidden to the outside world, you need only to take care on the logic inside the object; on the contrary, if the status is exposed with getters, you need to search all the usage of the status before modification.

This does not mean that you should use no getters whatsoever, but you surely need to think twice when you want to expose some part of the status, even if in read-only.

Gian Maria.

Published by

Ricci Gian Maria

.Net programmer, User group and community enthusiast, programmer - aspiring architect - and guitar player :). Visual Studio ALM MVP

4 thoughts on “Getters are an antipattern?”

  1. Hi Gian!

    Great post ! I couldn’t agree more with you. But I don’t think it is too extreme. When you see a bunch of classes with a lot of getters, you often find some other classes which hold no data, but only manipulate these getter-classes. These classes messes with the internals of the getter-classes, taking on a responsibility which is not theirs. So when one see a bunch of these classes, then it becomes a true activated anti-pattern.

    (There are a few situations where the framework you work with forces you to have getters (and even setters) like serialization and data binding, but the properties should be left for that purpose alone. )

  2. 100% agree with you :). If you work in WPF with MVVM, you cannot avoid having public properties, the article refers only to the classes of the Core Domain.

    There also situation where even in the core domain classes having getters (and even setters) is not a problem. If I have a Customer class that is part of a DDD-like model, it can have a “picture” property with getter and setter and I’m happy with it. Setters and getters are antipattern for all those properties that are involved in Domain Logic, and in the previous domain, Picture is not involved in any logic so I’m fine with the Customer Object to have getter and setters :).

    Thanks for the comment.

  3. I concur, i recall having a big discussion with a colleage about this ( and why ORM’s can be evil ).
    Nice post :)

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.