Miscellaneous questions...

Alan Lovejoy squeak-dev.sourcery at forum-mail.net
Fri Sep 9 05:03:26 UTC 2005


Tim Phoenix>"Nope. But unless I have a reason to hide them, I have accessors
for all instance variables, as a rule."

Blake>"Not a strong believer in encapsulation?<s>"

I'm with you, Blake.  I only add accessors and/or mutators when I have a
demonstrated need for them.  And I don't automatically add a mutator for
each acessor (or vice versa,) either.

In Smalltalk, there is no justification for "**always** using an
accessor/mutator method."  There are architectural and design reasons for
doing that in particular cases, just as there are architectural and design
reasons for not doing that in other cases.  One size does not fit all.

An accessor method must be justified by one of two requirements: 1) The
value of the instance variable needs to be provided as a part of the
object's API, or 2) the value must be lazily initialized or recomputed.
Otherwise, an accessor is not only unnecessary, it violates the information
hiding principle.

A mutator method must be justified by one of two requirements: 1) The value
of the instance variable must be settable as part of the object's public
API, or 2) there is an invariant that must be maintained whenever the value
of the instance variable is changed (e.g., a change event must be
propagated, the value of another instance variable must be recomputed, etc.)
Otherwise, a mutator is not only unnecessary, it violates encapuslation.

Re: Global Symbols

Blake>[Using global symbols] "Could make for some REALLY obscure bugs under
some unlikely situations...."

Such as? Symbols aren't mutable. And they're flyweights.  Other than that,
they are subject to all the same misuses as would apply to String instances,
aren't they? (Your original question did suggest using Strings as the
alternative.)

Blake>"A deck of cards IS an ordered collection of cards."

Except when it's a SortedCollection of cards.  Or an Array of cards. Or a
linked list of cards. Or... I think you get the point.

Also, it's not necessarily the case that all the operations in the API of
Collection should be supported by a deck of cards. More importantly, the
level of abstraction and the semantics are both different.  For one thing, a
deck of cards should not contain numbers, Strings nor Timestamps. For
another, there is both a semantic and a behavioral distinction between the
object that represents the state of a particular deck of cards at a
particular moment in a particular game and one that defines the complete set
of cards with which the game is played.

--Alan





More information about the Squeak-dev mailing list