I'm an independent consultant working in Italy with more than 8 years of experience in designing and developing application for .NET framework, both in windows and Web environments. I'm particularly involved in Continuous Integration strategies, and designing infrastructure of applications, since I'm a lover of pattern and Application Lifecycle Management. I'm a great fan of Communities and I'm a co-founder of DotNetMarche, an Italian community focused on .NET development and I love blogging about technology. Ricci is a DZone MVB and is not an employee of DZone and has posted 42 posts at DZone. View Full User Profile

Getters are an Antipattern?

  • submit to reddit

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.


From http://www.codewrecks.com/blog/index.php/2011/12/16/getters-are-an-antipattern/

Published at DZone with permission of Ricci Gian Maria, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)


Alexandru Repede replied on Wed, 2011/12/21 - 3:08am

Word. And, something to keep in the back of your mind (whenever your thinking of exposing state of a class through some type of setter): tell, don't ask, that's how objects should interact

To requote a starting quote from that link:

Procedural code gets information then makes decisions. Object-oriented code tells objects to do things

Silvio Bierman replied on Wed, 2011/12/21 - 7:10am in response to: Alexandru Repede

That is utter nonsense. It means that no object could react to the state of a different object. A Dog could not chase a Cat if there is no way to tell where the Cat is. If one can only tell objects to do things but can never query them for information then modelling capabilities become really limited.

The amount of bullshit "OO requires X or prohibits Y" claims is overwhelming. There are no absolute rules in OO (which is no more than a paradigm and lacks a formal definition) and since OO can be done in many different languages each language calls for different strategies for good design.

Having getters/setters (which are ambiguous terms on their own) is not a red flag per se. It can be if they are an abstraction/logical mismatch with the rest of the public interface.

Good OO design is hard. General rules like Demeters law and established patterns can help in making the right design decisions, but good judgement and taste remain essential and usually come with experience.

Calling out mantras, however, is pointless.

Mladen Girazovski replied on Wed, 2011/12/21 - 7:34am

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.

I think that you're mxing up two OOP concepts here, what you describe in the first sentence is called Information Hiding, not Encapsulation.

Apart from that there is a deifference between ""simple & dumb" Datastructures and "real" Objects.

For datastructures (ie. JavaBeans, TransferObjects, etc. ) it is perfectly valid to have accessors and mutators for every field and mainly ignoring encapsulation and information hiding, for real Objects it is mandatory.


Sandeep Bhandari replied on Wed, 2011/12/21 - 12:17pm

If one really wants to make the object as immutable then don't allow access to member variables of class but that doesn't mean you can't use getters to send a copy of the member variables which will not effect the immutability of the source object.Java Thread Scheduler

Steven Goldsmith replied on Thu, 2011/12/22 - 3:15pm

Using this train of thought one would have to eliminate DTOs (formaly known as VOs), JavaBeans, etc.! How then would you propose to replace this type of data container?


Ash Mughal replied on Thu, 2012/01/26 - 2:07am

I never knew and thought that getter and setters are antipattern.

I always believe that these are the core of OOP and to support encapsulation.

IF you hide everything from outside and are not providing any interface then how can couple or integrate different modules?

what is the purpose of an object then?

advanced java

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.