Ga naar de hoofdinhoud

Replace primitive piles with value objects

Identify when a primitive should become an immutable value object.

object-oriented-programming5 min read

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…

Read the full lesson

Sign up free: one personalized lesson every day, matched to your role and goals.

← Back to library

Neem contact op