Null object pattern
   HOME

TheInfoList



OR:

In
object-oriented Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects", which can contain data and code. The data is in the form of fields (often known as attributes or ''properties''), and the code is in the form of ...
computer programming Computer programming is the process of performing a particular computation (or more generally, accomplishing a specific computing result), usually by designing and building an executable computer program. Programming involves tasks such as anal ...
, a null object is an
object Object may refer to: General meanings * Object (philosophy), a thing, being, or concept ** Object (abstract), an object which does not exist at any particular time or place ** Physical object, an identifiable collection of matter * Goal, an ...
with no referenced value or with defined neutral (''null'') behavior. The null object
design pattern A design pattern is the re-usable form of a solution to a design problem. The idea was introduced by the architect Christopher Alexander and has been adapted for various other disciplines, particularly software engineering. The "Gang of Four" boo ...
, which describes the uses of such objects and their behavior (or lack thereof), was first published as "Void Value" and later in the ''Pattern Languages of Program Design'' book series as "Null Object" .


Motivation

In most object-oriented languages, such as
Java Java (; id, Jawa, ; jv, ꦗꦮ; su, ) is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea to the north. With a population of 151.6 million people, Java is the world's mo ...
or C#,
references Reference is a relationship between objects in which one object designates, or acts as a means by which to connect to or link to, another object. The first object in this relation is said to ''refer to'' the second object. It is called a ''name'' ...
may be
null Null may refer to: Science, technology, and mathematics Computing * Null (SQL) (or NULL), a special marker and keyword in SQL indicating that something has no value * Null character, the zero-valued ASCII character, also designated by , often use ...
. These references need to be checked to ensure they are not null before invoking any
methods Method ( grc, μέθοδος, methodos) literally means a pursuit of knowledge, investigation, mode of prosecuting such inquiry, or system. In recent centuries it more often means a prescribed process for completing a task. It may refer to: *Scien ...
, because methods typically cannot be invoked on null references. The Objective-C language takes another approach to this problem and does nothing when sending a message to nil; if a return value is expected, nil (for objects), 0 (for numeric values), NO (for BOOL values), or a struct (for struct types) with all its members initialised to null/0/NO/zero-initialised struct is returned.


Description

Instead of using a null reference to convey absence of an object (for instance, a non-existent customer), one uses an object which implements the expected interface, but whose method body is empty. The advantage of this approach over a working default implementation is that a null object is very predictable and has no side effects: it does ''nothing''. For example, a function may retrieve a list of files in a folder and perform some action on each. In the case of an empty folder, one response may be to throw an exception or return a null reference rather than a list. Thus, the code which expects a list must verify that it in fact has one before continuing, which can complicate the design. By returning a null object (i.e., an empty list) instead, there is no need to verify that the return value is in fact a list. The calling function may simply iterate the list as normal, effectively doing nothing. It is, however, still possible to check whether the return value is a null object (an empty list) and react differently if desired. The null object pattern can also be used to act as a stub for testing, if a certain feature such as a database is not available for testing.


Example

Given a
binary tree In computer science, a binary tree is a k-ary k = 2 tree data structure in which each node has at most two children, which are referred to as the ' and the '. A recursive definition using just set theory notions is that a (non-empty) binary t ...
, with this node structure: class node One may implement a tree size procedure recursively: function tree_size(node) Since the child nodes may not exist, one must modify the procedure by adding non-existence or null checks: function tree_size(node) This, however, makes the procedure more complicated by mixing boundary checks with normal logic, and it becomes harder to read. Using the null object pattern, one can create a special version of the procedure but only for null nodes: function tree_size(node) function tree_size(null_node) This separates normal logic from special case handling, and makes the code easier to understand.


Relation to other patterns

It can be regarded as a special case of the State pattern and the
Strategy pattern In computer programming, the strategy pattern (also known as the policy pattern) is a behavioral software design pattern that enables selecting an algorithm at runtime. Instead of implementing a single algorithm directly, code receives run-time in ...
. It is not a pattern from ''
Design Patterns ''Design Patterns: Elements of Reusable Object-Oriented Software'' (1994) is a software engineering book describing software design patterns. The book was written by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides, with a forewo ...
'', but is mentioned in Martin Fowler's ''Refactoring'' and Joshua Kerievsky's Refactoring To Patterns as the ''Insert Null Object''
refactoring In computer programming and software design, code refactoring is the process of restructuring existing computer code—changing the '' factoring''—without changing its external behavior. Refactoring is intended to improve the design, structu ...
. Chapter 17 of Robert Cecil Martin's ''Agile Software Development: Principles, Patterns and Practices'' is dedicated to the pattern.


Alternatives

From C# 6.0 it is possible to use the "?." operator (aka null-conditional operator), which will simply evaluate to null if its left operand is null. // compile as Console Application, requires C# 6.0 or higher using System; namespace ConsoleApplication2 // The output will be: // 4


Extension methods and Null coalescing

