Sitemap
Better Programming

Advice for programmers.

Member-only story

Design Patterns: Null Object

8 min readDec 26, 2019

--

Photo by Thor Alvis on Unsplash

There are 23 classic design patterns, which are described in the original book, “Design Patterns: Elements of Reusable Object-Oriented Software.” These patterns provide solutions to particular problems, often repeated in the software development.

In this article, I am going to describe what the Null-Object Pattern is and how and when it should be applied. This pattern is not included in the classic pattern book, but it was first published in the “Pattern Languages of Program,” and it is widely used to avoid complexity.

Basic Idea

In object-oriented computer programming, a null object is an object with no referenced value or with defined neutral (“null”) behaviour. The null object design pattern describes the uses of such objects and their behaviour (or lack thereof).—Wikipedia

The main feature of this pattern is that this allows us to avoid complexity in our code. In most languages, such as Java, C# or JavaScript, the references may be null. Depending on our business logic, checking the code may be necessary to ensure they are not null before invoking any methods because methods typically cannot be invoked on null references.

--

--

Carlos Caballero
Carlos Caballero

Written by Carlos Caballero

Hi! My name is Carlos Caballero and I’m PhD. in Computer Science from Málaga, Spain. Teaching developers and degree/master computer science how to be experts!

Responses (3)