Subject: Re: Using "extra" blocks to declare local variables (Was: switch/extension for see below strongly needed)
scott@slp53.sl.home (Scott Lurndal) writes:
I often declare local variables within a compound statement
introduced by a do/while/for/case construct.
I do this too, although I don't know if I would say often.
Probably closer to 50/50. Also the same applies to if().
I commonly use the declaration syntax in for() statements to have
an iteration variable (or variables) local to the loop.
In pre or post C99 code, I do not create compound statements
willy-nilly just to hide a prior defined local variable with the
same name (a maintenance nightmare, to be sure) or to locate the
declaration closer to the use in a function.
I agree with both of these. Very rarely I might introduce a
compound statement "out of the blue" not for either those reasons
but expressly to limit the scope of a variable in the midst of a
long and complicated function. Especially because such cases are
rare, I like to flag them with double braces, something like
{{ ULL carry = 0;
for(...){
... uses of carry
... uses of carry
}
if(...) uses of carry
if(...)
}}
(and yes, in case anyone is wondering, there are reasons why the
code in this case couldn't be abstracted into a sub-function).
--- PyGate Linux v1.5.14
* Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)