Code Smell 126 - Fake Null Object
Published: 2022-04-02 13:29:06

*Null Objects are great alternatives to The Billion Dollar Mistake. Sometimes we don't need them*

> TL;DR: Don't abuse patterns. Even NullObject.

# Problems

- Empty Classes

- Namespace Polluting

- Duplicated Behavior

# Solutions

1. Create [Null Objects](https://dev.to/mcsee/null-the-billion-dollar-mistake-1h1c) instantiating real-object classes.

# Context

Null Object pattern is a great alternative to [Nulls](https://dev.to/mcsee/code-smell-12-null-la4) and [IFs](https://dev.to/mcsee/code-smell-36-switch-case-elseif-else-if-statements-h6c) (Both are code smells).

The structure of the pattern tells us to create a hierarchy.

This is not really necessary, we need real objects to be polymorphic to null objects.

Inheritance is not a proper way to achieve polymorphism.

A simple solution is to create a real object behaving like a null one.

For example: '0' is numbers' null object.

'' (or "") is String's null object

An empty collection is collection's null object.

# Sample Code

## Wrong

[Gist Url]: # (https://gist.github.com/mcsee/36ff0b92d6365291ba000de230e3924e)
```java
abstract class Address {
	public abstract String getCity();
	public abstract String getState();
	public abstract String getZipCode();
}

//Using inheritance for null objects is a mistake
//We should use interfaces (when available)
public class NullAddress extends Address {
	
	public NullAddress() { }
	
	public String getCity() {
		return Constants.EMPTY_STRING;
	}

	public String getState() {
		return Constants.EMPTY_STRING;
	}

	public String getZipCode() {
		return Constants.EMPTY_STRING;
	}

}

public class RealAddress extends Address {
	
	private String zipCode;
	private String city;
	private String state;

	public RealAddress(String city, String state, String zipCode) {
		this.city = city;
		this.state = state;
		this.zipCode = zipCode;
	}

	public String getZipCode() {
		return zipCode;
	}
	
	public String getCity() {
		return city;
	}

	public String getState() {
		return state;
	}

}
```

## Right

[Gist Url]: # (https://gist.github.com/mcsee/baa5c877b821b807ef9c691569a4174d)
```java
//There are just "addresses"
public class Address {
	
	private String zipCode;
	private String city;
	private String state;

	public Address(String city, String state, String zipCode) {
    //Looks anemic :(
		this.city = city;
		this.state = state;
		this.zipCode = zipCode;
	}

	public String zipCode() {
		return zipCode;
	}
	
	public String city() {
		return city;
	}

	public String state() {
		return state;
	}

}

Address nullAddress = new Address(Constants.EMPTY_STRING, Constants.EMPTY_STRING, Constants.EMPTY_STRING);
//we have our null object
//we should NOT assign it to a singleton, static or global
//It behaves like a null object. That's enough
//No premature optimizations

```

# Detection

[X] Manual

This is a semantic smell.

# Tags

- Null

# Conclusion

Creating Null Object classes is sometimes overdesign.

We need to create a real object.

This real object should never be [global](https://dev.to/mcsee/code-smell-17-global-functions-4b82), [singleton](https://dev.to/mcsee/code-smell-32-singletons-1ka), or [static](https://dev.to/mcsee/code-smell-18-static-functions-fpj).

Too many smells to avoid.

# Relations

{% post https://dev.to/mcsee/code-smell-12-null-la4 %}

{% post https://dev.to/mcsee/code-smell-32-singletons-1ka %}

{% post https://dev.to/mcsee/code-smell-114-empty-class-4khg %}

{% post https://dev.to/mcsee/code-smell-18-static-functions-fpj %} 

{% post https://dev.to/mcsee/code-smell-17-global-functions-4b82 %}

# More Info

- [Null: The Billion Dollar Mistake](https://dev.to/mcsee/null-the-billion-dollar-mistake-1h1c)

- [Null Object Wikipedia](https://en.wikipedia.org/wiki/Null_object_pattern)

- [Refactoring.guru](https://refactoring.guru/es/introduce-null-object)

# Credits

Photo by [Juan Davila](https://unsplash.com/@juanster) on [Unsplash](https://unsplash.com/s/photos/void)
  
Thanks to Hernan Wilkinson for this idea on his course [Diseño a la Gorra](https://academia.10pines.com/disenio_a_la_gorra) (in Spanish)  

* * *

> All models are wrong but some models are useful

_George Box_
 
{% post https://dev.to/mcsee/software-engineering-great-quotes-26ci %}

* * *

This article is part of the CodeSmell Series.

{% post https://dev.to/mcsee/how-to-find-the-stinky-parts-of-your-code-1dbc %}