InfoQ

News

Respect Demeter's Law through Rails Plugin

Posted by Sebastien Auvray on Oct 25, 2007 09:00 AM

Community
Ruby
Topics
Ruby on Rails,
Design
Tags
Design Guideline,
Demeter Law
The Law of Demeter or Principle of Least Knowledge is a design guideline for developing software. The fundamental notion is that a given object should assume as little as possible about the structure, properties and behavior of anything else (including its subcomponents). Dan Manges helped to clarify the concept and the way to apply it in Ruby, notably through the use of the Forwardable module. Using mocks and stubs, Luke Redpath came across Demeter's law violation when writing his Unit Tests:
class WidgetsControllerCreateActionTest < Test::Unit::TestCase
def setup
# usual rails controller test setup here
@user = mock('user')
User.stubs(:find).returns(@user)
end

def test_should_create_new_widget_for_parent_user_using_posted_widget_params
widgets_proxy = mock('association proxy')
@user.stubs(:widgets).returns(widgets_proxy)
# Demeter's Law Violation here by using the widget_proxy through User object
widgets_proxy.expects(:create).with(:name => 'my funky widget')
post :create, :widget => {:name
=> 'my funky widget'}
end
The solution is to add a delegate method on all your models. But that quickly becomes boring, which is the reason why Luke introduced the Demeter's Revenge plugin which will create a collection of Demeter-compliant methods for your has_many and has_and_belongs_to_many associations:
# given a User that has_many Widgets you'll be able to use:
user.build_widget(params) # => user.widgets.build(params)
user.create_widget(params) # => user.widgets.create(params)
# ...
But aren't laws made to be broken? And the fact that a plugin can automate a so called "Law" isn't this making it null and void?

2 comments

Reply

Just to clarify a few things. by Luke Redpath Posted Oct 25, 2007 11:36 AM
Re: Just to clarify a few things. by berkay NiQuiL Posted Jun 30, 2008 5:53 PM
  1. Back to top

    Just to clarify a few things.

    Oct 25, 2007 11:36 AM by Luke Redpath

    I thought I'd made this clear on my blog but I'd like to respond to the final comment regarding a law being null and void because it can apparently be "fixed automatically". My plugin targets a very specific instance of demeter violation - that is, violations found in the inherent design of ActiveRecord - and nothing more. I wasn't happy with the way ActiveRecord practically forces you into breaking encapsulation by exposing the internals of its associations and the pain felt writing stubs and mocks is simply what drove me to finally do something about it; thanks to Ruby's dynamic nature, I was able to save myself writing the same repetitive wrapper (encapsulating) methods time and time again. I'm sure most readers will recognise that there is far more to the law of demeter than the one specific issue that my plugin sets out to solve and if you'd like to read more google provides a variety of articles on the subject. I'm somewhat concerned that some people seem to be interpreting this plugin as some kind of 'silver bullet' for all of your demeter violating woes (I thought I was quite explicit about that) and my blog entry links to some other articles that explore the fundamental issues more closely which I encourage people to read.

  2. Back to top

    Re: Just to clarify a few things.

    Jun 30, 2008 5:53 PM by berkay NiQuiL

Exclusive Content

Rationalizing the Presentation Tier

Thin client paradigm characterized by web applications is a kludge that needs to be repudiated. Old compromises are no longer needed and it's time to move the presentation tier to where it belongs.

Agile Project Management: Lessons Learned at Google

In this presentation filmed during QCon 2007, Jeff Sutherland, the creator of Scrum, talks about his visit at Google to do an analysis of Google's first implementation of Scrum.

AtomServer – The Power of Publishing for Data Distribution

In this article, Bryon Jacob and Chris Berry introduce AtomServer, their implementation of a full-fledged Atom Store based on Apache Abdera, which is now available as open source.

An Introduction to Virtualization

It is easy to think that virtualization applies only to servers. In reality the recent resurgence of the concept is also being applied to networking, storage, and application infrastructure.

REST Anti-Patterns

In this article, Stefan Tilkov explains some of the most common anti-patterns found in applications that claim to follow a "RESTful" design and suggests ways to avoid them.

Choosing between Routing and Orchestration in an ESB

In this article, Adrien Louis and Marc Dutoo discuss the differences and relative merits of using orchestration vs. routing in a typical ESB setup, and discuss various implementation options.

Enterprise Batch Processing with Spring

Wayne Lund discusses batch processing, Spring Batch objectives and features, scenarios for usage, Spring Batch architecture, scaling, example code, failures and retrying, and the future roadmap.

User Story Estimation Techniques

Developer Jay Fields draws on his experiences as a ThoughtWorks consultant to describe effective user story estimation techniques.