Kenneth Brody said:
Kenneth Brody said:
On 6/19/2012 4:13 PM, BartC wrote: [...]
I'm sorry if no-one else can see the problems (duplicate declarations,
clutter, separating related variables, scattered declarations, trickier
editing because you have to keep moving the declarations around,
worrying
about scope, shadowing, anonymity, etc).
Ahh... So you want all declarations to be global scope, always?
No. Function scope, file scope, and project-wide.
So this:
void foo(void)
{
int i;
...
}
should (in your opinion) create a variable "i" of type integer with a
scope of the braces that encloses the declaration (ie: the function
"foo"), but:
void foo(void)
{
...
if ( bar )
{
int i;
...
}
...
}
should behave differently, and give "i" some scope other than the braces
that enclose the declaration?
Until a few days ago, I'd never even considered that variables could be
declared local to a block. You second example was, to me, just a convenient
way of declaring 'i' near the place where you started using it, but would
have exactly the same scope as the ones at function level (ie. declared at
the top of the function).
Now it seems the 'i' is limited to it's immediately enclosing braces, which
doesn't appear so useful. Suppose it's an if-else statement, both branches
having code that uses a variable 'i' of the same type; why bother declaring
two distinct versions of it?
[...]
No-one has said anything of the kind. Scope would still exist across
modules,
across one module, and across one function. I'm saying it doesn't need to
be
across individual blocks too, given that a typical function is small.
if ( foo.type == MyInteger )
{
int num;
num = foo.integer;
... do stuff with int ...
}
else if ( foo.type == MyFloat )
{
float num;
num = foo.floating;
... do stuff with float ...
}
I accept that it does allow you to do stuff like this, but it really doesn't
seem a very good idea, using the same identifier for two different types,
within a few lines of each other in the same function.
Imagine you're trying to read such a function, and your eyes jump from one
part of the function to another. First num is a int; fine. Now you jump to
the 'do stuff with float' part, and you have to do a double take. From this
point on, you can't trust anything you see; what type is 'num' going to have
next time you see it? Maybe these are two nested blocks in a loop, and you
break from one to the other. You have to trace the code, character by
character counting blocks, to match the use of 'num' with it's correct
declaration.
Sorry, but these possibilities which everyone else thinks are great, to me
just seem nightmarish.
[...]
We're talking about adding extra block scopes to a 10- or 20-line
function
that already has local variables valid throughout the function.
How small a scope do you need: per line, per statement, or per
expression?
Why should the scope of a variable declared inside the outermost braces of
a function be different from a variable within some other set of braces?
What's difficult about the concept that the scope is the innermost set of
braces that enclose the declaration?
What is a set of braces? C uses braces to:
- Enclose the statements of a function body (which can't have just one
non-block statement).
- Enclose an initialiser expression or list
- Group several ordinary statements into one. C statements such as while,
for and if only allow one statement in a loop body or each branch of the
'if', so {, } are a mechanism for allowing more than one.
The syntax could have used entirely different symbols for these
requirements. Having a local scope for a function body, that's fine; every
language except Basic has that. But what's so special about a compound
statement that it ought to have it's own private set of variables?
On just about every other occasion where the complexity of a function grows
beyond about 3 lines, I'm constantly told it needs to split into
sub-functions. Now entire, self-contained sub-programs can be created in
each branch of a minor 'if' in a minor 'for' loop in a dusty corner of a
function, and that is perfectly OK?
In a different kind of forum from comp.lang.c, I would almost start to
suspect a wind-up!