Re: rant // OE scope \\ forked out of 500 Rosetta

new topic     » goto parent     » topic index » view thread      » older message » newer message
DerekParnell said...
_tom said...

I have found an analogy that seems to work in explaining scope.

...

The analogy of water and identifiers is a start in understanding how identifiers work in source-code.

Sorry Tom, but my mind must work differently. I read this many times and each time I got more and more confused about the topic. And I KNOW identifiers! Also, I have some fundamental disagreements about the assertions mentioned in the analogy.

If the analogy helps someone then good, but it wouldn't be much use for me.

The analogy does need more work. The reason for you "confusion" is that you are a programmer of long standing, and I am trying to create an explanation for new non-programmers--and not doing it well.


scope rant, improved


A computer program is written as a plain text file.

Fig. 1 : your program file myprog.ex

 ┌╴╴╴╴╴╴╴╴┐ 
 │        │ 
 └╶╴╴╴╴╴╴╴┘ 

Inside the file is a space where you type your source-code.

Since this is a Euphoria program, your program file (and its source-code space) is part of a larger Euphoria space that belongs to the Interpreter. Euphoria space gives you built-in procedures and functions; the built-ins work all the time anywhere you type.

Fig. 2 : myprog.ex inside Euphoria space

 ┌╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴┐ Interpreter space 
 │                       │    
 │ ┌╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴┐    │ 
 │ │  Program space │    │      
 │ └╶╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴┘    │ 
 │                       │ 
 └╶╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴┘ 

The basic rule in Euphoria is "declare all identifiers before using them in a statement; declare variables, procedures, functions, types before you need to use them." If you follow this basic rule you will not get into too much trouble.

Scope is where, in space, you can use an identifier in a statement. So, if you follow the basic rule of declaring everything before using you don't have think about scope very much, things just work.

  • In-scope things work.
  • Out-of-scope things do not work.

The scope of a variable is from the declaration to the end of space.

Fig. 3 : scope for a variable

 ┌╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴┐ 
 │                    │     out-of-scope 
 │                    │ 
 │ atom x = 2.4       │ <-- variable declaration 
 │ ░░░░░░░░░░░░░░░░░░ │ 
 │ ░░░░░░░░░░░░░░░░░░ │     in-scope 
 │ ░░░░░░░░░░░░░░░░░░ │ 
 │ ░░░░░░░░░░░░░░░░░░ │ 
 │ ░░░░░░░░░░░░░░░░░░ │ 
 │ ░░░░░░░░░░░░░░░░░░ │ 
 └╶╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴┘ 

Variable scope is like a waterfall, space from the declaration to the end of space is in-scope.

The scope of a routine (procedure, function, type) is special. The entire space, including inside the routine, is in-scope. Routine scope is like a flood filling all the available space; an identifier for a routine is in-scope for the entire file. If you follow the basic rule declare everything before using to don't have to think about how routine scope; it just works.

Fig. 4 : scope for a routine

 ┌╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴┐ 
 │ ░░░░░░░░░░░░░░░░░░ │ 
 │ ░░░░░░░░░░░░░░░░░░ │ in-scope 
 │ ░░░░░░░░░░░░░░░░░░ │ 
 │ ░░░░░░░░░░░░░░░░░░ │ 
 │ procedure foo()    │ <-- routine declaration 
 │ ░░░░░░░░░░░░░░░░░░ │ 
 │ ░░░░░░░░░░░░░░░░░░ │ in-scope 
 │ end procedure      │ 
 │ ░░░░░░░░░░░░░░░░░░ │ 
 │ ░░░░░░░░░░░░░░░░░░ │ in-scope 
 │ ░░░░░░░░░░░░░░░░░░ │ 
 │ ░░░░░░░░░░░░░░░░░░ │ 
 │ ░░░░░░░░░░░░░░░░░░ │ 
 │ ░░░░░░░░░░░░░░░░░░ │ 
 └╶╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴╴┘ 

Why is routine scope special?

Calling a routine from anywhere in a file space is great because:

  • one routine can call another routine (even if it declared later in the program)
  • a routine can call itself (which allows recursive programming)

A "waterfall" naturally starts at the top of the cliff and drops to the bottom. Variable scope is the same, it starts at the point of declaration and drops to the bottom of you programming space.

In a "flood" water is everywhere. Routine scope (procedure, function, type) "fills" ("floods") the entire programming space.

As a sailer, this makes perfect sense to me. I have tested waterfall and flood conditions using a dingy.

_tom

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu