🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.misc
Article #6702

Use of scope

#6702
From: Erland Sommarskog
Date: Sat, 10 Feb 1990 22:26
46 lines
2898 bytes
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.
Technical Details
Message-ID: <760@enea.se>
Path: arc1-ca.pugleaf.net!utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!cs.utexas.edu!samsung!zaphod.mps.ohio-state.edu!tut.cis.ohio-state.edu!ucbvax!bloom-beacon!eru!luth!sunic!enea!sommar
References: <PCG.90Feb5163252@rupert.cs.aber.ac.uk> <14226@lambda.UUCP>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!cs.utexas.edu!samsung!zaphod.mps.ohio-state.edu!tut.cis.ohio-state.edu!ucbvax!bloom-beacon!eru!luth!sunic!enea!sommar
From: sommar@enea.se (Erland Sommarskog)
Newsgroups: comp.lang.misc
Subject: Use of scope
Message-ID: <760@enea.se>
Date: 10 Feb 90 22:26:21 GMT
References: <PCG.90Feb5163252@rupert.cs.aber.ac.uk> <14226@lambda.UUCP>
Organization: Enea Data AB, Sweden
Lines: 45