Newsgroups: comp.lang.smalltalk Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!wuarchive!m.cs.uiuc.edu!cs.uiuc.edu!voss From: voss@cs.uiuc.edu (Bill Voss) Subject: Re: Access methods - New feature ? In-Reply-To: mjohnson@sirius.acs.calpoly.edu's message of 23 Apr 91 05:10:32 GMT Message-ID: Sender: news@m.cs.uiuc.edu (News Database (admin-Mike Schwager)) Nntp-Posting-Host: laslo.cs.uiuc.edu Organization: Typed Smalltalk Group, Dept of Comp Sci, Univ of IL in Urbana. References: <1991Apr21.221149.8057@vuse.vanderbilt.edu> <2813c1c8.5a14@petunia.CalPoly.EDU> Distribution: na Date: 23 Apr 91 17:00:44 Lines: 76 > but what about when using > simple "readonly" methods to extract information from a "record-like" > object? My objection to access methods is in part because they do encourage "record-like" objects, instead of more active objects. The use of access methods tends to indicate that the programmer is using a Class the way a Pascal programmer would use a Record. Instead of having the object do the work, the programmer has the object store information, then regurgitate raw information which code outside of the object manipulates. > If the object Mark below is a "record-like" object containing > personal information about me, what's wrong with this type of access > > Mark eyeColor > > to check my eye color? Note that Mark does not necessarily have an > instance variable named eyeColor. Maybe alot, maybe nothing. To answer your question I need to know WHY you want to know Mark's eyeColor. Do you want to see if Mark has the same color eyes as Bob? Then you should use Mark sameColorEyesAs: Bob ifTrue:..... instead of m <- Mark eyeColor. b <- Bob eyeColor. m = b ifTrue:..... Do you want to draw a picture of Mark's eyes? Then you should use Mark drawEyesOn: someDisplayable at: somePoint andSoOn:.... instead of m <- Mark eyeColor. blaBlaBla... color: m. The key point is that code which needs to know about Mark's eye color should be INSIDE the class which keeps track of Mark's eye color. This could be the class Human, or Human could contain two instances of class Eye. In that case the class Human would split up "drawEyesOn:" for example into two "drawYourselfOn:" messages, one for each eye. The code which needs to know about Mark's eyes should almost never be located in some external method which simply send's Mark an "eyeColor" message. Thus in my (never humble ;-) opinion, if Mark is a "record-like" object, you have already made a fundamental mistake. You should have used Pascal/.... which directly supports Records instead. (but please don't Smalltalk is much nicer. :-) > As an aid to me learning Smalltalk in a vacuum, can anyone recommend a > Smalltalk "style guide" which addresses these issues? I don't think such a book exists. Just monitor comp.lang.smalltalk for messages containing words like "extreme" "wrong" .... ;-) Exercise: Assume two equivalent systems, both need to determine in say twenty different places wether or not Mark and Somebody have the same color eyes. One system uses my "sameColorEyesAs:" method, and one system uses the "eyeColor" method. Your job (should you choose to accept it) is to change one of the two systems to support people with different color eyes. (Someone with a brown left eye, and a green right eye does NOT have the same color eyes as someone with TWO green eyes.) Which system would you rather change? Then (try to) write your code that way to begin with. Bill Voss -- voss@cs.uiuc.edu