On Sat, May 17, 2014 at 11:14 PM, Hans W Borchers <[email protected]> wrote:
> and it took me some time before I realized that the answer came from outside
> the function.
> That behavior can really lead to very difficult testing situations.
Fair enough. I suspect there can be some subtle bugs which occur with
the more relaxed scoping as well, though I can't think of a good
example right now.
> I don't know whether something like this can happen in C/C++.
It can, though this particular case is not at all common if you avoid
globals with short names. A more insidious problem which does happen
occasionally is something like the following:
int i = 0;
for (int j = 0; j < 10; ++j)
{
if (x[j] = 0)
{
int i = j; // Oops. Should have been "i = j"
break;
}
}
Here a separate variable i is declared inside the inner loop, so the
one outside doesn't get modified. In a real case there would
obviously be more code around the problem making it hard to find, and
the error would probably creep in after carelessly copying some code
between scopes.
~Chris