J
James Kuyper
Nick Keighley wrote:
....
That doesn't fit conventional usage of the term. Consider a typical
usage: "This variable has a value of 3." While the binding is highly
relevant to the definition of the term 'variable', it is not what that
term directly refers to. Neither does it refer to the identifier that is
bound. The term variable refers to the object that is bound to the
identifier.
In much simpler terms: a variable is a named object.
....
That also does not fit conventional usage. You've taken a value judgment
about global variables, and converted that judgment into the definition.
Consider:
char* aaah(int n, char s[])
{
int i;
for(i=0; i<n; i++)
s = 'a';
return s+n;
}
According to your definition, 'i' is a global variable, because it is
visible at the return statement, which is not a place where it is
necessary for it to be visible. The function could have been re-written
to use:
for(int i=0; i<n; i++)
Personally, I like this C99 feature, and favor its use in any code that
is not required to be C90-compatible. However, that's a value judgment,
and shouldn't have anything to do with the definition of 'global variable'.
On the flip side, your definition could arguably be used to deny the
label 'global variable' to a variable with file scope and external
linkage, if the global visibility of that variable is in fact necessary.
Obviously, if you insist on the dogma that it can never be justified, no
such possibility exists. While I think that global variables are almost
always design errors, I insist on the word "almost" - there are exceptions.
In any event, the definition of whether or not something is a global
variable should not depend upon individual value judgments about the
merits of global visibility.
My own definition of 'global variable' in a C context is
"a variable whose identifier has file scope and external linkage"
I don't think file scope gives a variable sufficient visibility to
justify the the adjective 'global'. I don't think the fact that such
variables can be hidden by a local declaration is sufficient
justification to deny them that label.
As I've defined them, C has variables and in particular global
variables, even though the C standard itself provides no definitions for
either of those terms. That's no different from the fact that C has a
grammar, even though the C standard never defines the term 'grammar'.
The C standard doesn't have to be the source of the definition of every
term that it is useful to use while describing C.
....
Def:
a variable is a binding between an identifier and an area of storage.
That doesn't fit conventional usage of the term. Consider a typical
usage: "This variable has a value of 3." While the binding is highly
relevant to the definition of the term 'variable', it is not what that
term directly refers to. Neither does it refer to the identifier that is
bound. The term variable refers to the object that is bound to the
identifier.
In much simpler terms: a variable is a named object.
....
Def:
global variable is an identifier with an unnecessarily large
visibility
That also does not fit conventional usage. You've taken a value judgment
about global variables, and converted that judgment into the definition.
Consider:
char* aaah(int n, char s[])
{
int i;
for(i=0; i<n; i++)
s = 'a';
return s+n;
}
According to your definition, 'i' is a global variable, because it is
visible at the return statement, which is not a place where it is
necessary for it to be visible. The function could have been re-written
to use:
for(int i=0; i<n; i++)
Personally, I like this C99 feature, and favor its use in any code that
is not required to be C90-compatible. However, that's a value judgment,
and shouldn't have anything to do with the definition of 'global variable'.
On the flip side, your definition could arguably be used to deny the
label 'global variable' to a variable with file scope and external
linkage, if the global visibility of that variable is in fact necessary.
Obviously, if you insist on the dogma that it can never be justified, no
such possibility exists. While I think that global variables are almost
always design errors, I insist on the word "almost" - there are exceptions.
In any event, the definition of whether or not something is a global
variable should not depend upon individual value judgments about the
merits of global visibility.
My own definition of 'global variable' in a C context is
"a variable whose identifier has file scope and external linkage"
I don't think file scope gives a variable sufficient visibility to
justify the the adjective 'global'. I don't think the fact that such
variables can be hidden by a local declaration is sufficient
justification to deny them that label.
As I've defined them, C has variables and in particular global
variables, even though the C standard itself provides no definitions for
either of those terms. That's no different from the fact that C has a
grammar, even though the C standard never defines the term 'grammar'.
The C standard doesn't have to be the source of the definition of every
term that it is useful to use while describing C.