Talk:Scope (computer science)

From Wikipedia, the free encyclopedia
Jump to: navigation, search
WikiProject Computing / CompSci (Rated C-class, High-importance)
WikiProject icon This article is within the scope of WikiProject Computing, a collaborative effort to improve the coverage of computers, computing, and information technology on Wikipedia. If you would like to participate, please visit the project page, where you can join the discussion and see a list of open tasks.
C-Class article C  This article has been rated as C-Class on the project's quality scale.
 High  This article has been rated as High-importance on the project's importance scale.
Taskforce icon
This article is supported by WikiProject Computer science (marked as High-importance).
 
WikiProject Computer science (Rated C-class, High-importance)
WikiProject icon This article is within the scope of WikiProject Computer science, a collaborative effort to improve the coverage of Computer science related articles on Wikipedia. If you would like to participate, please visit the project page, where you can join the discussion and see a list of open tasks.
C-Class article C  This article has been rated as C-Class on the project's quality scale.
 High  This article has been rated as High-importance on the project's importance scale.
 

Misleading lead text.[edit]

In my experience, the concept of "scope" pertains exclusively to identifiers and declarations; and indeed, most of the article is only about scope insofar as it pertains to identifiers and declarations. (Though, maybe that's not saying much: most of the article is only about variable-names, and obviously lots of other things can also have scope.) The lead text, however, seems to downplay that aspect of scope; the word "identifier" hardly appears, and instead we get text about "values and expressions", and about how scopes can "contain statements and/or expressions which define an executable algorithm or part thereof" and can "nest or be nested" (which I think are really supposed to be statements about blocks, in block-structured languages, rather than about scopes per se).

So, I think the lead text should be completely rewritten.

Before I proceed — does anyone disagree?

RuakhTALK 04:36, 1 February 2012 (UTC)

Yes check.svg DoneRuakhTALK 15:20, 2 February 2012 (UTC)

"variables in C can be accessed outside their scope"[edit]

Diego Moya (talk · contribs) removed the statement that

> Outside of its scope, a declaration or identifier effectively does not exist.

describing it as a "false claim" because "variables in C can be accessed outside their scope".

I think (s)he's mistaken, but edit-summaries aren't the place to carry out this argument, so: Diego Moya, can you explain what you mean? For example, can you give an example of valid C code where a variable-name is accessed outside of its scope?

RuakhTALK 16:12, 23 February 2012 (UTC)

As I further edited, some compilers in some languages allow the programmer to access the variable outside their official scope and the result will be compiler-dependent and not portable. For example, a loop variable has only a language-defined value inside the loop, but in some compilers and languages (I don't remember if this was Turbo C++ or Turbo Pascal) you can still access its value outside the loop block after it terminated. Given that the behaviour is undefined, the result is outside the language official semantics, but some programs will still be coded in this way and have its running behavior dependent of this "undocumented feature". Diego (talk) 17:55, 23 February 2012 (UTC)
I disagree with your assessment. I think it would be more accurate to say that in some compilers and languages, a variable-name has a wider scope than is immediately obvious, or than is provided for by the language specification. —RuakhTALK 18:03, 23 February 2012 (UTC)
No, the scope of the variable is defined by the language semantics, not the compiler implementation (this is why we say the behaviour is "undefined", not "defined by the compiler"). In this compiler the variable is used outside the scope.
In C in particular, you can also access the memory space of a local variable through a pointer after the scope has finished. This is another instance of the variable "existing outside its scope".
Your version of the lead is better than the previous existing one; but I wanted to first 1) clarify this point, and 2) avoid that the scope definition mainly depends on the definition of "bound", which it was before. Explaining what happens to the variable when it's outside its scope is a good way to understand what happens when it's inside it. Diego (talk) 18:15, 23 February 2012 (UTC)
Re: first paragraph: I think you're talking out of both sides of your mouth. Either what's relevant is the de jure language semantics, in which case the variable-name does not exist outside of its scope (because it doesn't exist outside its official scope), or what's relevant is the de facto language semantics implemented by the compiler, in which case the variable-name does not exist outside of its scope (because its scope is wider than its official scope). It's perfectly valid to discuss, and contrast, both of these, but we shouldn't try to blur them by making statements that refer partly to each and are true of neither. (And by the way, if you have any evidence that referring to a variable outside of its scope is "undefined behavior" in any language other than C and C++, I'd be glad to see it.)
Re: second paragraph: That's completely irrelevant to this discussion, which is about whether a declaration or identifier exists outside of its scope. The statement "the variable exists outside of its scope, and can be accessed via a pointer" is true for one sense of "variable", but the statement "the variable-name exists outside of its scope, and can be accessed via a pointer" would be false. The latter is what's relevant to a discussion of identifiers. (Don't get me wrong, I think the former is absolutely worth mentioning; but it doesn't justify removing a correct statement about identifiers.)
Re: third paragraph: I'm not the one who introduced the word "bound". I tried to avoid it initially when I rewrote the lead text, but when you restored it, I had to admit that your version was less awkward than "the context within which a[n …] identifier identifies what it identifies", so I left it be.
RuakhTALK 18:38, 23 February 2012 (UTC)
I've reviewed my edits, and with your comments now I see where you're coming from. A variable is actually comprised of two parts, a name and a data storage location. My sentences make sense if you think of the "storage" part, which still exists outside the scope of the "name" part.
I was confused by your use of "variable-*dash*-name", which is a non-standard way to write it and not used in the target article Variable (computer science); thus it isn't clear what its meaning is in the lead. I did not understand that you were referring only to the name in the rest of the paragraph; my sentences were referring to the variable as a whole, and the data storage location in particular. Note that when talking only about the variable name you're describing a particular form of identifier, so it's redundant with the other assertions about identifiers.
Now can we edit the lead so that distinction between the scope of the variable name and the access to the variable storage is clear? Diego (talk) 22:11, 23 February 2012 (UTC)
Re: your first paragraph: I still don't think your sentences made sense, because although the term "variable" is genuinely ambiguous between {name} and {name + storage}, the term "identifier" does not share that ambiguity. A variable's storage can indeed exist while its name is out of scope, but you used that fact to justify removing a sentence that didn't even mention variables! But if we're on the same page now, then I don't suppose it matters.
Re: first part of your second paragraph: I'm surprised that you find "variable-name" confusing, but by all means, we can change it to "variable name".
Re: "when talking only about the variable name you're describing a particular form of identifier": yes, exactly; re: "so it's redundant with the other assertions about identifiers": not at all. I was perfectly aware that variable-names are one kind of identifier, and therefore avoided such redundancy. Sometimes I made a statement about identifiers in general; sometimes I made a statement only about variable-names (when it didn't seem worth elaborating on how it applied to other identifiers); and sometimes I made a statement about variable-names, followed by a mention of "other identifiers".
Re: your last paragraph: yes, certainly, if you wish. I've never objected to that.
RuakhTALK 01:43, 24 February 2012 (UTC)

Understanding Lexical/Dynamic Scoping[edit]

So I came here following some link trying to find out what exactly the difference between Lexical/Dynamic Scoping is. I read through the page for a few minutes and actually still hadn't much of a clue. Then I went to this page: http://c2.com/cgi/wiki?DynamicScoping

It's very easy to understand from these two paragraphs:

"In lexical scoping (and if you're the interpreter), you search in the local function (the function which is running now), then you search in the function (or scope) in which that function was defined, then you search in the function (scope) in which that function was defined, and so forth. "Lexical" here refers to text, in that you can find out what variable is being referred to by looking at the nesting of scopes in the program text.

In dynamic scoping, by contrast, you search in the local function first, then you search in the function that called the local function, then you search in the function that called that function, and so on, up the call stack. "Dynamic" refers to change, in that the call stack can be different every time a given function is called, and so the function might hit different variables depending on where it is called from."

In contrast to this the text here goes:

"In lexical scoping (or lexical scope; also called static scoping or static scope), if a variable name's scope is a certain function, then its scope is the program text of the function definition: within that text, the variable name exists, and is bound to its variable, but outside that text, the variable name does not exist. By contrast, in dynamic scoping (or dynamic scope), if a variable name's scope is a certain function, then its scope is the time-period during which the function is executing: while the function is running, the variable name exists, and is bound to its variable, but after the function returns, the variable name does not exist. "

I don't know, but this doesn't make much sense to me. I'm trying to understand what it tries to express but it isn't easy, even though I know from the above explanation what the whole thing tries to express.

--Birgitschrange (talk) 10:55, 5 January 2013 (UTC)

Your information is very helpful to me. I've developed the habit, when consulting WP, of going right to the talk pages, due to the (in my amateur opinion) unclear expositions I often find in articles (I nevertheless have benefitted greatly from WP and am grateful to all those who have worked on it). Actually, I have the same problem on the internet in general - it's hard to find good explanations. The explanation you have here seems to draw a very clear distinction. ---lifeform (talk) 00:43, 6 December 2014 (UTC)

Omission of Basic programming language[edit]

I was hoping to find references to the history of variable scoping in the Basic programming language. Was Microsoft Visual Basic the first to implement this feature/concept? Linhartr22 (talk) 16:51, 21 July 2013 (UTC)


Wrong reference to code[edit]

File scope ends with a reference to C code that does not exist in the text. However, perhaps the author was referring to the Python code above (because of the function name)? — Preceding unsigned comment added by Polariseke (talkcontribs) 04:24, 1 December 2014 (UTC)