---
title: Avoiding primitive obsession
date: 2024-09-09
permalink: daily/2024/09/09/avoiding-primitive-obsession
tags:
  - software-development
  - php
  - clean-code
  - drupal
cta: ~
snippet: |
  Avoiding primitive obsession with value objects.
drupal_planet: true
---

Something interesting that [Dave Liddament][0] and I discussed was the use of value objects in application code.

Instead of using a primitive type, such as `string`, you can create a new value object for a specific type of string, such as an `EmailAddress` or, in my side project, a `LicenceKey`.

Both are strings, but using value objects of specific types can make the code more readable and its intent clearer.

A value object can contain additional logic, such as validation to execute an ensure the value object is valid, such as making sure a string is not empty, is a specific length or only contains valid characters.

This an approach that I'm going to use more going forward.

I also found [a lighting talk by Dave][1] at a PHPSW meetup where he explains this further and, of course, you can listen to the podcast episode after it's been released.

[0]: https://www.daveliddament.co.uk
[1]: https://www.youtube.com/watch?v=yJpvObzKewY