Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > variable scope in for loop

Reply
Thread Tools

variable scope in for loop

 
 
Rolf Magnus
Guest
Posts: n/a
 
      01-06-2004
Pete Becker wrote:

> Rolf Magnus wrote:
>>
>> You consider this less intrusive? You have to use that workaround in
>> every function that has more than one for loop

>
> It's not a workaround. It's a way of writing portable code when you
> plan to target a compiler that doesn't support for loop scoping.


I'd not be willing to use that "way of writing code" if I don't need to.
Well, actually, I'm not using VC++ myself, but if anyone wants to port
my code to it, he either needs to change all the for loops (since I
always define the counter in the for scope, which looks cleaner to me),
or add the macro.

>> So you wouldn't use that macro just for the sole sake of avoiding
>> macros? Or do you have any real technical reason?

>
> Neither. Apparently you don't grasp humor.


English is not my native tongue, so I might have difficulties to grasp
your humor. Anyway, what's your point then?

 
Reply With Quote
 
 
 
 
Howard
Guest
Posts: n/a
 
      01-07-2004

"Rolf Magnus" <(E-Mail Removed)> wrote in message
news:btfgsn$e7n$04$(E-Mail Removed)-online.com...
> Pete Becker wrote:
>
> > Rolf Magnus wrote:
> >>
> >> You consider this less intrusive? You have to use that workaround in
> >> every function that has more than one for loop

> >
> > It's not a workaround. It's a way of writing portable code when you
> > plan to target a compiler that doesn't support for loop scoping.

>
> I'd not be willing to use that "way of writing code" if I don't need to.
> Well, actually, I'm not using VC++ myself, but if anyone wants to port
> my code to it, he either needs to change all the for loops (since I
> always define the counter in the for scope, which looks cleaner to me),
> or add the macro.
>
> >> So you wouldn't use that macro just for the sole sake of avoiding
> >> macros? Or do you have any real technical reason?

> >
> > Neither. Apparently you don't grasp humor.

>
> English is not my native tongue, so I might have difficulties to grasp
> your humor. Anyway, what's your point then?
>


Just an old expression used as a joke. Originally it was "look, ma, no
hands"...used when doing something like riding your bicycle without using
your hands.

I think I agree with him that I'd just as soon not use macros. No real
rechnical reason, I just don't like using them, I guess because you have to
go look up what they do, try to figure out what the result of them is after
parsing, and then they can be changed on you and you have to find everywhere
they're used and be sure those places aren't broken. Just a pain in the
arse if you ask me.

Personally, I do it like you and write them according to the standard, with
i declared within each for loop. Then, when I port to VC++ 6.0 (which I
often do), I change the code that fails to compile and use a single
declaration for i before the first loop.

But I never use macros. (Templates are a pain in the ass to figure out
sometimes, too, but their power is to great to ignore! )

-Howard




 
Reply With Quote
 
 
 
 
Jack Klein
Guest
Posts: n/a
 
      01-07-2004
On Tue, 06 Jan 2004 14:41:03 -0500, Jeff Schwab <(E-Mail Removed)>
wrote in comp.lang.c++:

> Pete Becker wrote:
> > Rolf Magnus wrote:
> >
> >>Wagner Bruna wrote:
> >>
> >>
> >>>Hello,
> >>>
> >>>(E-Mail Removed) (Wenjie) wrote in message
> >>>news:<(E-Mail Removed) le.com>...
> >>>
> >>>>We had a code review with the argument of
> >>>>whether "i" is out of scope as illustrated
> >>>>below:
> >>>>for (int i=0; i<2004; i++)
> >>>>{
> >>>> doSomething(i);
> >>>>}
> >>>>
> >>>>for (i=0; i<2004; i++)
> >>>>{
> >>>> doSomethingElse(i);
> >>>>}
> >>>>
> >>>>Is it correct according to C++ language?
> >>>>VC++6.0 works fine with above code.
> >>>
> >>>No, it's not correct. The scope of i ends at the end of the first
> >>>loop.
> >>>
> >>>If you need VC++6 portability, a good workaround IMHO is to enclosure
> >>>the loops inside artificial blocks:
> >>>
> >>>{
> >>> for (int i=0; i<2004; i++) {
> >>> doSomething(i);
> >>> }
> >>>}
> >>>
> >>>{
> >>> for (int i=0; i<2004; i++) {
> >>> doSomethingElse(i);
> >>> }
> >>>}
> >>
> >>IIRC, another, less intrusive workaround is to
> >>
> >>#define for if(false); else for

> >
> >
> > An even less intrusive workaround is to move the variable declaration
> > outside the loop:
> >
> > int i;
> > for (i=0; i<2004; i++)
> > {
> > doSomething(i);
> > }
> >
> > for (i=0; i<2004; i++)
> > {
> > doSomethingElse(i);
> > }
> >
> > Look, ma, no macros.
> >

>
> I certainly prefer your macroless approach, but why not initialize i
> where it is defined, and save an assignment?
>
> int i = 0;
>
> for( ; i < 2004; ++i )
> do_something( i );
>
> for( i = 0; i < 2004; ++i )
> do_something_else( i );


Because somebody maintaining your code some day, somebody else or even
you, will rearrange the loops or stick another one or another use for
'i' between the definition with initialization and the first for loop.
Then a perhaps strange and hard-to-find defect will be born.

The loop should perform its own initialization.

> In production code, I tend to prefer while-loops to for-loops anyway, so
> it would look something like this:
>
> int i = 0;
>
> while( i < 2004 )
> {
> do_something( i );
> ++i
> }
>
> i = 0;
>
> while( i < 2004 )
> {
> do_something_else( i );
> ++i;
> }
>
> I know folks are religious about for-loops; please don't be offended if
> I don't respond to criticisms about keeping loop control at the top of
> the block.


