Newbie q

Richard A. O'Keefe ok at cs.otago.ac.nz
Sun Sep 28 23:15:17 UTC 2003


Phil Hudson <phil.hudson at iname.com> wrote:
	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?
	
Where is it written that that is even a _good_ practice at all,
let alone a best practice?  Who says it is "widely accepted"?  I'm
familiar with a lot of the programming practice literature, and I
haven't seen this piece of idiocy "widely accepted" therein.

This is one of the totally mindless rules which in practice leads to bad
code.  I am sick of seeing C code which initialises variables to values
that never get used; that is lying to the reader, and lying to the reader
is NEVER good idea.  Variables should only ever be initialised when you
have a value that you intend to use that you can initialise them with.

Note that Smalltalk variables *are* initialised (to nil), just as
instance variables are always initialised in Java, so you don't have to
fuss about initialising things for safety reasons.

	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)?
	
What Smalltalk would _gain_ would be complexity;
what it would _lose_ would be readability.

Note that
(A) Smalltalk methods are almost always very short, so that declarations
    and initialisations aren't widely separated in the first place.
(B) Smalltalk makes it easy to write and use small blocks, so you can
    divide scopes up really small if you want to.



More information about the Squeak-dev mailing list