Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Follow publication

Code Smell 68 — Getters

Maximiliano Contieri
Dev Genius
Published in
2 min readApr 29, 2021

Photo by Vidar Nordli-Mathisen on Unsplash

Problems

  • Naming
  • Information Hiding
  • Coupling
  • Encapsulation Violation
  • Mutability
  • Anemic Models

Solutions

  1. Avoid Getters
  2. Use domain names instead
  3. Protect your implementation decisions.

Sample Code

Wrong

Right

Detection

Getters coincide in certain scenarios with a true responsibility. It will be reasonable for a window to return its color, and it may accidentally store it as color. So a color() method returning the attribute color might be a good solution.

getColor() breaks bijection since it is implementative and has no real counterpart on our mappers.

Most linters can warn us if they detect anemic models with getters and setters.

Tags

  • Information Hiding

Conclusion

Getters and Setters are a poorly established practice. Instead of focusing on object behavior (essential), we are desperate to know object guts (accidental) and violate their implementation.

Relations

More info

The value of a prototype is in the education it gives you, not in the code itself.

Alan Cooper

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Published in Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Written by Maximiliano Contieri

I’m a senior software engineer specialized in declarative designs. S.O.L.I.D. and agile methodologies fan. Maximilianocontieri.com

No responses yet

Write a response