In some
Microsoft .NET The Microsoft .NET strategy is a marketing plan that Microsoft followed in the early 2000s. Steve Ballmer described it as the company's "most ambitious undertaking since Internet Strategy Day in 1995". In support of this strategy, between 2000 and ...
languages, Extension methods can be used to perform what is called 'null coalescing'. This is because extension methods can be called on null values as if it concerns an 'instance method invocation' while in fact extension methods are static. Extension methods can be made to check for null values, thereby freeing code that uses them from ever having to do so. Note that the example below uses the C#
Null coalescing operator The null coalescing operator (called the Logical Defined-Or operator in Perl) is a binary operator that is part of the syntax for a basic conditional expression in several programming languages, including C#, PowerShell as of version 7.0.0, Perl ...
to guarantee error free invocation, where it could also have used a more mundane if...then...else. The following example only works when you do not care the existence of null, or you treat null and empty string the same. The assumption may not hold in other applications. // compile as Console Application, requires C# 3.0 or higher using System; using System.Linq; namespace MyExtensionWithExample // The output will be: // 18


In various languages


C++

A language with statically typed references to objects illustrates how the null object becomes a more complicated pattern: #include class Animal ; class Dog : public Animal ; class NullAnimal : public Animal ; Here, the idea is that there are situations where a pointer or reference to an Animal object is required, but there is no appropriate object available. A null reference is impossible in standard-conforming C++. A null Animal* pointer is possible, and could be useful as a place-holder, but may not be used for direct dispatch: a->MakeSound() is undefined behavior if a is a null pointer. The null object pattern solves this problem by providing a special NullAnimal class which can be instantiated bound to an Animal pointer or reference. The special null class must be created for each class hierarchy that is to have a null object, since a NullAnimal is of no use when what is needed is a null object with regard to some Widget base class that is not related to the Animal hierarchy. Note that NOT having a null class at all is an important feature, in contrast to languages where "anything is a reference" (e.g., Java and C#). In C++, the design of a function or method may explicitly state whether null is allowed or not. // Function which requires an , Animal, instance, and will not accept null. void DoSomething(const Animal& animal) // Function which may accept an , Animal, instance or null. void DoSomething(const Animal* animal)


C#

C# is a language in which the null object pattern can be properly implemented. This example shows animal objects that display sounds and a NullAnimal instance used in place of the C# null keyword. The null object provides consistent behaviour and prevents a runtime null reference exception that would occur if the C# null keyword were used instead. /* Null object pattern implementation: */ using System; // Animal interface is the key to compatibility for Animal implementations below. interface IAnimal // Animal is the base case. abstract class Animal : IAnimal // Dog is a real animal. class Dog : Animal /*

= * Simplistic usage example in a Main entry point. */ static class Program


Smalltalk

Following the Smalltalk principle, ''everything is an object'', the absence of an object is itself modeled by an object, called nil. In the GNU Smalltalk for example, the class of nil is UndefinedObject, a direct descendant of Object. Any operation that fails to return a sensible object for its purpose may return nil instead, thus avoiding the special case of returning "no object" unsupported by Smalltalk designers. This method has the advantage of simplicity (no need for a special case) over the classical "null" or "no object" or "null reference" approach. Especially useful messages to be used with nil are isNil, ifNil: or ifNotNil:,, which make it practical and safe to deal with possible references to nil in Smalltalk programs.


Common Lisp

In Lisp, functions can gracefully accept the special object nil, which reduces the amount of special case testing in application code. For instance, although nil is an atom and does not have any fields, the functions car and cdr accept nil and just return it, which is very useful and results in shorter code. Since nil is the empty list in Lisp, the situation described in the introduction above doesn't exist. Code which returns nil is returning what is in fact the empty list (and not anything resembling a null reference to a list type), so the caller does not need to test the value to see whether or not it has a list. The null object pattern is also supported in multiple value processing. If the program attempts to extract a value from an expression which returns no values, the behavior is that the null object nil is substituted. Thus (list (values)) returns (nil) (a one-element list containing nil). The (values) expression returns no values at all, but since the function call to list needs to reduce its argument expression to a value, the null object is automatically substituted.


CLOS

In Common Lisp, the object nil is the one and only instance of the special class null. What this means is that a method can be specialized to the null class, thereby implementing the null design pattern. Which is to say, it is essentially built into the object system: ;; empty dog class (defclass dog () ()) ;; a dog object makes a sound by barking: woof! is printed on standard output ;; when (make-sound x) is called, if x is an instance of the dog class. (defmethod make-sound ((obj dog)) (format t "woof!~%")) ;; allow (make-sound nil) to work via specialization to null class. ;; innocuous empty body: nil makes no sound. (defmethod make-sound ((obj null))) The class null is a subclass of the symbol class, because nil is a symbol. Since nil also represents the empty list, null is a subclass of the list class, too. Methods parameters specialized to symbol or list will thus take a nil argument. Of course, a null specialization can still be defined which is a more specific match for nil.


Scheme

