Home / News / The Null Awakens: Making Missing Data Harmless

The Null Awakens: Making Missing Data Harmless

$logo = $job->company ? $job->company->profile->logo : asset('images/default-company.png');

Here’s a sarcastic summary with ironic hashtags related to the topic at the start:

Usually in job portals, we get a ton of registration but some users don’t even finish their profile. Some users set their profile, but others don’t even exist. This causes our code to look pretty bad and hard to maintain, where it becomes like this every time we want to use user profile. This is a huge hassle, especially when we have to maintain a lot of checks to keep our code clean and safe.

The Solution : Null Object Pattern

The Null Object Pattern is a simple and elegant design pattern that solves this problem by returning a special “empty” object that behaves like the real one but with safe defaults. Instead of returning null, we create a NullProfile class that implements the `getLogo()` method to return the company profile logo image. This removes the need for null checks and keeps our code clean.Usually in job portal , we got a lot of registration but some of user don’t even finish the profile . Some user set their profile , some user not set at all .

This cause our code look really bad and hard to maintain where it become like this every time we want to use user profile .

$logo = $job->company && $job->company->profile
    ? $job->company->profile->logo
    : asset('images/default-company.png');

there’s a lot if checks . This is really hard to maintain when we want to use this in a lot of place .

The Solution : Null Object Pattern

The Null Object Pattern is a design pattern where instead of returning null, you return a special “empty” object that behaves like the real one but with safe defaults.

This removes the need for null checks and keeps our code clean.

First we create a NullProfile class

<?php

namespace App\Models\NullObjects;

class NullProfile
{
    public function getLogo(): string
    {
        return asset('images/default-company.png');
    }
}

Once we have NullProfile class , we use it in model

class Company extends Model
{
    public function profile()
    {
        return $this->hasOne(Profile::class);
    }

    public function getProfileOrDefaultAttribute()
    {
        return $this->profile ?? new NullProfile();
    }
}

Then we can use it like this , we don’t need anymore complicated logic if in all over place anymore . Just use this and it will be safe enough .

$company->profile_or_default->getLogo();

Why It Helpful

Usually for a job portal there will be multiple relationship that require us to access the company profile or user profile , by using this null object paatern we can avoids null checks across all our code . This can help to keep our views and controller clean and safe .

Why Not Just Handle in Accessors ?

1 . For me small project would be good enough already
2 . For field level default accessor already work great , but in this case the profile is object for the company .

Tagged:

Leave a Reply

Your email address will not be published. Required fields are marked *