Replace Primitive with Value Object

A special case of Replace Primitive with Object

Motivation

Many systems excessively use primitive types to express value-like domain concepts. All over the place you will find naked ints and Strings. (Martin Fowler calls this bad smell Primitive Obsession.) A better way is to define types in their own right for these domain concepts and thus both make the code more expressive and less error-prone.

Mechanics

  • Create a simple value object class.
  • Make sure the class is both identity-less and immutable.
  • Stepwise use the new value class where sofar a primitive type was used.
  • Where applicable, move domain logic (typical cases are validation and arithmetic) into the value class with Extract Method, Move Method, and the like.

Example(s)