From: Ryan Davis Date: 2008-02-13T17:51:33+09:00 Subject: Re: @var = change to accessor method? Why? On Feb 12, 2008, at 8:50 PM, Philipp Hofmann wrote: > accessors are meant to make the instance variables of an object to be > accessible from outside. creating accessors for every variable used in > an object (in order to get rid of @s) makes every variable accessible > from outside the object. therefore the data is not encapsulated any > more. I'm afraid you're confusing encapsulation with information hiding [0], especially as it pertains to inheritance. Both are good tools when used in the right context, but they are not the same. You do not "break" your code every time you use one of the attr generator methods. Instead you provide an interface to some information that your class/instance provides. You're not opening a vault and letting everything run crazy/chaotic by providing access to those instance variables. Instead, you're providing a clean and orderly way of getting at that data[1]. Providing accessor methods is the only sane way to make your code and its subclasses more resilient to change. It allows you to change the internals without affecting the subclasses that use that interface. Classes that use ivars directly are more brittle and require more work to maintain. From Beck's Smalltalk Best Practices: > If you need to code for inheritance, use Indirect Variable Access. > Future generations will thank you. > > One warning--do not go half way. If you are going to use direct > access, use it for all access. [0] http://c2.com/cgi/wiki?EncapsulationIsNotInformationHiding [1] As I've said before, if it is anywhere in ruby and evaled or parsed in any way, I can get my hands on it and manipulate it. If that scares or bothers you, you're using the wrong language.