Switching on an Attribute of Another Object

It is a bad idea to do a switch based on an attribute of another object. If you must, it should be on your own data, not someone else’s. - Refactoring by Martin Fowler

We’re reading Refactoring as part of an internal book club. This quote reminds me of the Feature Envy code smell.

class Cart
  def price
    @item.price + @item.tax
  end
end

If most of the work in a method is being done on another object (@item), the method should be on that other object.

If you haven’t played with Kevin Rutherford’s Reek gem before, it’s a neat way to find smells like Feature Envy in your Ruby code.