"value":"\n <p>The Null Object pattern is one of my favourite ways to DRY up code and reduce duplication.<\/p>\n\n<p>I wrote the <a href=\"https:\/\/www.drupal.org\/project\/system_user\">System User module<\/a> for a client project a number of years ago, which also included the <a href=\"https:\/\/www.drupal.org\/project\/null_user\">Null User module<\/a>.<\/p>\n\n<p>If no system user is found, it returns a null (empty) user instead that returns null or empty values by default.<\/p>\n\n<p>Today, <a href=\"https:\/\/x.com\/opdavies\/status\/1829183673725284772\">I posted<\/a> <a href=\"https:\/\/mastodon.social\/@opdavies\/113046773292848198\">and tooted<\/a> a screenshot of some code from a side project I'm working on, which I realised would be another good time to use the Null User module.<\/p>\n\n<p>In the code, I search for a user with a given licence key.<\/p>\n\n<p>If a user is found, it is returned.<\/p>\n\n<p>If not, instead of returning FALSE or NULL, I return a null user object that I can treat the same as a real user.<\/p>\n\n<p>Now, when I create the node and assign an owner, I can just use <code>$account->id()<\/code> and it will always work if a user was found or not.<\/p>\n\n<p>This makes the code cleaner, simpler and easier to test and maintain.<\/p>\n\n ",
"format":"full_html",
"processed":"\n <p>The Null Object pattern is one of my favourite ways to DRY up code and reduce duplication.<\/p>\n\n<p>I wrote the <a href=\"https:\/\/www.drupal.org\/project\/system_user\">System User module<\/a> for a client project a number of years ago, which also included the <a href=\"https:\/\/www.drupal.org\/project\/null_user\">Null User module<\/a>.<\/p>\n\n<p>If no system user is found, it returns a null (empty) user instead that returns null or empty values by default.<\/p>\n\n<p>Today, <a href=\"https:\/\/x.com\/opdavies\/status\/1829183673725284772\">I posted<\/a> <a href=\"https:\/\/mastodon.social\/@opdavies\/113046773292848198\">and tooted<\/a> a screenshot of some code from a side project I'm working on, which I realised would be another good time to use the Null User module.<\/p>\n\n<p>In the code, I search for a user with a given licence key.<\/p>\n\n<p>If a user is found, it is returned.<\/p>\n\n<p>If not, instead of returning FALSE or NULL, I return a null user object that I can treat the same as a real user.<\/p>\n\n<p>Now, when I create the node and assign an owner, I can just use <code>$account->id()<\/code> and it will always work if a user was found or not.<\/p>\n\n<p>This makes the code cleaner, simpler and easier to test and maintain.<\/p>\n\n ",