Skip to main content

Sign in to CleanKata

Track your progress, earn XP, and unlock every lesson.

By signing in you agree to our Terms of Use and Privacy Policy.

Object Calisthenics70 XP7 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.

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.

πŸ”§ Some exercises may still have errors. If something seems wrong, use the Feedback button (bottom-right of the page) to report it β€” it helps us fix it fast.

Hint: If you can pass 'banana' as a currency without compile error, you have a primitive obsession problem.

βœ— Your version

Wrap All Primitives and Strings β€” CleanKata β€” CleanKata