[Newbies] Are setters a good idea?

Ralph Johnson johnson at cs.uiuc.edu
Sun Sep 28 03:47:39 UTC 2008

On Sat, Sep 27, 2008 at 1:40 PM, Andy Burnett
<andy.burnett at knowinnovation.com> wrote:
> I read somewhere - can't remember where - that setters are considered evil.
> The argument was that in the real world you can't suddenly change the colour
> of a car, just by settings its colour value. Instead, you have to perform
> some action, e.g. sprayPaintCar: aColour.  Therefore in the OO world, one
> should tackle problems in the same way, i.e. if you want to change the value
> of an instance variable, you have to call some sort of action method, that
> makes sense in the context of the object.

This is not a good argument.  Many Smalltalk objects are not modeled
after the real world.  Even if they are, is there really any
difference between sprayPaint: aColour and colour: aColour?

Setters are neither good nor evil.  It is how you use them that is
good or evil.  Many people overuse setters.  I try to avoid setters
because it is harder to tell where a variable is modified if there is
a setter.  But sometimes they are just what you need.

-Ralph Johnson

More information about the Beginners mailing list