DEV Community

Cover image for Something out of Nothing: Null Object Pattern
hkly
hkly

Posted on • Originally published at blog.beezwax.net

Something out of Nothing: Null Object Pattern

Originally published on Beezwax.net.


What is nothing? nil? null? undefined? A vast void of emptiness that fills your soul with dread? Oh sorry, that's just my stomach.

We often think of nothing as... well, nothing. It's when something doesn't exist and therefore cannot be interacted with. So in our code, we try account for having nothing. No User? No problem.

def display_name
  if @user.present?
    @user.name.titlecase
  else
    "Guest User"
  end
end

Fixed it with a conditional. Right...?

But what happens when you want something else from the User elsewhere? Use another conditional?

def avatar
  if @user.present?
    @user.avatar
  else
    default_avatar
  end
end

Technically it would solve your problem for now, but soon there will a bunch of if statements cluttering your code when all you really want to do is simply call a method.

Here's where the Null Object Pattern can come in handy.

What if instead of nothing, you had a something. Something that responded to the API of your object. That something would be the "null object"-- It is an object that represents when you have no object, kind of like a placeholder.

Instead of having to check whether or not you have a User object, you can ensure you always have something that will respond to your method calls by providing a null object that responds to the API of your object.

class GuestUser
  def name
    "Guest User"
  end

  def avatar
    default_avatar
  end
end

Now you can use this GuestUser whenever you don't have a User.

def initialize(user = GuestUser.new)
  @user = user
end

def display_name
  @user.name.titlecase
end

def avatar
  @user.avatar
end

As you can see in the above example, I've injected the GuestUser in as a default parameter in the constructor. Now if a User isn't passed in, the code will use the GuestUser, and no more need for additional conditionals!

To the keen eye, you'll notice that this Null Object Pattern is actually an example of Composition over Inheritance! Check out this post on Composition over Inheritance if you'd like to learn more.

The code examples are in Ruby, but the concept can be applied to any Object Oriented programming.


Cover photo by Casey Horner.

Top comments (0)