[comp.lang.smalltalk] Data Hiding...

yon@apollo.HP.COM (David Yon) (01/19/90)

	Thanks for all the responses to my posting on Data Hiding
in Smalltalk.

	First off, to those who questioned this...YES, I know *why*
the data-hiding breaks down, that was never in question.

	However, what I wanted to bring to light is that there is
an awful lot of Smalltalk code (at least when browsing the STV/286
system) that just answers one of it's instance variables, instead 
of making a copy.  Although this is standard practice, it would seem
easy to be lulled into a false sense of security, since after all: 
"only an object can modify it's own internal state".  If "get" and "put"
methods merely answer references rather than new instances, this 
is decidedly not that case.  There are of course many contexts in 
which answering with a reference is the right thing to do.  There 
are also many times where it's unclear whether you should answer by
reference or with a new instance.

	Thanks again for the response.

David Yon

johnson@p.cs.uiuc.edu (01/24/90)

I agree with David Yon that accessing another's objects instance
variables is often (usually?) not a good thing to do.  I call this
treating an object like a record.  When I see code that does this
(all too often I am the author, but I fix other people's code, too)
then I will try to understand why the program is accessing the
instance variables and try to create new methods in the object's
class that eliminate the need to have the accessing methods.  This
almost always improves the quality of the code.  Sometimes accessing
the instance variables is the correct thing to do, but often it is not.

Ralph Johnson

segall@caip.rutgers.edu (Ed Segall) (01/25/90)

I agree as well that returning a reference to an object 
is often used when it is more appropriate to return a copy of 
the object.  This is not an issue for things like SmallIntegers, 
for which references and copies are the same.  It is, however, 
an issue for instances of most other classes.

People who are used to programming in Lisp (for example) don't usually
confuse references with copies, but people more used to numeric
programming often do.  I think the important think to remember is that
in Smalltalk, just about everything is a reference (pointer), and that
when programming (or writing programming manuals), it is important to
keep that straight.  It is especially important to be aware of this
when writing code that returns some object.  I always ask myself: "Do I
want to return THIS object, or a copy of it?".


--Ed Segall
-- 


uucp:   {...}!rutgers!caip.rutgers.edu!segall
arpa:   segall@caip.rutgers.edu