Null Object refactoring pattern jumps into action when we have cases where we are checking if given object is present and if it’s not then we are returning the default value for given attributes or methods. It usually requires from us to use if condition which makes code a little bit less readable and a little bit harder to test - with Null Object pattern code is very simple and easy to test.

To better demonstrate advantages of using Null Object pattern let’s consider following case: we have two classes, User object which operates on Post class.

class User < ActiveRecord::Base
  has_many :posts

  def latest_post_title
    post = posts.order('created_at DESC').first

    if post.present?
      post.title
    else
      "No posts yet"
    end
  end
end

It’s far from single responsibility principle - we are doing a few actions here:

  1. We fetch newest post
  2. We are checking if post is present
  3. We are displaying post title if post is present
  4. We are displaying proper information when post is not present

This is the time when Null Object comes in. Let’s create our new object first:

class NoPost
  def title
    "No posts yet"
  end
end

Want to receive useful tips, information about new Ruby gems and articles on a daily basis? Make sure you follow me and say hello!

It’s just a plain Ruby object with simple logic. Now it’s time to refactor our User model by doing a couple things:

  1. We will move query to separated method
  2. We will move latest post assignment to separated method using NoPost Null Object
  3. We would simplify method responsible for returning latest post title

After implementing all of these points, our User class is way cleaner and more readable:

class User < ActiveRecord::Base
  has_many :posts

  def latest_post_title
    lastest_post.title
  end

  private

  def latest_post
    find_latest_post || NoPost.new
  end

  def find_latest_post
    posts.order('created_at DESC').first
  end
end

The contents of our User#latest_post_title method is obvious now and we get rid of if condition. It’s also very important to name our Null Object properly so its name reflects the idea behind it.

Want to become a better Rails developer?
Download for free the Introduction Rails patterns book and dive into the world of refactoring and easy-testable Ruby code today.
Join over 1,000 developers already subscribed to my newsletter and download the book. You can unsubscribe anytime:

Subscribe and get the book!