intermediate
Value objects
Represent immutable values by their attributes so validation, equality, units, and formatting rules stay close to the concept.
Value objects represent concepts defined entirely by their attributes—money amounts, email addresses, date ranges—with immutable equality and validation colocated. They eliminate primitive obsession and make illegal states harder to represent. Replacing a value means creating a new instance, not mutating fields in place.
On interviews: refactor stringly typed fields into value objects. Discuss validation, rounding rules for money, and serialization at boundaries.
Common pitfalls: mutable value objects; value objects that secretly query databases; over-using VOs for every string field without domain meaning.
The trade-off is flexibility versus complexity—know when the simpler path is enough.
Checklist:
- Immutable after construction.
- Equality uses all significant attributes.
- Validation runs at creation time.
- Map to primitives only at persistence or API edges.