Identify when a primitive should become an immutable value object.
The move: give small concepts a type. A value object is an object defined by its attributes rather than identity. Good candidates are money, date ranges, coordinates, email addresses, percentages, quantities, and IDs with format rules. It carries invariants DateRange can enforce start before end. EmailAddress can normalize and validate. Money can stop accidental arithmetic across currencies. It carries behavior The behavior does not need to be large. Money.add, DateRange.overlaps, and EmailAddress.domain are enough to remove repeated low-level code. It compares by value If two objects represent the same value, they should be treated as equal. That makes tests clearer…
Sign up free — one personalized lesson every day, matched to your role and goals.
Already have an account? Sign in