oliverdavies.uk/content/node.94d78d4d-fcd1-4d1a-a0fc-e6f8c39d084b.json
2025-05-11 20:02:14 +01:00

100 lines
No EOL
6.8 KiB
JSON

{
"uuid": [
{
"value": "94d78d4d-fcd1-4d1a-a0fc-e6f8c39d084b"
}
],
"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": "Don't use \"else\"\n"
}
],
"created": [
{
"value": "2023-09-11T00: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\/11\/dont-use-else",
"langcode": "en"
}
],
"body": [
{
"value": "\n <p>A popular approach to writing clean code is to avoid the \"else\" keyword and, if possible, avoid nesting <code>if<\/code> statements within other <code>if<\/code> statements.\nIf I look at some code, I want to see as few indentation levels as possible, making the code easier to read and understand.<\/p>\n\n<h2 id=\"what-should-i-do-instead%3F\">What should I do instead?<\/h2>\n\n<p>Instead, you check for a condition; if that isn't met, you return early.\nFor\u00a0example, here is some code I saw recently during a meetup talk:<\/p>\n\n<pre><code class=\"language-php\">$callingClass = $scope-&gt;getClassReflection()-&gt;getName();\n\nif ($callingClass === TextMessageQueueProcessor::class) {\n\u00a0 \u00a0 return [];\n}\n\n$type = $scope-&gt;getType($node-&gt;var);\n\nforeach ($type-&gt;getReferencedClasses() as $targetClass) {\n\u00a0 \u00a0 if ($targetClass === TextMessageSender::class) {\n\u00a0 \u00a0 \u00a0 \u00a0 return [\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 RuleErrorBuilder::message(\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 sprintf(\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \"Can not call %s from %s\",\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 $targetClass,\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 $callingClass\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 )\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 )-&gt;build()\n\u00a0 \u00a0 \u00a0 \u00a0 ];\n\u00a0 \u00a0 }\n}\n\nreturn [];\n<\/code><\/pre>\n\n<p>There are no <code>else<\/code> statements.<\/p>\n\n<p>If the calling class isn't of the required type, it returns immediately with no violations, and we continue, knowing the calling class must be what we need.<\/p>\n\n<p>If the target class is one where the code shouldn't be called from, it returns immediately with the\u00a0violation.<\/p>\n\n<p>Finally, if no violations were found within the referenced classes, it returns an empty array.<\/p>\n\n<h2 id=\"here%27s-the-thing\">Here's the thing<\/h2>\n\n<p>The code always returns an array of rule violations but does so as soon as possible at each point.<\/p>\n\n<p>The code is clean and readable, and I can understand it, knowing once each condition is passed, I don't need to continue thinking about it.<\/p>\n\n<p>Whilst there are some situations to use <code>else<\/code>, most of the time I've found that I can use an early return instead.<\/p>\n\n ",
"format": "full_html",
"processed": "\n <p>A popular approach to writing clean code is to avoid the \"else\" keyword and, if possible, avoid nesting <code>if<\/code> statements within other <code>if<\/code> statements.\nIf I look at some code, I want to see as few indentation levels as possible, making the code easier to read and understand.<\/p>\n\n<h2 id=\"what-should-i-do-instead%3F\">What should I do instead?<\/h2>\n\n<p>Instead, you check for a condition; if that isn't met, you return early.\nFor&nbsp;example, here is some code I saw recently during a meetup talk:<\/p>\n\n<pre><code class=\"language-php\">$callingClass = $scope-&gt;getClassReflection()-&gt;getName();\n\nif ($callingClass === TextMessageQueueProcessor::class) {\n&nbsp; &nbsp; return [];\n}\n\n$type = $scope-&gt;getType($node-&gt;var);\n\nforeach ($type-&gt;getReferencedClasses() as $targetClass) {\n&nbsp; &nbsp; if ($targetClass === TextMessageSender::class) {\n&nbsp; &nbsp; &nbsp; &nbsp; return [\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RuleErrorBuilder::message(\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sprintf(\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \"Can not call %s from %s\",\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $targetClass,\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $callingClass\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )-&gt;build()\n&nbsp; &nbsp; &nbsp; &nbsp; ];\n&nbsp; &nbsp; }\n}\n\nreturn [];\n<\/code><\/pre>\n\n<p>There are no <code>else<\/code> statements.<\/p>\n\n<p>If the calling class isn't of the required type, it returns immediately with no violations, and we continue, knowing the calling class must be what we need.<\/p>\n\n<p>If the target class is one where the code shouldn't be called from, it returns immediately with the&nbsp;violation.<\/p>\n\n<p>Finally, if no violations were found within the referenced classes, it returns an empty array.<\/p>\n\n<h2 id=\"here%27s-the-thing\">Here's the thing<\/h2>\n\n<p>The code always returns an array of rule violations but does so as soon as possible at each point.<\/p>\n\n<p>The code is clean and readable, and I can understand it, knowing once each condition is passed, I don't need to continue thinking about it.<\/p>\n\n<p>Whilst there are some situations to use <code>else<\/code>, most of the time I've found that I can use an early return instead.<\/p>\n\n ",
"summary": null
}
],
"feeds_item": [
{
"imported": "2025-05-11T09:00:32+00:00",
"guid": null,
"hash": "d2e8d39f68f4ae6ba0c9ffab688585b7",
"target_type": "feeds_feed",
"target_uuid": "90c85284-7ca8-4074-9178-97ff8384fe76"
}
]
}