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 _______________________________________________ Beginners mailing list [hidden email] http://lists.squeakfoundation.org/mailman/listinfo/beginners |
The short answer is that you are exposing the internals of the object. If you have an object that is just a packet of values, great. If you have an object that will always have e.g. a colour, then possibly great, or possibly not depending on the domain.
On Sat, Sep 27, 2008 at 7:40 PM, Andy Burnett <[hidden email]> wrote:
_______________________________________________ Beginners mailing list [hidden email] http://lists.squeakfoundation.org/mailman/listinfo/beginners |
In reply to this post by Andy Burnett
On Sat, Sep 27, 2008 at 1:40 PM, Andy Burnett
<[hidden email]> 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 _______________________________________________ Beginners mailing list [hidden email] http://lists.squeakfoundation.org/mailman/listinfo/beginners |
On Sat, 27 Sep 2008 20:47:39 -0700, Ralph Johnson <[hidden email]>
wrote: > On Sat, Sep 27, 2008 at 1:40 PM, Andy Burnett > <[hidden email]> 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. _______________________________________________ Beginners mailing list [hidden email] http://lists.squeakfoundation.org/mailman/listinfo/beginners |
On Sun, 28 Sep 2008 13:27:52 -0700, Blake <[hidden email]> 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_______________________________________________ Beginners mailing list [hidden email] http://lists.squeakfoundation.org/mailman/listinfo/beginners |
In reply to this post by Andy Burnett
Thank you to everyone who responded. It seems as though the summary view is:
1. don't create setters unless you really need them, because it can make it harder to see when values are changed 2. if one uses action verbs, they should truly reflect the physical or logical action taking place. All of which seems very reasonable to me :-) Cheers AB _______________________________________________ Beginners mailing list [hidden email] http://lists.squeakfoundation.org/mailman/listinfo/beginners |
In reply to this post by Andy Burnett
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 mailing list [hidden email] http://lists.squeakfoundation.org/mailman/listinfo/beginners |
Free forum by Nabble | Edit this page |