oliverdavies.uk/content/node.d3fe0eae-7883-4fe6-9a98-747b53d860ca.json
2025-05-14 00:36:23 +01:00

100 lines
No EOL
4.4 KiB
JSON

{
"uuid": [
{
"value": "d3fe0eae-7883-4fe6-9a98-747b53d860ca"
}
],
"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:14+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": "Don't add boolean arguments"
}
],
"created": [
{
"value": "2024-05-03T00:00:00+00:00"
}
],
"changed": [
{
"value": "2025-05-11T09:00:14+00:00"
}
],
"promote": [
{
"value": false
}
],
"sticky": [
{
"value": false
}
],
"default_langcode": [
{
"value": true
}
],
"revision_translation_affected": [
{
"value": true
}
],
"path": [
{
"alias": "\/daily\/2024\/05\/03\/dont-add-boolean-arguments",
"langcode": "en"
}
],
"body": [
{
"value": "\n <p>A convention I like from the Laravel framework is to avoid adding boolean arguments to methods.<\/p>\n\n<p>For example, if I have this function:<\/p>\n\n<pre><code class=\"php\">public function getPosts() { ... }\n<\/code><\/pre>\n\n<p>If I wanted to only get published posts, one way would be to add a boolean argument:<\/p>\n\n<pre><code class=\"php\">public function getPosts(boolean $onlyPublished) { ... }\n<\/code><\/pre>\n\n<p>Then, I'd need to use that within the method body to add another condition (this is referred to as control coupling, where one method affects another).<\/p>\n\n<p>The non-boolean approach would be to create a separate method with its own distinct name.<\/p>\n\n<p>For example, <code>getPosts()<\/code> could be named <code>getAllPosts()<\/code> and there could be a separate <code>getPublishedPosts()<\/code> method for only getting published posts:<\/p>\n\n<pre><code class=\"php\">public function getAllPosts() { ... }\n\npublic function getPublishedPosts() { ... }\n<\/code><\/pre>\n\n<p>Whilst we have two methods now instead of one, it's much clearer what each does and there aren't any random <code>true<\/code> or <code>false<\/code>s wherever the method is used.<\/p>\n\n ",
"format": "full_html",
"processed": "\n <p>A convention I like from the Laravel framework is to avoid adding boolean arguments to methods.<\/p>\n\n<p>For example, if I have this function:<\/p>\n\n<pre><code class=\"php\">public function getPosts() { ... }\n<\/code><\/pre>\n\n<p>If I wanted to only get published posts, one way would be to add a boolean argument:<\/p>\n\n<pre><code class=\"php\">public function getPosts(boolean $onlyPublished) { ... }\n<\/code><\/pre>\n\n<p>Then, I'd need to use that within the method body to add another condition (this is referred to as control coupling, where one method affects another).<\/p>\n\n<p>The non-boolean approach would be to create a separate method with its own distinct name.<\/p>\n\n<p>For example, <code>getPosts()<\/code> could be named <code>getAllPosts()<\/code> and there could be a separate <code>getPublishedPosts()<\/code> method for only getting published posts:<\/p>\n\n<pre><code class=\"php\">public function getAllPosts() { ... }\n\npublic function getPublishedPosts() { ... }\n<\/code><\/pre>\n\n<p>Whilst we have two methods now instead of one, it's much clearer what each does and there aren't any random <code>true<\/code> or <code>false<\/code>s wherever the method is used.<\/p>\n\n ",
"summary": null
}
],
"feeds_item": [
{
"imported": "1970-01-01T00:33:45+00:00",
"guid": null,
"hash": "e20e2bce0a560bd0261032201f0cacd5",
"target_type": "feeds_feed",
"target_uuid": "90c85284-7ca8-4074-9178-97ff8384fe76"
}
]
}