128 lines
5.3 KiB
YAML
128 lines
5.3 KiB
YAML
uuid:
|
|
- value: 3c851569-9655-4936-aed7-6f965aa6b4d1
|
|
langcode:
|
|
- value: en
|
|
type:
|
|
- target_id: daily_email
|
|
target_type: node_type
|
|
target_uuid: 8bde1f2f-eef9-4f2d-ae9c-96921f8193d7
|
|
revision_timestamp:
|
|
- value: '2025-06-26T11:59:55+00:00'
|
|
revision_uid:
|
|
- target_type: user
|
|
target_uuid: b8966985-d4b2-42a7-a319-2e94ccfbb849
|
|
revision_log: { }
|
|
status:
|
|
- value: true
|
|
uid:
|
|
- target_type: user
|
|
target_uuid: b8966985-d4b2-42a7-a319-2e94ccfbb849
|
|
title:
|
|
- value: 'Giving things descriptive names'
|
|
created:
|
|
- value: '2025-06-23T11:30:14+00:00'
|
|
changed:
|
|
- value: '2025-06-26T11:59:55+00:00'
|
|
promote:
|
|
- value: false
|
|
sticky:
|
|
- value: false
|
|
default_langcode:
|
|
- value: true
|
|
revision_translation_affected:
|
|
- value: true
|
|
path:
|
|
- alias: /daily/2025/06/23/giving-things-descriptive-names
|
|
langcode: en
|
|
body:
|
|
- value: |-
|
|
An approach I like to use when writing code is value objects, and it was great to see a recent talk by Dan Leech (past guest on the [Beyond Blocks podcast][podcast]) about them at a recent PHP meetup.
|
|
|
|
To quote from Dan's talk - "value objects are objects that represent a value".
|
|
|
|
They are simple classes that give a meaningful name to a value.
|
|
|
|
For example, I could write this value object to represent a railway station code:
|
|
|
|
```php
|
|
readonly final class StationCode {
|
|
|
|
public function __construct(public string $value) {
|
|
assert(strlen($value) === 3);
|
|
}
|
|
|
|
}
|
|
```
|
|
|
|
Now, instead of referencing a generic `string` type, I can reference a `StationCode` object.
|
|
|
|
This makes the code more readable and easier to understand.
|
|
|
|
In this case, I can also validate the string is in the correct format so I know that anywhere a `StationCode` object is used, its values are the correct format and this is done in a single place.
|
|
|
|
I can also take this a step further and introduce a `Journey` value object that represents a journey between two stations:
|
|
|
|
```
|
|
readonly final class Journey {
|
|
|
|
public function __construct(
|
|
public StationCode $origin,
|
|
public StationCode $destination,
|
|
) {
|
|
}
|
|
|
|
}
|
|
```
|
|
|
|
In this case, a journey always has two stations - an origin and a destination.
|
|
|
|
Creating an object to represent this gives it a name, but also prevents data clumping - where groups of variables are passed around together.
|
|
|
|
These are two examples from my recent code, but I could find many others.
|
|
|
|
And that's one reason why I like value objects - they are so easy and quick to use.
|
|
|
|
If you haven't before, try introducing value objects into your code.
|
|
|
|
If you already do, reply and tell me about some of the use cases you've found for them.
|
|
|
|
You can see the slides from Dan's presentation at <https://www.dantleech.com/slides/2025/dpc-php-value-objects-and-you/presentation.html>.
|
|
|
|
[podcast]: /podcast/6-dan-leech-php-tui
|
|
format: markdown
|
|
processed: |
|
|
<p>An approach I like to use when writing code is value objects, and it was great to see a recent talk by Dan Leech (past guest on the <a href="http://default/podcast/6-dan-leech-php-tui">Beyond Blocks podcast</a>) about them at a recent PHP meetup.</p>
|
|
<p>To quote from Dan's talk - "value objects are objects that represent a value".</p>
|
|
<p>They are simple classes that give a meaningful name to a value.</p>
|
|
<p>For example, I could write this value object to represent a railway station code:</p>
|
|
<pre><code>readonly final class StationCode {
|
|
|
|
public function __construct(public string $value) {
|
|
assert(strlen($value) === 3);
|
|
}
|
|
|
|
}
|
|
</code></pre><p>Now, instead of referencing a generic <code>string</code> type, I can reference a <code>StationCode</code> object.</p>
|
|
<p>This makes the code more readable and easier to understand.</p>
|
|
<p>In this case, I can also validate the string is in the correct format so I know that anywhere a <code>StationCode</code> object is used, its values are the correct format and this is done in a single place.</p>
|
|
<p>I can also take this a step further and introduce a <code>Journey</code> value object that represents a journey between two stations:</p>
|
|
<pre><code>readonly final class Journey {
|
|
|
|
public function __construct(
|
|
public StationCode $origin,
|
|
public StationCode $destination,
|
|
) {
|
|
}
|
|
|
|
}
|
|
</code></pre><p>In this case, a journey always has two stations - an origin and a destination.</p>
|
|
<p>Creating an object to represent this gives it a name, but also prevents data clumping - where groups of variables are passed around together.</p>
|
|
<p>These are two examples from my recent code, but I could find many others.</p>
|
|
<p>And that's one reason why I like value objects - they are so easy and quick to use.</p>
|
|
<p>If you haven't before, try introducing value objects into your code.</p>
|
|
<p>If you already do, reply and tell me about some of the use cases you've found for them.</p>
|
|
<p>You can see the slides from Dan's presentation at <a href="https://www.dantleech.com/slides/2025/dpc-php-value-objects-and-you/presentation.html">https://www.dantleech.com/slides/2025/dpc-php-value-objects-and-you/presentation.html</a>.</p>
|
|
summary: ''
|
|
field_daily_email_cta:
|
|
- target_type: node
|
|
target_uuid: 9b4c39a3-702f-486c-a79b-4d7b96a4f3f6
|