{ "uuid": [ { "value": "ebad7a57-881a-470e-9ec0-84ec23ac0802" } ], "langcode": [ { "value": "en" } ], "type": [ { "target_id": "daily_email", "target_type": "node_type", "target_uuid": "8bde1f2f-eef9-4f2d-ae9c-96921f8193d7" } ], "revision_timestamp": [ { "value": "2025-07-06T22:48:02+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": "Avoiding indentation" } ], "created": [ { "value": "2025-07-04T22:46:00+00:00" } ], "changed": [ { "value": "2025-07-06T22:48:02+00:00" } ], "promote": [ { "value": false } ], "sticky": [ { "value": false } ], "default_langcode": [ { "value": true } ], "revision_translation_affected": [ { "value": true } ], "path": [ { "alias": "\/daily\/2025\/07\/04\/avoiding-indentation", "langcode": "en" } ], "body": [ { "value": "A guide that I use when writing or reviewing code is to avoid or minimise indentation.\r\n\r\nUsually, the nearer to the left of the file the text is, the better.\r\n\r\nIf you had this code:\r\n\r\n```php\r\n 3) {\r\n \/\/ Do something...\r\n }\r\n }\r\n }\r\n}\r\n```\r\n\r\nWhat if you refactored it to use early returns and remove some indentation?\r\n\r\n```php\r\nA guide that I use when writing or reviewing code is to avoid or minimise indentation.<\/p>\n

Usually, the nearer to the left of the file the text is, the better.<\/p>\n

If you had this code:<\/p>\n

<?php\n\nfunction doSomething() {\n  if ($a === TRUE) {\n    if ($b === 'banana') {\n      if ($c > 3) {\n        \/\/ Do something...\n     }\n    }\n  }\n}\n<\/code><\/pre>

What if you refactored it to use early returns and remove some indentation?<\/p>\n

<?php\n\nfunction doSomething() {\n  if ($a !== TRUE) {\n    return;\n  }\n\n  if ($b !== 'banana') {\n    return;\n  }\n\n  if ($c <= 3) {\n    return;\n  }\n\n  \/\/ Do something...\n}\n<\/code><\/pre>

This is easier for me to read and review.<\/p>\n

I can keep track of there I am in the function as it is more linear. Especially if the code within each condition block is more complex.<\/p>\n

Indentation in CSS<\/h2>\n

CSS now supports nesting, similar to Sass and other preprocessors, so you can write CSS like this:<\/p>\n

.menu {\n  ul {\n    li {\n      a {\n        color: red;\n      }\n    }\n  }\n}\n<\/code><\/pre>

I usually find this harder to search, particularly if a pre-processor like Sass is being used, so I avoid it as much as possible.<\/p>\n

I only really use it to style pseudo-selectors like :hover<\/code> and :focus<\/code> to element.<\/p>\n

Indentation in Nix<\/h2>\n

In Nix, these two examples achieve the same thing:<\/p>\n

services.gammastep = {\n  enable = true;\n  provider = \"geoclue2\";\n};\n\nservices.gammastep.enable = true;\nservices.gammastep.provider = \"geoclue2\";\n<\/code><\/pre>

In long files, it can be hard to keep track of what service you're configuring once it has been indented.<\/p>\n

Whilst it adds some duplication, I've recently favoured the second approach in my code.<\/p>\n

Here's the thing<\/h2>\n

There needs to be some indentation in code to allow for functions, conditions, loops, etc.<\/p>\n

But, whichever language I'm writing, I usually aim for no more than two levels of indentation in a file.<\/p>\n

If code is indented further, I'll try or suggest an alternative approach to reduce the indentation - which usually results in cleaner code overall.<\/p>\n", "summary": "" } ], "field_daily_email_cta": [ { "target_type": "node", "target_uuid": "9b4c39a3-702f-486c-a79b-4d7b96a4f3f6" } ] }