Take the 2-minute tour ×

Where is there a good description of the use of the word 'scope' in computer science?

In this post of a question about the Java final reserved word one responder implies that scope is textual so each iteration of a for loop causes a scope to be resumed as current. Another responder implies scope is time dependent so each iteration is a new scope. In those contexts, it was possible to understand what scope meant even though different people use the word differently but sometimes when I am reading a programming book I am not sure what scope means.

What are good qualifiers for the word 'scope'? I suggest 'textual' is one. Can you think of others? Should a qualifier almost always be used or is there a general understanding about contexts that help one perfectly infer the the intended meaning?

How is the word 'scope' used when speaking of objects (and object oriented programming) and what qualifiers might be necessary?

share|improve this question

migrated from stackoverflow.com Jul 31 '11 at 11:31

This question came from our site for professional and enthusiast programmers.

I don't think I know any language where "textual scopes" are inconsistent with "evaluation scopes". For instance, in Java, the execution "leaves the scope" when it has to evaluate the loop condition. –  zneak Jul 31 '11 at 6:21
What is the reason for the 'close it' vote, please? –  broiyan Jul 31 '11 at 6:26

5 Answers 5

For a variable to be "in scope" at a given location mean that you can write a snippet of code at that location that reference the value held by that variable.

This is not specific to objects but to the rules of the language.

How a variable should be treated before it is allowed to go out of scope varies depending on the language.

  • C require that memory allocated and stored in a pointer is returned to the memory pool before the pointer falls out of scope.
  • Java allows object references to fall out of scope and automatically be returned to the memory pool. If the object held resources needing explicitly to be free, this should be coded manually before the object falls out of scope.
  • Objective-C require the programmer to manually keep track of the number of uses for a given object, and when the object counter is zero the object is released.

So, scope just indicates "visibility" - where is the variable usable?

share|improve this answer

You could consider scope in the way it's physically implemented at the machine level. Pushing and popping from a stack. Once you're popped off the stack, you no longer exist (out of scope).

share|improve this answer

The scope of something (function, variable, macro, etc.) in a program is the range of code that it applies to. For instance, a variable can have "local-to-function" scope (local to a particular function; this would mean the variable can only be used inside the function), local to a class (variable only useable inside the class), or global (useable from anywhere). Depending on the programming language, there may be other scopes for variables or functions that are possible as well.

In some programming languages, special things happen when variables go in and out of scope. For example, memory may be allocated to hold data, or memory may be freed when variables go out of scope (since a variable cannot be used out-of-scope, there is no way to use it once it goes out of scope, so it's safe to recycle any memory it's holding1 when it goes out of scope). Scope is also useful as an error-checking (programmer-oriented) mechanism. If you define variables only within the scope where they are supposed to be used, many languages will give an error (often compile error) if there is an attempt to use the variables out-of-scope. This is true, for instance, in C, C++, and Java.

Scope can also avoid polluting the namespace. If you had a large program that involved 20,000 variables (totaling over all the different parts of the program) and they all had global scope, you could accidentally overwrite variables you didn't know you were using elsewhere or you could find it annoying to find a name you hadn't yet used elsewhere in the program for a temporary variable. Also, if you are using other people's code that you haven't read in its entirety, scoping becomes even more important. It's much better if names are localized to the places where they're used. If a name never leaves a function, the name should be scoped to the function; if it needs to leave a function but stay within a class, it should be scoped to the class, and so on.

Edit: To answer your question about qualifiers, AFAIK scope generally refers to a range of code. So there are your temporal and spatial qualifiers right there: the textual scope of a name/variable is the space of code in which a name is valid or the variable can be referred to by a particular name, and the temporal scope of a name/variable is the time during the program's runtime in which the code is in a place where the variable would be in textual scope.

1 Provided it does not share this memory with other objects and it knows that.

share|improve this answer

Scope is a useful concept that has a technical definition that depends upon the circumstances, such as the programming language (Java, PHP, ...), programming construct (for-loop, ...).

A broad definition is that scope defines a region, be it a section of code, such as within a class; or a period of a time such as the lifetime of a thread; that something is valid. This something is valid is most usually that a variable is defined or has a particular value; but it might be something more complex such as a particular application constraint is violated while it is being updated.

share|improve this answer

You'll find a decent article in wikipedia.

My reading of the answers you are referring to does not bring out a textual versus temporal distinction. It's more that the use of final needs to be understood in the context of a loop. The scope of the variable declared in a for loop is clearly limited to the "inside" of a loop. The less obvious thing is that the variable is effectively re-created for each loop iteration. The use of final suddenly makes that clear

     final int x = i;
share|improve this answer
"The scope of the variable declared in a for loop is clearly limited to the "inside" of a loop.", not in all languages! –  Matt Potts Nov 3 '11 at 17:29

Your Answer


