Newbie q

goran.krampe at bluefish.se goran.krampe at bluefish.se
Fri Sep 26 08:22:32 UTC 2003


Phil Hudson <phil.hudson at iname.com> wrote:
> >Welcome to Squeak Phil!
> 
> Thank you. I'm hooked already.

Good. :-) Very good newbie resource btw:
	http://www.iam.unibe.ch/~ducasse/WebPages/FreeBooks.html
 
> >If you mean the class definition when you say "declaration," the answer is
> >no.  Usually one puts the initialization of a variable to some default
> >value in the #initialize instance method.
> 
> Hmm. This prevents us from following a widely-accepted "best practice" in
> programming: always initialize every variable as soon as it comes into
> scope. Isn't this a Really Really Bad Thing?

Eh.. :-) Well, this is not generally perceived as a problem in
Smalltalk. At least not that I am aware of. If you are not aware of it -
variables are indeed automatically "initialized" to refer to nil. So I
am not sure what you mean with this "best practice" that is not being
followed...

> Put another way: wouldn't Smalltalk gain something useful, and lose
> nothing at all, by enabling initialization of each variable at the point
> where it is declared (or named, or introduced, or whatever is correct in
> Smalltalk)?

Well, what is the principal difference between:

| x y |
x _ 10.
y _ 'Hubba'

...and:

| x _ 10. y _ 'Hubba'|

...which in turn (if we add a newline and removed the ||) can be written
as:

x _ 10.
y _ 'Hubba'

So the only difference is in fact that in Smalltalk we have a list of
the variable names first, before the initializing code. And frankly, I
find that neat - very easy to see what variables we have in the method
without a clutter of initializing code (focusing on method temporaries
here).

Also note that most initialization can't be sensibly done at the time of
declaration. But since we have class vars, class instvars and instvars
we can make an educated choice based on the life cycle of the value at
hand and we then initialize the variable in either the instance side
#initialize method (or similar) or in a class side #initialize method.

regards, Göran

PS. Squeak is a Smalltalk - and Smalltalk has been around for...
eternity. :) This doesn't mean that it is perfect, but it does mean that
most issues have already been discussed at one time or another.



More information about the Squeak-dev mailing list