paint-brush
Code Refactoring Tips: No. 015 - Remove NULLby@mcsee

Code Refactoring Tips: No. 015 - Remove NULL

by Maximiliano ContieriJuly 29th, 2024
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

The Null Object Pattern simplifies your code by replacing null checks with a polymorphic Null Object, reducing complexity and avoiding null pointer exceptions. Implementing this pattern involves creating a Null Object class that implements the same interface as real objects, thus eliminating unnecessary null checks and making your code cleaner and more maintainable.
featured image - Code Refactoring Tips: No. 015 - Remove NULL
Maximiliano Contieri HackerNoon profile picture

Eliminating The Billion-Dollar Mistake forever

TL;DR: Use the Null Object Pattern to eliminate null checks and simplify your code.

Problems Addressed

Code Smell 12 - Null

Code Smell 260 - Crowdstrike NULL

Code Smell 149 - Optional Chaining

Code Smell 212 - Elvis Operator

Code Smell 192 - Optional Attributes

Code Smell 126 - Fake Null Object

Code Smell 208 - Null Island

Steps

These steps are a special case of Remove IF Refactoring

  1. Create a Null Object class that implements the same interface
  2. Replace null checks with the polymorphic Null Object

Sample Code

Before

public class SoccerMatch {
    private Team homeTeam;
    private Team awayTeam;
    private TimeRecord regularTime;
    private TimeRecord extraTime;

    public SoccerMatch(Team homeTeam, 
                       Team awayTeam,
                       TimeRecord regularTime, 
                       TimeRecord extraTime) {
        this.homeTeam = homeTeam;
        this.awayTeam = awayTeam;
        this.regularTime = regularTime;
        this.extraTime = extraTime;
    }

    public int totalGoals() {
        int goals = regularTime.goals();
        // You might forget this if check 
        // resulting in a null error
        if (extraTime != null) {
            goals += extraTime.goals();
        }
        return goals;
    }
}

class TimeRecord {
    private int goals;

    public TimeRecord(int goals) {
        this.goals = goals;
    }

    public int goals() {
        return goals;
    }
}

After

// 1. Create a Null Object class that implements the same interface

interface TimeRecord {
    // The common protocol between the real object 
    // and the Null Object
    int goals();
}

class PlayedTimeRecord implements TimeRecord {
    // This class is suitable both to be
    // a Regular Time or an Extra Time
    private int goals;

    public PlayedTimeRecord (int goals) {
        this.goals = goals;
    }

    public int goals() {
        return goals;
    }
}

class NoExtraTime implements TimeRecord {
    public int goals() {
        // They are polymorphic
        // They don't need IF checks
        return 0;
    }
}

public class SoccerMatch {
    private Team homeTeam;
    private Team awayTeam;
    private PlayedTimeRecord regularTime;
    private TimeRecord extraTime;

    public SoccerMatch(Team homeTeam, 
                       Team awayTeam,
                       PlayedTimeRecord regularTime,
                       TimeRecord extraTime) {
        this.homeTeam = homeTeam;
        this.awayTeam = awayTeam;        
        // There's a business rule telling
        // regular time is not optional
        // Therefore is an instance of PlayedTimeRecord
        this.regularTime = regularTime;
        this.extraTime = extraTime;
    }

    public int totalGoals() {
        // 2. Replace null checks with the polymorphic Null Object
        // No Ifs 
        // No null checks
        return regularTime.goals() + extraTime.goals();
    }
}


As a side note when you prompt Gemini-AI with the After version it says:

While the code is well-structured, there's a minor optimization opportunity. The NoExtraTime class can be eliminated without compromising the code's functionality.


Type

  • [x]Semi-Automatic

The refactoring has a semantic part that needs to find an existing NullObejct in the domain.

Safety

This refactoring is generally safe.

You must ensure that the Null Object's behavior matches the expected behavior for real cases in your code.

The null object needs to be partially polymorphic with real ones.

In strongly typed languages, they must adhere to the same interface or belong to the same hierarchy.

Why is the code better?

You eliminate null checks with accidental IF conditions, making the code cleaner and easier to understand.

The Null Object Pattern ensures you always work with objects, avoiding null pointer exceptions - The Billion Dollar Mistake.


Tags

  • Null

See also


This article is part of the Refactoring Series on HackerNoon