oliverdavies.uk/content/node.5017b3a0-ce88-4884-bace-c0550e729ab5.yml

108 lines
4.8 KiB
YAML

uuid:
- value: 5017b3a0-ce88-4884-bace-c0550e729ab5
langcode:
- value: en
type:
- target_id: daily_email
target_type: node_type
target_uuid: 8bde1f2f-eef9-4f2d-ae9c-96921f8193d7
revision_timestamp:
- value: '2025-05-11T09:00:20+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: 'Coding defensively and considering the unhappy path'
created:
- value: '2024-02-21T00:00:00+00:00'
changed:
- value: '2025-05-11T09:00:20+00:00'
promote:
- value: false
sticky:
- value: false
default_langcode:
- value: true
revision_translation_affected:
- value: true
path:
- alias: /daily/2024/02/21/coding-defensively-and-considering-the-unhappy-path
langcode: en
body:
- value: |
<p>Last week, whilst speaking about Sculpin at PHP South West, I showed an example from a client project I'd built with Sculpin.</p>
<p>The project was for a local gym and fitness centre, and I was demonstrating how I'd created the timetable page and kept the data separate from the HTML and Twig markup so the client could easily administer it (they edit the files on GitHub, which triggers a rebuild of the website).</p>
<p>Each class should have a name and start and end times and be on a specified day.</p>
<p>But what if the client didn't include a name or start or end time?</p>
<p>What if they put a number or boolean instead of a string?</p>
<p>What if there are no classes on a particular day?</p>
<p>You don't know how people will use your software, so it's best to be defensive - validate and verify things before rendering them and prevent the page or whole application from breaking if something isn't as you expected or assumed.</p>
<p>We also can't assume the "happy path" will always be correct.</p>
<p>What if there aren't any classes? Do we put an empty message on the timetable or not show that day?</p>
<h2 id="another-scenario">Another scenario</h2>
<p>If you were retrieving or posting data to an API endpoint, would you assume it was successful or verify the response code is what you expected?</p>
<p>If you get the data, do you check if it's in the correct format and something you can use?</p>
<p>If not, do you verify other actions in your application haven't run and it's not in an invalid state?</p>
<h2 id="here%27s-the-thing">Here's the thing</h2>
<p>In PHP, a common approach is to use the <code>assert()</code> function, as we saw in <a href="/daily/2024/02/20/which-level-is-right-for-you">yesterday's email</a>.</p>
<p>Then, write automated tests that don't test just the happy path but also the unhappy paths.</p>
format: full_html
processed: |
<p>Last week, whilst speaking about Sculpin at PHP South West, I showed an example from a client project I'd built with Sculpin.</p>
<p>The project was for a local gym and fitness centre, and I was demonstrating how I'd created the timetable page and kept the data separate from the HTML and Twig markup so the client could easily administer it (they edit the files on GitHub, which triggers a rebuild of the website).</p>
<p>Each class should have a name and start and end times and be on a specified day.</p>
<p>But what if the client didn't include a name or start or end time?</p>
<p>What if they put a number or boolean instead of a string?</p>
<p>What if there are no classes on a particular day?</p>
<p>You don't know how people will use your software, so it's best to be defensive - validate and verify things before rendering them and prevent the page or whole application from breaking if something isn't as you expected or assumed.</p>
<p>We also can't assume the "happy path" will always be correct.</p>
<p>What if there aren't any classes? Do we put an empty message on the timetable or not show that day?</p>
<h2 id="another-scenario">Another scenario</h2>
<p>If you were retrieving or posting data to an API endpoint, would you assume it was successful or verify the response code is what you expected?</p>
<p>If you get the data, do you check if it's in the correct format and something you can use?</p>
<p>If not, do you verify other actions in your application haven't run and it's not in an invalid state?</p>
<h2 id="here%27s-the-thing">Here's the thing</h2>
<p>In PHP, a common approach is to use the <code>assert()</code> function, as we saw in <a href="http://default/daily/2024/02/20/which-level-is-right-for-you">yesterday's email</a>.</p>
<p>Then, write automated tests that don't test just the happy path but also the unhappy paths.</p>
summary: null
field_daily_email_cta: { }