lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


* David Kastrup:

>> http://www.wxwidgets.org/develop/standard.htm - "6. Don't declare
>> variables inside for() ".
>
> [...] or, even better, use different names for the variables in the
> different for loops (in particular, avoid mute variable names like i
> above) -- then you can declare them in the loop statement and don't
> pollute the outer name space with local loop variables.

Here's another one, which also gives the reason:

| Do not declare loop variables in a for loop.
| 
| 	for (int i = 0; i < 4; ++i) {  // do NOT do this
| 
| Although the C++ standard states that the variable i is considered to
| be defined inside the scope of the loop, some compilers (including
| Microsoft Visual C++) incorrectly consider the variable to be declared
| outside the loop. This causes problems if you have two for loops which
| use the same variable name as a loop index. For example, the following
| will not work in Visual C++:

| The easiest thing to do is to declare your loop index variable once
| outside of the loops:
|
| 	int i;  // this will work in any compiler
| 	for (i = 0; i < 4; ++i) printf("foo\n");
| 	for (i = 0; i < 4; ++i) printf("bar\n");

<http://www.isi.edu/nsnam/ns/codestyle.html>

The Visual C++ version which this guide is referencing is version 6,
which used to be really popular and is probably still used to maintain
legacy applications.  Therefore, I expect anti-for-scope style rules
to be somewhat popular in the industry (comparable to anti-generics
rules in the Java world).