DEV Community

Cover image for Code Smell 68 - Getters
Maxi Contieri
Maxi Contieri

Posted on • Edited on • Originally published at maximilianocontieri.com

1 1

Code Smell 68 - Getters

Getting things is widespread and safe. But it is a very bad practice.

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

<?php
final class Window {
public $width;
public $height;
public $children;
public function getWidth() {
return $this->width;
}
public function getArea() {
return $this->width * $this->height;
}
public function getChildren() {
return $this->children;
}
}
view raw getters.php hosted with ❤ by GitHub

Right

<?
final class Window {
private $width;
private $height;
private $children;
public function width() {
return $this->width;
}
public function area() {
return $this->height * $this->width;
}
public function addChildren($aChild) {
// Do not expose internal attributes
return $this->children[] = $aChild;
}
}
view raw goodmodel.php hosted with ❤ by GitHub

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

Credits

Photo by Vidar Nordli-Mathisen on Unsplash


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

Alan Cooper


DevCycle image

OpenFeature Multi-Provider: Enabling New Feature Flagging Use-Cases

DevCycle is the first feature management platform with OpenFeature built in. We pair the reliability, scalability, and security of a managed service with freedom from vendor lock-in, helping developers ship faster with true OpenFeature-native feature flagging.

Watch Full Video 🎥

Top comments (2)

Collapse
 
jayjeckel profile image
Jay Jeckel

Method names should start with verbs. width() isn't a width, it is a method that gets a width and should be named appropriately, ie getWidth(). That goes doubly so for area() which not only isn't an area, it isn't even getting an area, it is calculating an area and the name should reflect that, ie calArea(). You using property naming conventions for methods is a bigger smell than anything you're pointing out.

And I don't know what kind of systems you work in, but a Window class that doesn't allow access to its list of children is best described as nearly useless.

Collapse
 
mcsee profile image
Maxi Contieri

what is 'cal' ?

I have asked my child what is 'calArea' and she laughed. She's perfectly aware of what is an area. And she has a friend called 'Cal' btw.

I work in a world class software company and have looooots of windows, we don't access objects since we believe in encapsulation and information hinding principles.
Windows are ok without being accessed

Feature flag article image

Create a feature flag in your IDE in 5 minutes with LaunchDarkly’s MCP server ⏰

How to create, evaluate, and modify flags from within your IDE or AI client using natural language with LaunchDarkly's new MCP server. Follow along with this tutorial for step by step instructions.

Read full post

👋 Kindness is contagious

Discover fresh viewpoints in this insightful post, supported by our vibrant DEV Community. Every developer’s experience matters—add your thoughts and help us grow together.

A simple “thank you” can uplift the author and spark new discussions—leave yours below!

On DEV, knowledge-sharing connects us and drives innovation. Found this useful? A quick note of appreciation makes a real impact.

Okay