oliverdavies.uk/content/node.1420a8f0-0482-4fa6-86ec-876d84c27255.yml

111 lines
3.8 KiB
YAML

uuid:
- value: 1420a8f0-0482-4fa6-86ec-876d84c27255
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:32+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: |
Why I prefer types
created:
- value: '2023-09-20T00:00:00+00:00'
changed:
- value: '2025-05-11T09:00:32+00:00'
promote:
- value: false
sticky:
- value: false
default_langcode:
- value: true
revision_translation_affected:
- value: true
path:
- alias: /daily/2023/09/20/why-i-prefer-types
langcode: en
body:
- value: |
<p>Whether it's PHP or JavaScript/TypeScript, I prefer type declarations in my code.</p>
<p>As well as benefits like auto-completion in your IDE or text editor and being able to more effectively statically analyse the code, to me, the code is more readable and easier to understand with the types included.</p>
<p>It's more to read, but I can do so easily and immediately know what a function expects as function arguments and what it will return.</p>
<p>Here's the code from my previous email on types from a few days ago, with and without the types declared:</p>
<pre><code class="js">add(...numbers) {
// ...
}
subtract(...numbers) {
// ...
}
add(...numbers: number[]): number {
// ...
}
subtract(...numbers: number[]): number {
// ...
}
</code></pre>
<p>Without types, I can infer what the function accepts and returns, but that's based on my assumption, which could be incorrect.</p>
<p>What if <code>numbers</code> was an array of strings of numbers - e.g. <code>['one', 'two', 'three']</code> - and what if instead of returning the result, it stored it in state to return from a different method like <code>equals()</code> or <code>calculate()</code>?</p>
<p>With the type declarations included, I don't need to presume, infer or make best guesses.</p>
<p>It's clear from just reading the code.</p>
format: full_html
processed: |
<p>Whether it's PHP or JavaScript/TypeScript, I prefer type declarations in my code.</p>
<p>As well as benefits like auto-completion in your IDE or text editor and being able to more effectively statically analyse the code, to me, the code is more readable and easier to understand with the types included.</p>
<p>It's more to read, but I can do so easily and immediately know what a function expects as function arguments and what it will return.</p>
<p>Here's the code from my previous email on types from a few days ago, with and without the types declared:</p>
<pre><code class="js">add(...numbers) {
// ...
}
subtract(...numbers) {
// ...
}
add(...numbers: number[]): number {
// ...
}
subtract(...numbers: number[]): number {
// ...
}
</code></pre>
<p>Without types, I can infer what the function accepts and returns, but that's based on my assumption, which could be incorrect.</p>
<p>What if <code>numbers</code> was an array of strings of numbers - e.g. <code>['one', 'two', 'three']</code> - and what if instead of returning the result, it stored it in state to return from a different method like <code>equals()</code> or <code>calculate()</code>?</p>
<p>With the type declarations included, I don't need to presume, infer or make best guesses.</p>
<p>It's clear from just reading the code.</p>
summary: null
field_daily_email_cta: { }