Nothing against while loops, I use them myself quite frequently, or
even do ... while loops, more often than most.

But I don't like an important condition of the loop separated from the
control statement itself, both for conceptual reasons and also for the
above mentioned maintenance problem. If someone, someday, puts
something between the initialization or the assignment and the
corresponding loop, bang!

In your particular example, I would use the for loop because it allows
setting the initial value of the control variable, a necessary
requirement of the loop, to appear in the loop control logic.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      01-07-2004
On Tue, 06 Jan 2004 16:32:01 -0500, Jeff Schwab <(E-Mail Removed)>
wrote in comp.lang.c++:

> Andrey Tarasevich wrote:
> > Jeff Schwab wrote:
> >
> >>...
> >>I think initialized variables properly does more for readability than
> >>making blocks of code look alike. Just my opinion, though.
> >>...

> >
> >
> > In my opinion, separating initialization of loop variable from the loop
> > itself might create more problems than delayed intialization of a variable.
> >

>
> For instance?


See my reply up-thread.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
 
Reply With Quote
 
Gary Labowitz
Guest
Posts: n/a
 
      01-07-2004
"Jack Klein" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
<<SNIP>>
> Nothing against while loops, I use them myself quite frequently, or
> even do ... while loops, more often than most.
>
> But I don't like an important condition of the loop separated from the
> control statement itself, both for conceptual reasons and also for the
> above mentioned maintenance problem. If someone, someday, puts
> something between the initialization or the assignment and the
> corresponding loop, bang!
>
> In your particular example, I would use the for loop because it allows
> setting the initial value of the control variable, a necessary
> requirement of the loop, to appear in the loop control logic.


We fought this problem of loop control for years in various shops and the
only bulletproof method we ever decided on was to use differently named loop
control variables in any block. While i, j, k seem traditional, presumably
left over from FORTRAN, it even makes for self-documenting code to use names
that mean something in the process being coded. Loop control variables like
addressVectorItem for referencing an address in a Vector of addresses I feel
is better than just i. Long, fluffy names, that's the ticket!
--
Gary


 
Reply With Quote
 
E. Mark Ping
Guest
Posts: n/a
 
      01-07-2004
In article <3ffae75c$0$32327$(E-Mail Removed)> ,
Ron Natalie <(E-Mail Removed)> wrote:
>
>"Pete Becker" <(E-Mail Removed)> wrote in message news:(E-Mail Removed)...
>> > #define for if(false); else for

>>
>> An even less intrusive workaround is to move the variable declaration
>> outside the loop:
>>
>> int i;
>>

>Actually, I find that MORE intrusive. You have to put sloppy coding
>practices in your code to correct for defects in the compiler.


You're saying that declaring the variable outside the loop is more
intrusive than using a macro to effectively redefine a language
keyword?

I find this baffling.
--
Mark Ping
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      01-07-2004

"E. Mark Ping" <(E-Mail Removed)> wrote in message news:bthq7l$2003$(E-Mail Removed)...

> >Actually, I find that MORE intrusive. You have to put sloppy coding
> >practices in your code to correct for defects in the compiler.

>
> You're saying that declaring the variable outside the loop is more
> intrusive than using a macro to effectively redefine a language
> keyword?
>
> I find this baffling.


I don't. The only thing I'm "redefining" here is making the keyword
behave like the language standard says it should. I pointed out that
it is technically wrong, but so is Microsoft, and I'd rather have standard
compliant code rather than Microsoft compliant code, we're portable
to platforms other than Windows.

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 not recommending the willy nilly redefining of keywords, but in
this case making the tested, controlled, one line change IS LESS
INTRUSIVE, than having to break standard-conforming code everywhere
else it appears OR adding bad programming practices because the
compiler is busted.

 
Reply With Quote
 
Howard
Guest
Posts: n/a
 
      01-07-2004

"Ron Natalie" <(E-Mail Removed)> wrote in message
news:3ffc3814$0$31842$(E-Mail Removed) m...
>
>
> 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?

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.

Personally, I prefer to declare i within each loop construct, and then
change that specific piece of code when I port to VC++. 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!)

-Howard



 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      01-07-2004

"Howard" <(E-Mail Removed)> wrote in message news:bthvop$(E-Mail Removed)...

>
> 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?


Because why permit it to be inadvertantly used in expressions in between
the unrelated loops you're stuck using it in. Terribly non-structured.

> 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


If it were not something that made sense to be shared, it should be destroyed.
But you're inventing issues, by default variables should be defined in the smallest
scope that encompasses their use. THIS IS JUST PLAIN GOOD C++ DESIGN.
Having to thwart that because Microsnot can't get it right isn't a good reason
to chage the design rules.

> Personally, I prefer to declare i within each loop construct, and then
> change that specific piece of code when I port to VC++. If I forget to
> change it, the compiler tells me.


If you do what I suggested, you don't have to go bastardizing your code
just because you have the misfortune of using Microsnot's compiler.

 
Reply With Quote
 
Howard
Guest
Posts: n/a
 
      01-07-2004

Microsnot? I'm telling Bill on you!





 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
Scope of a variable declared in for loop vaysagekv C Programming 56 02-25-2010 09:13 PM
Having trouble understanding function scope and variable scope Andrew Falanga Javascript 2 11-22-2008 09:23 PM
extra constructor calls, inefficent loop variable scope? ixtahdoom Perl 1 07-09-2004 12:34 AM
How do I scope a variable if the variable name contains a variable? David Filmer Perl Misc 19 05-21-2004 03:55 PM



Advertisments