40

Replace Primitive with Object


Goal

Each domain concept has a small typed home — Money, PhoneNumber, OrderId — that knows its rules.

Before the refactoring
function priceFor(cents, currency) {
  // ...
}
After the refactoring
class Money { /* amount + currency, with arithmetic */ }
function priceFor(money) {
  // ...
}
Savings

Misuse becomes a type error; behavior accretes around the concept; refactoring is local to the wrapper.

Note

Wrapping every primitive is overkill — wrap when the concept needs validation, formatting, or domain-specific behavior beyond what the primitive offers.