Code Smell 01 — Anemic Models
Your objects are a bunch of public attributes without behavior.
Published in
Oct 20, 2020
Protocol is empty (with setters/getters).
If we ask a domain expert to describe an entity he/she would hardly tell it is ‘a bunch of attributes’.
Problems
Solutions
1) Find Responsibilities.
2) Protect your attributes.
3) Hide implementations.
4) Delegate
Examples
- DTOs
Sample Code
Wrong
Right
Detection
Detection can be automated with sophisticated linters ignoring setters and getters and counting real behavior methods.
Also Known as
- Data Class
Tags
- Anemic
- OOP as Data
- Encapsulation
- Setters/Getters
- Mutability
More info
This article is part of the CodeSmell Series.