Suggestion of Demeter

Posted by Doug Fri, 24 Oct 2008 17:30:00 GMT

Frankly, I haven’t ever really made up my mind about how to address the Law of Demeter in Rails. I’d like to use this article as a spring board to discuss the alternatives.

As found on Wikipedia, the Law of Demeter says “Only talk to your immediate friends.” There’s a good write-up about it by Brian Donovan specifc to rails, Law of Demeter, or How to avoid coding yourself into a corner in Rails. In this article he sums up nicely the advantage of delegation.

Let’s look at an example:

class User < ActiveRecord::Base
  has_one :profile
end

class Profile < ActiveRecord::Base
  has_one :address
end

class Address < ActiveRecord::Base
end

Now if you want to find out what country the user lives in you need to access user.profile.address.country. That’s a violation of the LoD. The problem is if you refactor profile or address you’ll have to go back and fix all the places you did this access chaining as well. The common answer is to delegate!

class User < ActiveRecord::Base
  has_one :profile
  delegate :country, :to => :profile
end

class Profile < ActiveRecord::Base
  has_one :address
  delegate :country, :to => :address
end

class Address < ActiveRecord::Base
end

Now we can just access user.country. The problem so far is that (at least in Rails 2.1) if the user’s profile is nil or if the profile’s address is nil then the call to user.country will blow up. There are some patches and work arounds, but the problem exists. Also, this double delegation smells to me and I don’t know why. Maybe it’s the obtuseness of this particular design. Maybe double-delegation is a smell of bad modeling.

There’s a new feature in Rails 2.2 that relate to delegation and using a prefix for delegated methods. This might be a little contrived, but continuing with our above example:

class User < ActiveRecord::Base
  has_one :profile
  delegate :country, :to => :profile, :prefix => true
end

Means that now we call user.profile_country instead of user.country. Instead of passing true to the prefix option, you can also pass a string that will get used instead of the delegator object name.

I question why user.profile_country is any better than user.profile.country. If you rename the profile class, you’re almost always going to want to rename that prefix too. So everywhere in your code you called user.profile_country you’ll end up changing it to user.foobar_country.

Finally, I’m going to bring up Jay Field’s Presenter Pattern. Basically, this is creating an object that aggregates multiple objects so they can easily be presented on the same form simply. In addition, the presenter object gives you a place to put formatting and some computational logic that sometimes finds its way into views.

The Presenter Pattern seems neat, but I’m not sure when to use it instead of delegation straight from the “parent” object. In the example on Jay’s page he talks about a user_account, user_credential, and an address. All that gets aggregated into a CompletePresenter. But really, that seems contrived too. His user account only has a name and the credentials has the username and password. It seems more likely that you’d just define a user that has a name, username, and password and allow the user to has_one address. Why not just delegate the address attributes directly from the now combined user class?

I’ll also mention the Rails fields_for helper. This seems to be an effort to address having multiple objects on the same web form. Personally, I think the net effect of that is to push more logic into your controller to deal with these multiple groups of fields. Both the presenter pattern and straight up method delegation push this back into the models and out of the controllers. Maybe I just don’t understand how this is meant to be used.

So I’ll close with these questions. When is the Presenter Pattern better than delegation from your already existing models? When is the added complexity of an additional layer of objects worth it? What about double delegation? Is it a smell and for what? Should I just get over my unease when I see double delegation? What’s the real value of using a prefix for delegated methods? What are the legitimate uses of fields_for? Also, why doesn’t the Rails delegate method handle the case where the delegation target is nil? I know there are work arounds and patches, but it seems like the Rails implementation of delegate is broke.

Posted in ,  | Tags , , , , , ,  | 2 comments

My First Day of Freedom

Posted by Doug Mon, 21 Jan 2008 13:49:00 GMT

Friday was my last day at the company that won’t let me blog about them. I am very excited to be 100% independent freelancing! This has been a goal of mine for many years and it’s finally come true.

This morning Mark Windholtz is coming over to pair with me on a contract we share. It’s his good for not making me commute on my first day of independence. I’m looking forward to working with him and several other independent developers.

Hopefully now that I’m not under corporate thumbs I’ll be able to blog more about the types of problems I’m seeing and methods for overcoming. I’ve specifically been asked to write an article on my experiences working remotely on a Scrum project.

In addition to writing more, I’m also hoping to be able to contribute back to the open source community. It’s tricky when your family depends on your billable hours to also give your work away. However, I think it’s good balance to work on things my clients require as well as things I’m interested in. At first, this is likely to be helping Phil Hagelberg shape up Emacs for ruby development.

So, there you go. I’m going to be working with some good guys; doing more writing; and working on more open source. Wish me luck!

Tags , , , ,  | 4 comments

In the Fishbowl

Posted by Doug Wed, 08 Mar 2006 13:43:13 GMT

At last night’s XP Cinci we did an interesting exercise called “The Fishbowl”. Mark Windholtz setup his powerbook with a terminal, Safari, TextMate and a time tracking application he had been working on. We then each took turns pair programming on the big screen. While there were two of us “at the console” all the time, we swapped one of them out every 10 minutes.

The exercise was interesting for several reasons. Mostly it turned out to be a good example of what test driven development looks like in Ruby on Rails. The skill level of xp-cinci is split pretty evenly. About half of us have done significant RoR development and the other half is either just interested or just learning. Regardless of skill level though, no one was going to code in public at an XP meeting without writing tests. The best way to learn TDD is by doing. Until you’ve lived through the development cycle of TDD it’s hard to really grasp what it feels like.

The other benefit from the exercise was a fairly lively discussion on “this is how we do it in Rails” versus “this is what I’m used to in Java.” Most of xp-cinci comes from a strong Java background. Even though about half of us are “ruby nubies,” pretty much everyone has a very strong developer background with one technology or another. Here’s a for-instance. I coded up a method that used MyModel.find_by_id(params[:id]) and was asked why I used find_by_id rather than just find. I said that I liked how find_by_id returned nil so I could use it as a false value when doing error checking. As a long-time Java smart-guy, Ed Summerfield was pretty quick to jump on this as a bad practice. He demonstrated how his Rails controllers looked using begin and rescue. I’m not entirely convinced that assigning nil to an object as a fail condition is bad, but his code looked fairly neat with all his error trapping in rescue blocks.

While the skill levels of various members varied, our application we worked on wasn’t really the typical “hello world” style application of tutorials. We started with a working application. Mark and Scott are actually using the application as part of their consulting work at Rails Studio. This gave us the chance to work in a more “normal” fare. We had an existing database we were migrating; we had existing code we had to live within; and we had a “real” customer looking for “real” improvements to their application.

Last night’s meeting was different from our usual fare. It was back to a hands on style where we actually wrote code. While we didn’t get very far in terms of feature points, I think we made a lot of progress in general understanding of both coding practices and Rails development. I hope we continue doing more and talking less.

Posted in , , ,  | Tags , , ,  | 5 comments

Older posts: 1 2 3 4

Copyright 2001 - 2005 by Lathi.net and Doug Alcorn

Creative Commons, Some Rights Reserved Ruby on Rails Developer Powered by Debian GNU/Linux Powered by Typo