Null object pattern

Last updated

In object-oriented computer programming, a null object is an object with no referenced value or with defined neutral (null) behavior. The null object design pattern, which describes the uses of such objects and their behavior (or lack thereof), was first published as "Void Value" [1] and later in the Pattern Languages of Program Design book series as "Null Object" . [2] [ year needed ]

Contents

Motivation

In most object-oriented languages, such as Java or C#, references may be null. These references need to be checked to ensure they are not null before invoking any methods, 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. [3]

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. A key purpose of using a null object is to avoid conditionals of different kinds, resulting in code that is more focused, quicker to read and follow - i e improved readability. One 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.


Since the child nodes may not exist, one must modify the procedure by adding non-existence or null checks:

function tree_size(node) {     set sum = 1     if node.left exists {         sum = sum + tree_size(node.left)     }     if node.right exists {         sum = sum + tree_size(node.right)     }     return sum }

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) {     return 1 + tree_size(node.left) + tree_size(node.right) }
function tree_size(null_node) {     return 0 }

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.

It is not a pattern from Design Patterns , but is mentioned in Martin Fowler's Refactoring [4] and Joshua Kerievsky's Refactoring To Patterns [5] as the Insert Null Object refactoring.

Chapter 17 of Robert Cecil Martin's Agile Software Development: Principles, Patterns and Practices [6] 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 higherusingSystem;namespaceConsoleApplication2{classProgram{staticvoidMain(string[]args){stringstr="test";Console.WriteLine(str?.Length);Console.ReadKey();}}}// The output will be:// 4

Extension methods and Null coalescing

