Skip to main content
Object Calisthenics 70 XP · 7 min

Wrap All Primitives and Strings

A raw int or string has no domain meaning — wrapping it in a class like Money or Email lets the compiler enforce intent and gives behavior a natural home.

Showing
Ad (728×90)

Why this matters

A bare int called age has no constraints — nothing prevents you from passing -5 or 999. A tiny Age class validates itself at construction, carries its own rules, and can't be confused with an unrelated integer like a quantity or a price. You get type safety, a single place for validation, and meaningful names everywhere.

Code Challenge

Study the messy code, try to refactor it, then reveal the clean version.

Key takeaway

If a primitive has a business rule attached to it (a range, a format, a unit), wrap it. Invalid values become impossible to construct — you stop defending everywhere and start trusting your types.

Done with this lesson?

Mark it complete to earn XP and track your progress.