Unlike Common Lisp, and many dialects of Lisp, the Scheme dialect does not have a nil value which works this way; the functions car and cdr may not be applied to an empty list; Scheme application code therefore has to use the empty? or pair? predicate functions to sidestep this situation, even in situations where very similar Lisp would not need to distinguish the empty and non-empty cases thanks to the behavior of nil.


Ruby

In duck-typed languages like
Ruby A ruby is a pinkish red to blood-red colored gemstone, a variety of the mineral corundum ( aluminium oxide). Ruby is one of the most popular traditional jewelry gems and is very durable. Other varieties of gem-quality corundum are called ...
, language inheritance is not necessary to provide expected behavior. class Dog def sound "bark" end end class NilAnimal def sound(*); end end def get_animal(animal=NilAnimal.new) animal end get_animal(Dog.new).sound => "bark" get_animal.sound => nil Attempts to directly monkey-patch NilClass instead of providing explicit implementations give more unexpected side effects than benefits.


JavaScript

In duck-typed languages like
JavaScript JavaScript (), often abbreviated as JS, is a programming language that is one of the core technologies of the World Wide Web, alongside HTML and CSS. As of 2022, 98% of websites use JavaScript on the client side for webpage behavior, of ...
, language inheritance is not necessary to provide expected behavior. class Dog class NullAnimal function getAnimal(type) dog', nullmap((animal) => getAnimal(animal).sound()); // Returns bark", null


Java

public interface Animal public class Dog implements Animal public class NullAnimal implements Animal This code illustrates a variation of the C++ example, above, using the Java language. As with C++, a null class can be instantiated in situations where a reference to an Animal object is required, but there is no appropriate object available. A null Animal object is possible (Animal myAnimal = null;) and could be useful as a place-holder, but may not be used for calling a method. In this example, myAnimal.makeSound(); will throw a NullPointerException. Therefore, additional code may be necessary to test for null objects. The null object pattern solves this problem by providing a special NullAnimal class which can be instantiated as an object of type Animal. As with C++ and related languages, that special null class must be created for each class hierarchy that needs a null object, since a NullAnimal is of no use when what is needed is a null object that does not implement the Animal interface.


PHP

interface Animal class Dog implements Animal class Cat implements Animal class NullAnimal implements Animal $animalType = 'elephant'; function makeAnimalFromAnimalType(string $animalType): Animal makeAnimalFromAnimalType($animalType)->makeSound(); // ..the null animal makes no sound function animalMakeSound(Animal $animal): void foreach ( makeAnimalFromAnimalType('dog'), makeAnimalFromAnimalType('NullAnimal'), makeAnimalFromAnimalType('cat'), as $animal)


Visual Basic .NET

The following null object pattern implementation demonstrates the concrete class providing its corresponding null object in a static field Empty. This approach is frequently used in the .NET Framework (String.Empty, EventArgs.Empty, Guid.Empty, etc.). Public Class Animal Public Shared ReadOnly Empty As Animal = New AnimalEmpty() Public Overridable Sub MakeSound() Console.WriteLine("Woof!") End Sub End Class Friend NotInheritable Class AnimalEmpty Inherits Animal Public Overrides Sub MakeSound() ' End Sub End Class


Criticism

This pattern should be used carefully as it can make errors/bugs appear as normal program execution.Fowler, Martin (1999). Refactoring pp. 216 Care should be taken not to implement this pattern just to avoid null checks and make code more readable, since the harder-to-read code may just move to another place and be less standard—such as when different logic must execute in case the object provided is indeed the null object. The common pattern in most languages with
reference type In computer programming, data types can be divided into two categories: value types (or by-value types) and reference types (or by-reference types). Value types are completely represented by their meaning, while reference types are references to ano ...
s is to compare a reference to a single value referred to as null or nil. Also, there is additional need for testing that no code anywhere ever assigns null instead of the null object, because in most cases and languages with static typing, this is not a compiler error if the null object is of a reference type, although it would certainly lead to errors at run time in parts of the code where the pattern was used to avoid null checks. On top of that, in most languages and assuming there can be many null objects (i.e., the null object is a reference type but doesn't implement the
singleton pattern In software engineering, the singleton pattern is a software design pattern that restricts the instantiation of a class to a singular instance. One of the well-known "Gang of Four" design patterns, which describe how to solve recurring problems ...
in one or another way), checking for the null object instead of for the null or nil value introduces overhead, as does the singleton pattern likely itself upon obtaining the singleton reference.


See also

*
Nullable type Nullable types are a feature of some programming languages which allow a value to be set to the special value NULL instead of the usual possible values of the data type. In statically typed languages, a nullable type is an option type, while in ...
* Option type


References


External links


Jeffery Walker's account of the Null Object Pattern
* ttp://www.owlnet.rice.edu/~comp212/00-spring/handouts/week06/null_object_revisited.htm Null Object Pattern Revisitedbr>Introduce Null Object refactoringSourceMaking TutorialNull Object Pattern in Swift
{{Design Patterns patterns Software design patterns Articles with example Java code