Why you should use Value Objects in your Code

Bending the Clean Architecture Principles

Anthony Trad
3 min readSep 15, 2022

Introduction

Every now and then, I try to revisit some of my old projects, or even explore on GitHub what’s trendy. In this journey, I found a common problem that I was addressed a lot:

  • Primitive Type obsession
  • Strongly Typed IDs

What

All of us developers worked with some generic entities like a User, an Order, Money, or anything really… It even goes beyond that, I saw my sister’s introductory programming course at school. One of the questions were:

How do you represent someone’s Age?

Naively, the right answer was an Integer. Which is not wrong in a sense, but it got me thinking, if I would ask this question to the community, what will they answer?

I think most of them will use an Integer without ever looking back. So let’s do that for a second.

Trading Shoes

I hope you got that Eminem reference, our Age is just a number right? So I used an integer for my User class. That’s great but now, how do I initialize it?

I added a default value, because we can’t have an age of 0. Now, our new intern decided to change the age of our users. He wanted to calculate it from the date of birth and the current date. But little did he know that his calculations were wrong due to the…

--

--

Anthony Trad

Senior Software Engineer focused on .NET and the Cloud. Reconsidering major principles and patterns, ideas are my own.