1.2 KiB
title | date | permalink | tags | cta | snippet | drupal_planet | ||||
---|---|---|---|---|---|---|---|---|---|---|
Avoiding primitive obsession | 2024-09-09 | daily/2024/09/09/avoiding-primitive-obsession |
|
~ | Avoiding primitive obsession with value objects. | true |
Something interesting that Dave Liddament 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 at a PHPSW meetup where he explains this further and, of course, you can listen to the podcast episode after it's been released.