Last Updated: February 25, 2016
·
470
· t-dnzt

Don't break the law... of Demeter !

I'm sure you know about the Law of Demeter. It's also known as the principle of least knowledge. Let's see how to apply it in Ruby on Rails !

If you don't know about it, let me explain it quicly. The law of Demeter is a programming guideline implying that an entity should only talk to its close friends. If we put in another way, an object should not call methods through another object.

When using Rails and the cool associations feature, it can be tempting to chain method calls like this:

product.provider.name  
provider.address.city
company.building.city

These lines violate the Law of Demeter. To fix it, we should change them to be :

product.provider_name
provider.address_city #provider.city also works in this case
company.city

This way, it's much cleaner and we don't break the law anymore. But to do that, we need to add a bunch of methods to our models :

class Product < ActiveRecord::Base
  belongs_to :provider

  def provider_name
    provider.name
  end

  # More methods to access the provider's attributes
end

class Provider < ActiveRecord::Base
  has_many :products

  # Has a name attribute
end

You get the idea ! The models will grow out of control if we define methods for each attribute like this. But Rails has a solution : Delegate.

class Product < ActiveRecord::Base
  belongs_to :provider

  delegate :name, to: :provider, prefix: true
end

class Provider < ActiveRecord::Base
  has_many :products

  # Has a name attribute
end

Note the use of the prefix key. It allows us to use product.provider_name instead of just product.name since a product would probably have its own name.

Delegate is a really powerful feature that let you clean up your models. Let's see another example.

If we have a Company that belongs to a Building, we could do :

class Company < ActiveRecord::Base
  belongs_to :building

  delegate :street, :city, :country, to: :building
end

class Building < ActiveRecord::Base
  has_many :companies

  attr_accessible :street, :city, :country
end

Now, we don't break the law of Demeter anymore since we can access the street directly from the company object. Plus, less code inside our models ;)

If you never used the delegate method, it's time to add it to your toolbox !

You can get more information about delegate in the documentation.