[comp.lang.misc] Use of scope

sommar@enea.se (Erland Sommarskog) (02/11/90)

Jim Giles (jlg@lambda.UUCP) writes:
>Piercarlo Grandi (pcg@rupert.cs.aber.ac.uk) writes:
>> A careful programmer will never declare a variable for a scope larger
>> than that in which it is used, [...]
>
>I disagree.  A careful programmer will neither write extremely monolithic
>code, nor write code which is fragmented into lots of little scopes.  If
>I have a 50 line function which uses XYZ in only the middle third, I'm
>not likekly to make a new scope just for XYZ.  I _may_ split out the
>middle third _if_ it seems to be a distinct and nearly independent
>segment (but, then I would ask myself whether the code should actually
>all be in the same function at all).

I tend to agree with Piercarlo Grandi here. Not from the view of
optimization, but to keep the code clean. I prefer to declare a
variable as close as possible to where it is used. Whether I do or
not, depends on the language. Languagues where you can declare
variable in the beginning of a block like Simula or Ada are thankful
to work with.
  Mostly evil forces make me program in Pascal where I would have to
make a separate procedure. In the case Jim Giles describes I would
tend to break out that middle third; 50 lines is a quite long function.
  (One consequence of my obscene desire is that I always put the VAR
section after procedures and functions unless I have some global ones,
in which case I split the VAR section in two.)
                                                            
>> [...]                          will never reuse a variable with the same
>> name for two different roles.
>
>Now, I agree with that.  However, I make one caveat: in most languages
>including C, the idiom for the use of index variables in loops is single
>letter variables.  Such variables are often used in separate loops (with
>non-overlapping scopes of course) without any sign of confusion that I've
>ever noticed.

Ada is really nice here. You declare the index variable with the FOR
loop, so that you can only refer to it within the loop.

The nightmare is standard Pascal where you must declare the variables
before internal functions and procedures. If you have a loop in the main
body that calls an internal routine that has another loop and use the
variable name i in both cases is jolly fun when you forgot to declare
the i in the inner function.
-- 
Erland Sommarskog - ENEA Data, Stockholm - sommar@enea.se
Unix is a virus.

poser@csli.Stanford.EDU (Bill Poser) (02/11/90)

In article <760@enea.se> sommar@enea.se (Erland Sommarskog) writes:
>Ada is really nice here. You declare the index variable with the FOR
>loop, so that you can only refer to it within the loop.

This is true of some other languages as well, but I'm not sure if it is
a feature. When writing C I find that it is often very convenient to
have access to the loop index. Sometimes testing it is an easy way to
detect an unusual loop exit that requires special handling, and sometimes
I want to process part of an array in the first loop, exit at a point
that may be data dependent, then resume processing in a second loop
that needs to know where to start. (And often a for loop is more convenient
than a while loop.)

schwartz@cs.psu.edu (Scott Schwartz) (02/11/90)

In article <760@enea.se> sommar@enea.se (Erland Sommarskog) writes:
>Ada is really nice here. You declare the index variable with the FOR
>loop, so that you can only refer to it within the loop.
>
>The nightmare is standard Pascal where you must declare the variables
>before internal functions and procedures.

My favorite is "Stroustrup's revenge": in C++ you can write
	for (int i = 0; ... ; ... ) { ... }
but the variable has scope outside the loop.  

-- 
Scott Schwartz		schwartz@cs.psu.edu

"the same idea is applied today in the use of slide rules." -- Don Knuth 

new@udel.edu (Darren New) (02/13/90)

In article <760@enea.se> sommar@enea.se (Erland Sommarskog) writes:
>The nightmare is standard Pascal where you must declare the variables
>before internal functions and procedures. If you have a loop in the main
>body that calls an internal routine that has another loop and use the
>variable name i in both cases is jolly fun when you forgot to declare
>the i in the inner function.

Actually, in standard Pascal, such a construct is illegal.  If you have a
loop index, it is not allowed to be "threatened" by anything inside the
loop, including functions or procedures called. I think most compilers
that check such things check that it does not get passed as a var parameter.
I may be wrong, but I also think it is illegal to use a more-global
variable as a loop index; i.e., the inner function would be considered
illegal because it uses a non-local var as a loop index.  Of course, if
your compiler does not check this, you're stuck.   -- Darren