Decoupling Ruby: Delegation vs Dependency Injection

We've all worked with tightly-coupled code. If a butterfly flaps its wings in China, the unit tests break. Maintaining a system like this is...unpleasant. In this article, Jonathan Miles dives into the origins of tight-coupling. He demonstrates how you can use dependency injection (DI) to decouple code. Then he introduces a novel decoupling technique based on delegation that can be useful when DI is not an option.

In Object-Oriented Programming, one object will often depend on another object in order to function.

For example, if I create a simple class to run finance reports:

class FinanceReport
  def net_income
    FinanceApi.gross_income - FinanceApi.total_costs
  end
end

We can say that FinanceReport depends on FinanceApi, which it uses to pull information from an external payment processor.

But what if we want to hit a different API at some point? Or, more likely, what if we want to test this class without hitting external resources? The most common answer is to use Dependency Injection.

With Dependency Injection, we don't explicitly refer to FinanceApi inside FinanceReport. Instead, we pass it in as an argument. We inject it.

Using Dependency Injection, our class becomes:

class FinanceReport
  def net_income(financials)
    financials.gross_income - financials.total_costs
  end
end

Now our class has no knowledge that the FinanceApi object even exists! We can pass any object to it as long as it implements gross_income and total_costs.

This has a number of benefits:

  • Our code is now less "coupled" to FinanceApi.
  • We're forced to use FinanceApi via a public interface.
  • We can now pass in a mock or stub object in our tests so that we don't have to hit the real API.

Most developers consider Dependency Injection to be a good thing in general (me too!). However, as with all techniques, there are trade-offs.

Our code is slightly more opaque now. When we explicitly used FinanceApi, it was clear where our values were coming from. It's not quite as clear in the code incorporating Dependency Injection.

If the calls would otherwise have gone to self, then we have made the code more verbose. Instead of using the Object-Oriented "send a message to an object and let it act" paradigm, we find ourselves moving to a more functional "inputs -> outputs" paradigm.

It is this last case (redirecting calls that would have gone to self) that I want to look at today. I want to present a possible alternative to Dependency Injection for these situations: change the base class dynamically (kinda).

The Problem to Solve

Let’s back up a moment and start with the problem that led me down this path to start with: PDF reports.

My client requested the ability to generate various printable PDF reports — one report listing all expenses for an account, another listing revenue, another the forecasted profits for future years, etc.

We’re using the venerable prawn gem to create these PDFs, with each report being its own Ruby object subclassed from Prawn::Document.

Something like this:

class CostReport < Prawn::Document
  def initialize(...)
    ...
  end

  def render
    text "Cost Report"
    move_down 20
    ...
  end

So far, so good. But here’s the rub: the client wants an “Overview” report that includes portions from all these other reports.

Solution 1: Dependency Injection

As mentioned previously, one common solution to this kind of problem is to refactor the code to use Dependency Injection. That is, rather than having all these reports call methods on self, we will instead pass in our PDF document as an argument.

This would give us something more like:

class CostReport < Prawn::Document
...
  def title(pdf = self)
    pdf.text "Cost Report"
    pdf.move_down 20
    ...
  end
end

This works, but there is some overhead here. For one thing, every single drawing method now has to take the pdf argument, and every single call to prawn now has to go through this pdf argument.

Dependency injection has some benefits: it pushes us toward decoupled components in our system and allows us to pass in mocks or stubs to make unit testing easier.

However, we are not reaping the rewards of these benefits in our scenario. We are already strongly coupled to the prawn API, so changing to a different PDF library would almost certainly require an entire rewrite of the code.

Testing is also not a big concern here, because in our case testing generated PDF reports with automated tests is too cumbersome to be worthwhile.

So Dependency Injection gives us the behavior we want but also introduces additional overhead with minimal benefits for us. Let’s have a look at another option.

Solution 2: Delegation

Ruby’s standard library provides us SimpleDelegator as an easy way to implement the decorator pattern. You pass in your object to the constructor, and then any method calls to the delegator are forwarded to your object.

Using SimpleDelegator, we can create a base report class that wraps around prawn.

class PrawnWrapper < SimpleDelegator
  def initialize(document: nil)
    document ||= Prawn::Document.new(...)
    super(document)
  end
end

We can then update our reports to inherit from this class, and they will still function the same as before, using the default document created in our initializer. The magic happens when we use this in our overview report:

class OverviewReport < PrawnWrapper
  ...
  def render
    sales = SaleReport.new(..., document: self)
    sales.sales_table
    costs = CostReport.new(..., document: self)
    costs.costs_pie_chart
    ...
  end
end

Here SaleReport#sales_table and CostReport#costs_pie_chart remain unchanged, but their calls to prawn (e.g., text(...), move_down 20, etc.) are now being forwarded to OverviewReport via the SimpleDelegator we created.

In terms of behavior, we have essentially made it as if SalesReport is now a subclass of OverviewReport. In our case, this means that all the calls to prawn’s API now go SalesReport -> OverviewReport -> Prawn::Document.

How SimpleDelegator Works

The way SimpleDelegator works under the hood is basically to use Ruby's method_missing functionality to forward method calls to another object.

So SimpleDelegator (or a subclass of it) receives a method call. If it implements that method, great; it will execute it just as any other object would. However, it if does not have that method defined, then it will hit method_missing. method_missing will then attempt to call that method on the object given to its constructor.

A simple example:

require 'simple_delegator'
class Thing
  def one
    'one'
  end
  def two
    'two'
  end
end

class ThingDecorator < SimpleDelegator
  def two
    'three!'
  end
end

ThingDecorator.new(Thing.new).one #=> "one"
ThingDecorator.new(Thing.new).two #=> "three!"

By subclassing SimpleDelegator with our own ThingDecorator class here, we can overwrite some methods and let others fall through to the default Thing object.

The trivial example above doesn’t really do SimpleDelegator justice, though. You may look at this code and very well say to me, “Doesn’t subclassing Thing give me the same outcome?”

Yes, yes it does. But here’s the key difference: SimpleDelegator takes the object it will delegate to as an argument in its constructor. This means we can pass in different objects at runtime.

This is what allows use to redirect the calls to a prawn object in Solution 2 above. If we call a single report, the prawn calls go to a new document created in the constructor. The overview report, however, can change this so that calls to prawn are forwarded to its document.

Conclusion

Dependency Injection is probably the best solution to most decoupling problems most of the time.

As with all techniques, however, there are trade-offs. In my case, I didn’t think that the overhead introduced by DI was worth the benefits it provided, so I looked for another solution.

As with all things in Ruby, there’s always another way. I wouldn’t reach for this solution often, but it is certainly a nice addition to your Ruby toolbelt for these situations.

What to do next:
  1. Try Honeybadger for FREE
    Honeybadger helps you find and fix errors before your users can even report them. Get set up in minutes and check monitoring off your to-do list.
    Start free trial
    Easy 5-minute setup — No credit card required
  2. Get the Honeybadger newsletter
    Each month we share news, best practices, and stories from the DevOps & monitoring community—exclusively for developers like you.
    author photo

    Jonathan Miles

    Jonathan began his career as a C/C++ developer but has since transitioned to web development with Ruby on Rails. 3D printing is his main hobby but lately all his spare time is taken up with being a first-time dad to a rambunctious toddler.

    More articles by Jonathan Miles
    Stop wasting time manually checking logs for errors!

    Try the only application health monitoring tool that allows you to track application errors, uptime, and cron jobs in one simple platform.

    • Know when critical errors occur, and which customers are affected.
    • Respond instantly when your systems go down.
    • Improve the health of your systems over time.
    • Fix problems before your customers can report them!

    As developers ourselves, we hated wasting time tracking down errors—so we built the system we always wanted.

    Honeybadger tracks everything you need and nothing you don't, creating one simple solution to keep your application running and error free so you can do what you do best—release new code. Try it free and see for yourself.

    Start free trial
    Simple 5-minute setup — No credit card required

    Learn more

    "We've looked at a lot of error management systems. Honeybadger is head and shoulders above the rest and somehow gets better with every new release."
    — Michael Smith, Cofounder & CTO of YvesBlue

    Honeybadger is trusted by top companies like:

    “Everyone is in love with Honeybadger ... the UI is spot on.”
    Molly Struve, Sr. Site Reliability Engineer, Netflix
    Start free trial
    Are you using Sentry, Rollbar, Bugsnag, or Airbrake for your monitoring? Honeybadger includes error tracking with a whole suite of amazing monitoring tools — all for probably less than you're paying now. Discover why so many companies are switching to Honeybadger here.
    Start free trial