April 04, 2004

The Art of the Shortcut

One thing you see a lot in Java programmers is the use of Javabean-style properties: classes with a pile of "get" and "set" methods that allow you to manipulate their state. Now the arguments as to whether this is acceptable practice or Considered Harmful are legion, and I'm not going into them today. Suffice to say I spend most of my time writing Java, and some habits get carried around with you.

One thing I wanted to do in Ruby, though, was to find some way to lessen the pain caused by having to change a bunch of properties at once. I did so by adding a method to the Object class that would allow me to pass in a hash of name->value pairs, and have them automatically mapped to object property "setter" methods. (In Ruby, the convention for a setter is to have a method called "foo=", which through a little operator-overloading magic allows you to write "myobject.foo = bar" in your code)

class Object
  def set_properties(props)
    props.each { |key, value| self.send("#{key}=", value) }
  end
end

This makes the following fragments of code equivalent:

  1. user.name = "Charles"
    user.email="cmiller@pastiche.org"
    user.website="http://fishbowl.pastiche.org"
  2. user.set_properties ({ :name => "Charles", 
      :email => "cmiller@pastiche.org", 
      :website => "http://fishbowl.pastiche.org" })

Of course, one of the most important things one should make sure of when writing a shorter way to do something, is that the short way isn't, say, 30% longer than the one you started with.

"Alright, we're here. Let us never speak of the shortcut again.".

Posted to nerd at April 4, 2004 10:00 AM
Comments currently disabled due to spam. If you want to comment on a post, email me, and I'll try to incorporate your feedback somehow.
Trackbacks <http://fishbowl.pastiche.org/mt-tb.cgi/507>

Finding the shortcut and using it effectively: Charles Miller examines the getter/setter syndrome in Java1 from a usage (rather than the normal construction) perspective and devices a...

From: Loud Thinking at April 5, 2004 01:06 AM
Comments

Amen. It's easy to have a clever idea. The mark of a craftsman is to evaluate it in practice, and, most importantly throw it away if it turns out to be a bad idea.

Posted by: Alan Green at April 4, 2004 12:39 PM (#link)

I wrote an article on how Rails using this shortcut to relief controllers of per-attribute knowledge responsibility: http://www.loudthinking.com/arc/000212.html

Posted by: David Heinemeier Hansson at April 5, 2004 01:15 AM (#link)

Oh, never mind that comment. Seems like trackback is working just fine for this :)

Posted by: David Heinemeier Hansson at April 5, 2004 01:16 AM (#link)

It seems to me that what you want is

user.with {
name = "Charles";
email = "cmiller@pastiche.org";
website = "http://fishbowl.pastiche.org"
}

where 'with' simply aliases to instance_eval and you trust the client programmer not to abuse the fact.

Posted by: Martin DeMello at June 21, 2004 06:04 PM (#link)