Please use accessors!

Terry Raymond traymond at
Thu Aug 5 02:58:06 UTC 1999

On Sun, 01 Aug 1999 13:19:10 -0500, John Brant wrote:

[John' reasons deleted]

I agree with John.  But my biggest reason for not using accessors is that
I spend much of my time reading other people's code and I find it a real
pain to have to read an accessing method just because the writer liked
the style.  When you are reading someone else's code you have to read
all methods, you cannot assume that what looks like an accessor is just
an accessor.  If that were so, then having them so they could be overridden
would not make sense.  Direct variable access is much less ambiguous than
accessors and therefore clearer.

As I understand XP programming you first write the code to what you know,
you don't add features because they might be used.  Later, when you find
you need a feature you add it.  With that philosophy you would use direct
access until you find you need to override a method.  Then you use the
refactoring browser to change the direct access to use an accessor.

>Often I hear arguments that one should use accessors because it is easier
>to debug since you can put debugging statements in the accessor methods. If
>you where to use direct variable referencing, then you would have to change
>a bunch of methods. On the other hand, people in the direct accessor camp
>often claim that it is harder to debug a program with accessors since you
>cannot tell by looking at the message send whether this just a simple
>setter or is doing something more complex. Since probably 95% of all
>accessors are simple, it is easy to step over something that looks like an
>accessor only to find that half of your state has changed.
>While both of these arguments are valid, I believe that they are really
>problems with the development environment, not the programs. If the
>development environment supported break/watchpoints, then one wouldn't have
>to use accessors to easily add debugging statements. You would enter your
>watchpoint and the development environment would add the necessary code to
>all methods. 

If you are a VisualWorks user, the Professional Debug Package enables the
user to put breakpoints and watchpoints at all instance variable references
in a class and its subclasses.

Don't sacrifice readability for deficient tools, change the tools, Squeak is
great for that.  That is what John and I have done in VW (different tools).

Terry Raymond
Crafted Smalltalk
(401) 846-6573

More information about the Squeak-dev mailing list