When you are accessing private, internal state of your object, do you prefer to use attribute accessors (getter and setter methods) or direct instance variable access? Consider this contrived example:
class LineItem def initialize(item, quantity) @item = item @quantity = quantity end # Access our internal state via instance variables def total @item.price * @quantity end # Or via attr_readers def total item.price * quantity end #... which would require something like this: private attr_reader :item, :quantity end
Which would you prefer? In the simplest cases, the two are functionally equivalent, so does it matter? Is there a point where your preference for one over the other changes? What are the pros and cons of each approach?
We’ll be discussing this at the thoughtbot Boston developer discussion tomorrow afternoon and will post our notes and any follow-up here. Please feel free to contribute your thoughts here at any time.