DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Coding
  3. Languages
  4. Getters are an Antipattern?

Getters are an Antipattern?

Ricci Gian Maria user avatar by
Ricci Gian Maria
·
Dec. 21, 11 · Interview
Like (0)
Save
Tweet
Share
6.40K Views

Join the DZone community and get the full member experience.

Join For Free

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/

Object (computer science)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • The 5 Books You Absolutely Must Read as an Engineering Manager
  • Create Spider Chart With ReactJS
  • Is DevOps Dead?
  • Key Elements of Site Reliability Engineering (SRE)

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: