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.
Without wishing to send startHolyWar to BeginnersList, do any of the experienced developers work this way, or is it just a theoretical position?
cheers AB
On Sat, Sep 27, 2008 at 1:40 PM, Andy Burnett andy.burnett@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
On Sat, 27 Sep 2008 20:47:39 -0700, Ralph Johnson johnson@cs.uiuc.edu wrote:
On Sat, Sep 27, 2008 at 1:40 PM, Andy Burnett andy.burnett@knowinnovation.com wrote:
I read somewhere - can't remember where - that setters are considered evil.
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.
If you're going to assign an action verb to a setter, it should be reflective of the process.
donut := Donut makePlain. donut ice: #chocolate. donut addSprinkles: jimmies atRandom.
IMO.
On Sun, 28 Sep 2008 13:27:52 -0700, Blake blake@kingdomrpg.com wrote:
If you're going to assign an action verb to a setter, it should be reflective of the process.
donut := Donut makePlain. donut ice: #chocolate. donut addSprinkles: jimmies atRandom.
Looking at this again, it doesn't seem very illustrative.
What I mean is that if you're going to assign an action verb like sprayPaint, there should be some reason that it's =not= "color", i.e, that it reflects some other aspect of the process besides changing the color. For example,
car color: #red.
says to me, "the color is red, whatever it was before is no more." We're divorcing the code from physical reality (which is perfectly fine and usual in most cases).
Other action verbs should be indicative of either the logical process we want the client to be aware of or a physical reality that we're modeling
On Sunday 28 Sep 2008 12:10:37 am Andy Burnett 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.
I don't recall ever seeing the word 'setter' in blue book. In Smalltalk, you only send messages to the object. The sender has no control over the send's effect on the internal variables of an object.
The question itself is mischievous :-).
Subbu
beginners@lists.squeakfoundation.org