0% found this document useful (0 votes)
25 views1 page

Why You Need Null Objects

The document discusses the benefits of using null objects instead of null values in code. It provides examples of when null objects can make code more expressive by indicating the absence of a value, explicitly showing when errors occur, and preventing method signatures from lying about return types or behavior.

Uploaded by

NikolaMilosevic
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views1 page

Why You Need Null Objects

The document discusses the benefits of using null objects instead of null values in code. It provides examples of when null objects can make code more expressive by indicating the absence of a value, explicitly showing when errors occur, and preventing method signatures from lying about return types or behavior.

Uploaded by

NikolaMilosevic
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Sign in Get started

WRIT E FOR US CODING INT ERVIEW COURS E →

You have 2 free stories left this month. Sign up and get an extra one for free.

APPLIED DESIGN PAT T ERNS

Why You Need Null Objects


Let me tell you why this weird pseudo null-object is so great
Nicklas Millard Follow
Jun 11 · 4 min read

So, I wrote this piece “Stop Checking for Nulls” and it took some heat
from a few developers doubting the NOP’s usefulness. I figured I’d take
some time and provide additional examples.

Stop Checking for Nulls


Null Object Pattern, Factory Methods — Let’s see some production ready
code!
medium.com

For those who didn’t read the previous article and still don’t know what a
Null Object is, here’s a simple explanation.

The main purpose is to return a suitable substitute


for a null. The Null Object simply allows for more
expressive code.

Let’s have a look at some examples when the Null Object is more
expressive than just using nulls that’d easily wreck your application.

1 Lying method signatures


A method signature may lie about what it does in two ways: 1) the return
type, and 2) the method name is incorrect.

We’d often see something like this in a repository. Without the null-
object, the problem with this one line of code is that it’s lying about what
it returns and what it does.

Reading this code tells me I can always, as in, always, count on receiving
an Author type. But that’s not true, because some junior developer might
allow it to return null.

What to do instead
If you don’t want to apply the null pattern, instead write code that
actually tells you want to expect from it. The method signature about
would need to be changed to the signature below.

You see the question mark after Author, and the name now includes
“Default”? Now we know we’d possibly end up with a null calling this
method.

2 Expressing the absence of a value


Null would probably mean “there’s no value”. But, we can be more
expressive than that, can’t we?

When performing a query, you can communicate the absence of a value


by returning an Author.Unknown type. It’s just as easy to check for as a
null, but it also allows you to pass it on if you’d want to. The object would
have very predictive behavior: it’d do nothing.

3 Explicitly tell if something went wrong


Using factories or factory methods, you’re able to return more
specialized objects that allow you to assert if something went wrong.

It’s not always you’d want to throw exceptions. As you’re obviously very
aware of, exceptions should only ever be thrown if something
exceptional happened.

Providing incorrect input to a method is often not very exceptional. It’s


common. We allow for these commonalities by returning a null object
that represents something went wrong.

What the critics say


Switch to another language with null-safety
That’s not always an option. Often times we’re not in total control of
which language we’re allowed to use. Some clients demand us to use C#,
Java, JavaScript, etc.

The Null Object Pattern is confusing


It’s less confusing than returning null if nothing is found. I wouldn’t
know if the null represents a missing value or if something went wrong.
At least returning a null object named something like “Unknown” tells us
what we looked for couldn’t be found.

This Pattern is Old


So is MVC, MVVM, State Pattern, Repository Pattern, and pretty much
every pattern in The Gang of Four’s Design Patterns.

Even the languages we use are old. Python is from 1990. Java is from
1995. Yeah, that’s right. Python is even more old school than Java. But
we still use them.

This Pattern is Obsolete


It’s still being applied in green field projects because it elegantly solves an
issue. There’s nothing obsolete about NOP.

It’s hiding errors


Well — As I’ve demonstrated above, it’s actually bringing errors right in
front of you, easily detected and debugged.

Sign up for Top Stories from Level Up Coding


A monthly summary of the best stories shared in Level Up Coding

Create a free Medium account to get Top Stories in


Get this newsletter your inbox.

Programming Software Engineering Dotnet Data Science Software Development

62 claps

WRIT T EN BY
Nicklas Millard Follow

Senior Tech Consultant with an opinion. Sharing what I learn


at client projects. I build bon res and hand lighter uid and
matches to the reader.

Level Up Coding Follow

Coding tutorials and news. The developer homepage


gitconnected.com

Write the rst response

More From Medium

Great Habits Every Why Programmers Need How to squash git How Do You Determine
Developer Should Work a Training Journal commits your Salary As A Startup
on Amy M Haddad in Level Up Srebalaji T hirumalai in Level CEO?
Daan in Level Up Coding Coding Up Coding brett fox in Level Up Coding

Go Make or New Linux user tries Make the switch to 5 T hings T hat Are Hard
Patrick Gopher in Level Up Windows, in 2020 Typescript today To Grasp When You
Coding Dominik Tarnowski in Level Up Taylor Coon in Level Up Start Programming
Coding Coding Daan in Level Up Coding

Discover Medium Make Medium yours Become a member


Welcome to a place where words matter. On Medium, smart Follow all the topics you care about, and we’ll deliver the Get unlimited access to the best stories on Medium — and
voices and original ideas take center stage - with no ads in best stories for you to your homepage and inbox. Explore support writers while you’re at it. Just $5/month. Upgrade
sight. Watch

About Help Legal

You might also like