In some Microsoft .NET 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 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 higherusingSystem;usingSystem.Linq;namespaceMyExtensionWithExample{publicstaticclassStringExtensions{publicstaticintSafeGetLength(thisstringvalueOrNull){return(valueOrNull??string.Empty).Length;}}publicstaticclassProgram{// define some stringsstaticreadonlystring[]strings=new[]{"Mr X.","Katrien Duck",null,"Q"};// write the total length of all the strings in the arraypublicstaticvoidMain(string[]args){varquery=fromtextinstringsselecttext.SafeGetLength();// no need to do any checks hereConsole.WriteLine(query.Sum());}}}// 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<iostream>classAnimal{public:virtual~Animal()=default;virtualvoidMakeSound()const=0;};classDog:publicAnimal{public:virtualvoidMakeSound()constoverride{std::cout<<"woof!"<<std::endl;}};classNullAnimal:publicAnimal{public:virtualvoidMakeSound()constoverride{}};

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.voidDoSomething(constAnimal&animal){// |animal| may never be null here.}// Function which may accept an |Animal| instance or null.voidDoSomething(constAnimal*animal){// |animal| may be null.}

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: */usingSystem;// Animal interface is the key to compatibility for Animal implementations below.interfaceIAnimal{voidMakeSound();}// Animal is the base case.abstractclassAnimal:IAnimal{// A shared instance that can be used for comparisonspublicstaticreadonlyIAnimalNull=newNullAnimal();// The Null Case: this NullAnimal class should be used in place of C# null keyword.privateclassNullAnimal:Animal{publicoverridevoidMakeSound(){// Purposefully provides no behaviour.}}publicabstractvoidMakeSound();}// Dog is a real animal.classDog:Animal{publicoverridevoidMakeSound(){Console.WriteLine("Woof!");}}/* ========================= * Simplistic usage example in a Main entry point. */staticclassProgram{staticvoidMain(){IAnimaldog=newDog();dog.MakeSound();// outputs "Woof!"/* Instead of using C# null, use the Animal.Null instance.         * This example is simplistic but conveys the idea that if the Animal.Null instance is used then the program         * will never experience a .NET System.NullReferenceException at runtime, unlike if C# null were used.         */IAnimalunknown=Animal.Null;//<< replaces: IAnimal unknown = null;unknown.MakeSound();// outputs nothing, but does not throw a runtime exception        }}

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 nilis 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(defclassdog()());; 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.(defmethodmake-sound((objdog))(formatt"woof!~%"));; allow (make-sound nil) to work via specialization to null class.;; innocuous empty body: nil makes no sound.(defmethodmake-sound((objnull)))

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, language inheritance is not necessary to provide expected behavior.

classDogdefsound"bark"endendclassNilAnimaldefsound(*);endenddefget_animal(animal=NilAnimal.new)animalendget_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, language inheritance is not necessary to provide expected behavior.

classDog{sound(){return'bark';}}classNullAnimal{sound(){returnnull;}}functiongetAnimal(type){returntype==='dog'?newDog():newNullAnimal();}['dog',null].map((animal)=>getAnimal(animal).sound());// Returns ["bark", null]

Java

publicinterfaceAnimal{voidmakeSound();}publicclassDogimplementsAnimal{publicvoidmakeSound(){System.out.println("woof!");}}publicclassNullAnimalimplementsAnimal{publicvoidmakeSound(){// silence...}}

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

interfaceAnimal{publicfunctionmakeSound();}classDogimplementsAnimal{publicfunctionmakeSound(){echo"Woof...\n";}}classCatimplementsAnimal{publicfunctionmakeSound(){echo"Meowww...\n";}}classNullAnimalimplementsAnimal{publicfunctionmakeSound(){// silence...}}$animalType='elephant';functionmakeAnimalFromAnimalType(string$animalType):Animal{switch($animalType){case'dog':returnnewDog();case'cat':returnnewCat();default:returnnewNullAnimal();}}makeAnimalFromAnimalType($animalType)->makeSound();// ..the null animal makes no soundfunctionanimalMakeSound(Animal$animal):void{$animal->makeSound();}foreach([makeAnimalFromAnimalType('dog'),makeAnimalFromAnimalType('NullAnimal'),makeAnimalFromAnimalType('cat'),]as$animal){// That's also reduce null handling codeanimalMakeSound($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.).

PublicClassAnimalPublicSharedReadOnlyEmptyAsAnimal=NewAnimalEmpty()PublicOverridableSubMakeSound()Console.WriteLine("Woof!")EndSubEndClassFriendNotInheritableClassAnimalEmptyInheritsAnimalPublicOverridesSubMakeSound()' EndSubEndClass

Criticism

This pattern should be used carefully as it can make errors/bugs appear as normal program execution. [7]

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 types 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 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

Related Research Articles

A visitor pattern is a software design pattern that separates the algorithm from the object structure. Because of this separation new operations can be added to existing object structures without modifying the structures. It is one way to follow the open/closed principle in object-oriented programming and software engineering.

In computer programming, lazy initialization is the tactic of delaying the creation of an object, the calculation of a value, or some other expensive process until the first time it is needed. It is a kind of lazy evaluation that refers specifically to the instantiation of objects or other resources.

In programming language theory and type theory, polymorphism is the provision of a single interface to entities of different types or the use of a single symbol to represent multiple different types. The concept is borrowed from a principle in biology where an organism or species can have many different forms or stages.

In computer science, a tagged union, also called a variant, variant record, choice type, discriminated union, disjoint union, sum type or coproduct, is a data structure used to hold a value that could take on several different, but fixed, types. Only one of the types can be in use at any one time, and a tag field explicitly indicates which one is in use. It can be thought of as a type that has several "cases", each of which should be handled correctly when that type is manipulated. This is critical in defining recursive datatypes, in which some component of a value may have the same type as that value, for example in defining a type for representing trees, where it is necessary to distinguish multi-node subtrees and leaves. Like ordinary unions, tagged unions can save storage by overlapping storage areas for each type, since only one is in use at a time.

<span class="mw-page-title-main">Pointer (computer programming)</span> Object which stores memory addresses in a computer program

In computer science, a pointer is an object in many programming languages that stores a memory address. This can be that of another value located in computer memory, or in some cases, that of memory-mapped computer hardware. A pointer references a location in memory, and obtaining the value stored at that location is known as dereferencing the pointer. As an analogy, a page number in a book's index could be considered a pointer to the corresponding page; dereferencing such a pointer would be done by flipping to the page with the given page number and reading the text found on that page. The actual format and content of a pointer variable is dependent on the underlying computer architecture.

In computer science, dynamic dispatch is the process of selecting which implementation of a polymorphic operation to call at run time. It is commonly employed in, and considered a prime characteristic of, object-oriented programming (OOP) languages and systems.

<span class="mw-page-title-main">Java syntax</span> Set of rules defining correctly structured program

The syntax of Java is the set of rules defining how a Java program is written and interpreted.

In computing, a null pointer or null reference is a value saved for indicating that the pointer or reference does not refer to a valid object. Programs routinely use null pointers to represent conditions such as the end of a list of unknown length or the failure to perform some action; this use of null pointers can be compared to nullable types and to the Nothing value in an option type.

<span class="mw-page-title-main">Dependency injection</span> Software programming technique

In software engineering, dependency injection is a programming technique in which an object or function receives other objects or functions that it requires, as opposed to creating them internally. Dependency injection aims to separate the concerns of constructing objects and using them, leading to loosely coupled programs. The pattern ensures that an object or function which wants to use a given service should not have to know how to construct those services. Instead, the receiving 'client' is provided with its dependencies by external code, which it is not aware of. Dependency injection makes implicit dependencies explicit and helps solve the following problems:

<span class="mw-page-title-main">Factory (object-oriented programming)</span> An object which creates other objects

In object-oriented programming, a factory is an object for creating other objects; formally, it is a function or method that returns objects of a varying prototype or class from some method call, which is assumed to be "new". More broadly, a subroutine that returns a "new" object may be referred to as a "factory", as in factory method or factory function. The factory pattern is the basis for a number of related software design patterns.

The object pool pattern is a software creational design pattern that uses a set of initialized objects kept ready to use – a "pool" – rather than allocating and destroying them on demand. A client of the pool will request an object from the pool and perform operations on the returned object. When the client has finished, it returns the object to the pool rather than destroying it; this can be done manually or automatically.

<span class="mw-page-title-main">Multiton pattern</span> Software engineering design pattern

In software engineering, the multiton pattern is a design pattern which generalizes the singleton pattern. Whereas the singleton allows only one instance of a class to be created, the multiton pattern allows for the controlled creation of multiple instances, which it manages through the use of a map.

In the area of mathematical logic and computer science known as type theory, a unit type is a type that allows only one value. The carrier associated with a unit type can be any singleton set. There is an isomorphism between any two such sets, so it is customary to talk about the unit type and ignore the details of its value. One may also regard the unit type as the type of 0-tuples, i.e. the product of no types.

In type theory, a theory within mathematical logic, the bottom type of a type system is the type that is a subtype of all other types.

Haxe is a high-level cross-platform programming language and compiler that can produce applications and source code for many different computing platforms from one code-base. It is free and open-source software, released under the MIT License. The compiler, written in OCaml, is released under the GNU General Public License (GPL) version 2.

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 dynamically typed languages, equivalent behavior is provided by having a single null value.

This article describes the syntax of the C# programming language. The features described are compatible with .NET Framework and Mono.

The computer programming language, C#, introduces several new features in version 2.0. These include:

In software engineering, the module pattern is a design pattern used to implement the concept of software modules, defined by modular programming, in a programming language with incomplete direct support for the concept.

In programming language theory, flow-sensitive typing is a type system where the type of an expression depends on its position in the control flow.

References

  1. Kühne, Thomas (1996). "Void Value". Proceedings of the First International Conference on Object-Oriented Technology, White Object-Oriented Nights 1996 (WOON'96), St. Petersburg, Russia.
  2. Woolf, Bobby (1998). "Null Object". In Martin, Robert; Riehle, Dirk; Buschmann, Frank (eds.). Pattern Languages of Program Design 3. Addison-Wesley.
  3. "Working with Objects (Working with nil)". iOS Developer Library. Apple, Inc. 2012-12-13. Retrieved 2014-05-19.
  4. Fowler, Martin (1999). Refactoring. Improving the Design of Existing Code . Addison-Wesley. ISBN   0-201-48567-2.
  5. Kerievsky, Joshua (2004). Refactoring To Patterns. Addison-Wesley. ISBN   0-321-21335-1.
  6. Martin, Robert (2002). Agile Software Development: Principles, Patterns and Practices . Pearson Education. ISBN   0-13-597444-5.
  7. Fowler, Martin (1999). Refactoring pp. 216