aboutsummaryrefslogtreecommitdiff
path: root/docs/notes/scoping_and_hiding.txt
blob: a4837f7c27f2cd9f75c63413743da5355d8eb892 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53


scoping and hiding
------------------

Maybe we should make each each statement a subscope of previous
statements.

This allows hiding of identifiers if they are repeated, which is
IMO useful, but it should probably only be allowed if it doesn't
change the meaning of the code and doesn't create useless
identifiers. For example:

    int a = 5; // useless => NOT OK
    int a = 3;
    
    int a = 0;
    while (a < 10) work();
    int a = 3;               // OK
    while (a < 10) work();
    
    int a = 0;
    while (a < 10) work();
    float a;  // NOT OK
    while (a < 10) work();


The same rules should IMO apply for nested scopes inside functions:

    () f(bool b)
    {
        int i;
        double d = 1;
        
        if (b) {
            int i; // NOT OK, definition of outer i was useless up to the "if"
                   // (should have been done after the "if")
            int j;
            
            int d; // NOT OK: changes meaning of code!
            
            // Variables are used
            i = j = 0;
            g(i, j);
        }
        
        int j = 1; // OK
        
        // Variables are used
        g(i, j);
    }