Re: variable scope in for loop

From: Rolf Magnus (ramagnus_at_t-online.de)
Date: 01/08/04


Date: Thu, 08 Jan 2004 13:43:05 +0100

Howard wrote:

>> By making this change, once, with the approval of our design team
>> and putting it in a controlled place, it gets around a zillion
>> Microsoft specific hacks that involve bad coding practices of leaving
>> variables uninitialized or sitting around outside the scope they are
>> used in.
>>
>
> I'm curious why you call it "bad coding practice" to have a single
> variable
> used more than once. I understand that i is in this case just an
> integer, and there's no logical reason why it *shouldn't* be declared
> in each for loop construct, but what does it hurt to declare it once
> and use it multiple times?

IMHO, doing that doesn't hurt so much, but it does hurt to be forced to
do it. I think it's cleaner to have a loop counter defined in the loop
instead of before, and I like to be able to do that.
OTOH, how does the proposed macro hurt?

> Imagine if you had, instead of an integer being used in multiple for
> loops, some more complex object being used in multiple blocks of code
> within one function. The overhead of constructing the object multiple
> times now can outweigh the desired practice of declaring it within
> each block.

Constructing an object is often less or at least not more expensive than
assigning to it. I would expect an iterator to take the same time for
assigning to it as for constructing it.

> Personally, I prefer to declare i within each loop construct, and then
> change that specific piece of code when I port to VC++.

I know it's sometimes necessary to do some changes to conforming code to
make it compile with a specific compiler, but that's not one of those
places. I wouldn't like to do such a change in multiple places if I can
istead just add a single line to make them all work at once without
changing them.

> If I forget to change it, the compiler tells me.
>
> Provided that you do not rely on the value of i between or at the
> start of any block, but instead assign it a value in the initializer
> portion of each for loop, I fail to see where any harm can come from
> declaring it once and using it multiple times. (And in the bad old
> days when I needed every byte of memory I could get my grubby little
> hands on, it was practically a *requirement* that I do it that way!)

You could say the same about the "bad old days" when you needed to count
clock cycles.



Relevant Pages

  • Re: non blocking read()
    ... > multiple of the read size? ... Arguably, there is no longer any point to checking the length of b in this version, since at worst you will break on the next iteration of the loop. ... It doesn't hurt, though. ...
    (comp.lang.python)
  • RE: Beginner VBA help with multiple tables
    ... I also need help with looping multiple tables. ... Dim rsEmployees As DAO.Recordset ... add a record to the Hours recordset for each employee. ... conditional statements to further control your code within the loop, ...
    (microsoft.public.access.modulesdaovba)
  • Re: String concatenation performance
    ... > into one string for use over multiple lines of code. ... Running the sample many times via an internal loop. ... in a container and that container is using system resources along with the ...
    (comp.lang.javascript)
  • Re: Multiple Malfunction
    ... > as part of my problem with CoolWebSearch: ... What I would do is run Ad-Aware multiple times until ... Folder Options, View and check Show Hidden Files and Folders. ...
    (microsoft.public.windowsxp.perform_maintain)
  • Re: Help. I am brand new to Excel but need to quickly come up with a formula to do the following:
    ... One generally doesn't loop in XL - either use functions that loop ... internally, or use multiple cells, e.g.: ... > Multiply the Yearly Amount times the number of years to get a Total ... > Input an inflation Rate, let's say 5%* or call it IR ...
    (microsoft.public.excel)