http://qs321.pair.com?node_id=309966


in reply to Why get() and set() accessor methods are evil

If we later decide to change the implementation of our list to a hash our loop will also process the hash keys (this would most likely not be what we want). We have to change code outside our object to conform with the new internal data structure of the object.

No, you just need to change your accessor so that it returns the values instead of the whole hash. You incur the cost of building an array where you could just return a premade one before, but it's dangerous to return references to your internal data structures anyway (you lose control of what's in them). Being able to make this kind of change is most of the argument behind using accessors instead of data members in the first place.

I agree that accessors can be misused, but I'm a little worried that people are going to interpret your advice as meaning that you should always use an iterator or visitor pattern instead of returning an array. In perl, that's not necessary.

  • Comment on Re: Why get() and set() accessor methods are evil