Newbie q

Ivan Tomek ivan.tomek at acadiau.ca
Fri Sep 26 12:16:00 UTC 2003


Hi,

I would just like to add two things:

- Initializing variables is not necessarily the best practice. If the
variable is not certain to be used and requires computational resources, it
may be better to use lazy initialization as somebody suggested. This relies
on each variable being initialized to nil as pointed out below.
- If 'variables' means instance or similar kind of variables, there is
nothing to prevent one from defining and using a class-creation method with
keywords for initialization. This way, instance variables can be initialized
during instantiation, possibly even in a context-sensitive manner by
defining the initialization operation via an expression, etc.

Ivan


Ivan Tomek
School of Computer Science
Acadia University
Canada


> -----Original Message-----
> From: goran.krampe at bluefish.se [mailto:goran.krampe at bluefish.se]
> Sent: Friday, September 26, 2003 4:23 AM
> To: The general-purpose Squeak developers list
> Subject: Re: Newbie q
> 
> 
> 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, Goran
> 
> 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