What is the Null Object Design Pattern?

Remove unnecessary conditional complexity

Jakub Kapuscik
Jan 27, 2020 · 2 min read
Image for post
Image for post
Photo by Monika Dhita Adiati on Unsplash

It is often the case that we have to check if a variable is null before using it. If we query a model from the database it might turn out to be nothing. Therefore, we have to add an extra if to check if it was the case.

The null object pattern introduces an alternative solution. We can use a dummy object that shares the same interference as the original but does not implement any behavior.

Our code gets cleaner and we can remove the “if”. The fewer “ifs” the better.

If a product with a given name exists, we will receive a proper model. Otherwise, null will be returned. By introducing a NullProduct that has no behavior, we can simplify our application.

NullProduct will introduce only default behavior with no underlying logic. It will be returned by ProductDAO if no model is found. Therefore, the return type of the getProductByName method will always be of type Product.

It is much better in many cases if some objects can do nothing. Most of the time, we do not want to use cache during development on our local machine. It could get really frustrating.

At the same time, we need to be sure that our software could be cached and everything will work in production.

Such a problem can also be solved by providing a dummy implementation of a cache component that does nothing and can easily be substituted with real ones.

We may have different implementations that store the cache in MySQL, Redis, Memcached, memory or any other possible place.

A sample implementation of in-memory cache

If we are developing the application locally, all we have to do is configure the cache to use our dummy implementation.

Advantages

  • Can simplify code and remove conditional complexity.
  • Can be helpful during development.
  • Simplifies interface for clients.

Disadvantages

  • Can hide errors and create unexpected behavior if improperly implemented.
  • Can over-complicate an application.

Better Programming

Advice for programmers.

Sign up for The Best of Better Programming

By Better Programming

A weekly newsletter sent every Friday with the best articles we published that week. Code tutorials, advice, career opportunities, and more! Take a look

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Jakub Kapuscik

Written by

Tech Manager at Miinto.dk, guineafowl enthusiast

Better Programming

Advice for programmers.

Jakub Kapuscik

Written by

Tech Manager at Miinto.dk, guineafowl enthusiast

Better Programming

Advice for programmers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store