Data Clumps
Groups of data that appear together repeatedly.
“Bunches of data that hang around together really ought to be made into their own object.” – Fowler & Beck
(x, y), (street, city, zip), (startDate, endDate).
Signs of Use (Symptoms)
- Same 3-4 variables passed together to multiple functions.
- Same 3-4 fields appearing in multiple classes.
Why it is bad (Consequences)
- Duplication: Same grouping logic repeated.
- Missed Abstraction: The concept (e.g.,
DateRange,Address) is not explicitly named.
Why it happens (Root Cause)
Easier to pass loose variables than to create a class.
When it might be okay (Exceptions)
- Rarely. If it happens once, maybe. If it happens twice, extract it.
Explanation
Problem
You pass startDate and endDate to 10 functions. They always travel together.
The Flaw
You’re not giving a name to the concept. DateRange is a thing. Acknowledge it.
Real world analogy
A key and a keychain. If you always carry them together, attach them!
Refactoring Solution
- Extract Class: Create a Value Object (e.g.,
DateRange,Coordinates,Address). - Introduce Parameter Object.
Pros and Cons (of the Antipattern)
| Pros (Why people do it) | Cons (The price you pay) |
|---|---|
Comparison
- Related Antipatterns: Primitive Obsession, Long Parameter List.
- Related Principles: DRY.
Code example
Typescript
Bad (The Antipattern)
Good (The Fix)
PHP
Bad (The Antipattern)
Good (The Fix)