Re: rant // OE scope \\ forked out of 500 Rosetta
- Posted by _tom (admin) Apr 10, 2017
- 1979 views
I have found an analogy that seems to work in explaining scope.
Water vs Computer Code
Space is "is where you write your source-code." The Interpreter is a computer program so this is the starting space for all programs. The Interpreter space contains built-in routines like print or rand. Each file in the standard library is also a space. The file where you type your program is another space. Useful programs are build from several spaces.
Scope is "where your code can do something because identifiers are visible." The general rule is that an identifier must be declared before it can be used in a statement. Before the declaration an identifier is not visible or out of scope; after a declaration an identifier is visible or in scope. There are rules to learn because scope can equal the available space or can be a small restricted area of space.
I will use the properties of water to explain the properties of space and scope. Space: a bottle to hold water; space: a file to hold identifiers. Scope: how water fills a bottle; scope: how identifiers fill a file.
A water bottles and files are alike; all bottles work work the same; all files work the same. The purpose of a bottle is to hold water; the purpose of a file is to hold identifiers. A bottle is water-tight; a file is identifer-tight. For more water you can get more bottles; for more identifiers you can get more files. You can pour water into a bottle (the water you have does not leak out); you can add identifiers from another file (identifiers you already have do not leak out). You can choose to pour water from a bottle (a controlled leak); you can choose to let identifiers leave a file (a controlled leak). Flooding a bottle fills it up to the top; some scopes (routines) can flood identifiers to the top a file. A waterfall fills only the bottom of a bottle; some scopes (variable) add identifiers only to the bottom of a file. It is possible to mix water from all bottles together; it is possible for global identifiers to apply to all files.
The analogy of water and identifiers is a start in understanding how identifiers work in source-code.
_tom