[Tutor] Re: Re: small program in Python and in C++

dominic.fox dominic.fox" <dominic.fox@ntlworld.com
Thu, 11 Jul 2002 21:25:36 +0100


Just a couple of comments on variable declarations. I used to hate having to
declare variables in C - I used to hate it enough to use it as a reason not
to learn C (I thought it was a way of artificially making things harder that
should be easier, which is the way my dad still thinks about C in its
entirety). Now when I'm programming in VB, which for my sins is what I do
all day in my day job, I declare everything up-front as a matter of course,
for no other reason than that I like to group together the bits of the code
that say what kinds of data a given method or function is going to be
dealing with. Increasingly, if I find I have a lot of disparate data types
being declared at the start of a function, I start wondering about which of
them could be moved into the header of a separate function.

Quite aside from code maintenance issues, I find it useful to make the kind
of statement about the *design* of a function that up-front variable
declarations makes. It helps me to see when there's something wrong with
that design, for instance when a function is trying to do too many different
kinds of things at once. Spreading declarations through the code mixes up
design and implementation - to me, it feels like putting pre- and
post-condifitions and invariants in the middle of a method would feel to an
Eiffel programmer...

Dominic