Meaning
Primitive Obsession is a code smell where primitive data types (such as strings, integers, or floats) are used excessively to represent domain concepts that would be better modeled as small objects, causing scattered validation and behavior logic.
Primary Function
Signals a design smell indicating overuse of primitive types instead of domain‑specific objects, prompting refactoring toward richer abstractions.
Communicative Purpose
Alerts developers to a modeling deficiency that encourages encapsulation of data and behavior, improving maintainability and reducing duplication.
Pattern
Detect overuse of primitive types for domain concepts → extract dedicated value objects → replace primitive usages
Core Structure
Use of a primitive type (string, number, boolean) to represent a domain concept that lacks associated behavior.
Função primária
Signals a design smell indicating overuse of primitive types instead of domain‑specific objects, prompting refactoring toward richer abstractions.
Propósito comunicativo
Alerts developers to a modeling deficiency that encourages encapsulation of data and behavior, improving maintainability and reducing duplication.
Situações de gatilho
When developers represent concepts like phone numbers, ZIP codes, monetary amounts, or dates as raw strings or numbers instead of creating dedicated value objects.
Contextos
Appears in codebases where domain concepts are scattered across primitives, often seen in utility classes, DTOs, or procedural code lacking object‑oriented encapsulation.
Padrão
Detect overuse of primitive types for domain concepts → extract dedicated value objects → replace primitive usages
Estrutura central
Use of a primitive type (string, number, boolean) to represent a domain concept that lacks associated behavior.
Slots de substituição
[primitive type]
Colocados típicos
- string
- integer
- float
- phone number
- zip code
- amount
- date
- code
Substituições comuns
- Introduce Parameter Object
- Replace Data Value with Object
- Value Object
Erros comuns
Using strings for phone numbers leading to formatting and validation bugs; using floats for monetary values causing rounding errors; scattering validation logic across the codebase.
Similar / contraste
Data Clumps (groups of primitives that always appear together), Primitive Envy (a method more interested in another object's data), Feature Envy
Interferências
Do not confuse with Primitive Envy, which is a feature envy smell where a method is overly interested in another object's data.
Família do chunk
- Data Clumps
- Primitive Obsession
- Introduce Parameter Object
Nuance
Primitive Obsession is not merely about using primitives; it's about missing behavior and validation that should belong to the domain concept.
Efeito pragmático
Highlights a need to refactor toward richer domain models, reducing duplication and improving type safety.
Dica de memória
Think of a phone number stored as a plain string—should be a PhoneNumber class with formatting and validation.
Nota
Primitive Obsession often appears alongside Data Clumps; refactoring to value objects improves type safety and encapsulates validation.
Upgrade path
Introduce Parameter Object or Replace Data Value with Object
Log in to save chunks.