Tim vs. Accessors

Ralph Johnson johnson at cs.uiuc.edu
Sun Oct 7 09:43:50 UTC 2007


I agree with Tim on this one.  Don't make accessors for private variables.

It only takes a minute or two to refactor a class manually to make
accessors for a variable and to replace uses of the variable with use
of the accessors.  It takes only seconds with the refactoring browser.
 So, I do not buy the argument that accessors makes your code more
flexible.

Accessors make it hard to figure out who is really changing things.
This is especially true if your variables have typical names like
"name" or "contents" or "children".  If you look for all senders of
the accessors, you will get a huge list, most of whom are irrelevant.

So, code is much clearer if it does NOT use accessors for private
variables, and it is almost as easy to change as code that does.
Therefore, use accessors ONLY when you want a variable to be public.

-Ralph Johnson



More information about the Squeak-dev mailing list