Log in / create account | Login with OpenID
DocForge
An Open Wiki For Software Developers

Scope

From DocForge

Scope is, in its simplest terms, the ability of a programming language to hide or prohibit access to memory on condition of the origin of the access. This prohibiting factor to memory can prohibit access to functions, variables, packages, and classes. This is used as a tool to either enforce contractual design philosophy or to obfuscate proprietary libraries, as well as to force the correct usage of classes and libraries. Scope is often associated with namespace.

Object Scope [edit]

All data inside of an object has scope. The scope is usually limited to being public, private, or protected. Any data in the public scope is said to be universally accessible and therefore universally modifiable. Any data in the private scope can only be accessed and modified by code being run from the same scope. Protected scope is a special kind of scope that prohibits access to all code except those in the same package. The use of scope to enforce contract programming is extremely prevalent and extremely useful. Most of the time it is enforced in what are known as "getters and setters," or functions that serve only to either get a reference or copy of data, or to set internal data to something else. For example:

import java.Math;
 
class Foo {
    public int my_foo=0;
    public float do_something() {
        return Math.sqrt(my_foo);
    }
}
class Bar {
    public static void main(String[] args) {
        Foo example=new Foo();
        example.my_foo=-1;
        System.out.println(example.do_something());
    }
}

Obviously there is a problem! In Java you cannot take the square root of negative one! Consider this next example, which uses scope and contract programming through getters and setters to prevent the programmer from misusing other code.

import java.Math;
 
class Foo {
    private int my_foo=0;
    public void setMy_foo(int my_foo) {
        if(my_foo<1)
            throw new IllegalArgumentException("error - bad data!");
        else
            this.my_foo=my_foo;
    }
    public int getMy_foo() {
        return my_foo;
    }
    public float do_something() {
        return Math.sqrt(my_foo);
    }
}
class Bar {
    public static void main(String[] args) {
        Foo example=new Foo();
        try {
            example.setMy_foo(-1);
        } catch ( Exception e ) {
            System.out.println("Caught an exception... I wonder which one it was...");
        }
        example.setMy_foo(4);
        System.out.println(example.do_something());
    }
}

As you can see, this new version makes the same mistake, but because the variable is set by a function, you can write code to throw exceptions where the bad data first enters the system, as opposed to trying to trace where it came from whenever you get exceptions thrown from where the real issue would arise. It also makes it easier to document, since documenting the preconditions in the setter function becomes very easy, much more so than hoping that the programmer bothers to read every note for every public variable!

Furthermore, because my_foo is private, the programmer is unable to modify it without going through the contract validator.

Scope in Memory [edit]

Scope is also used in garbage collection as a metaphor for when there are no more references to any particular location in memory. Since there are no more scoped references to it, it has no scope, therefore it is said to have fallen out of scope. Memory that has fallen out of scope is flagged to be collected by the garbage collector, otherwise it's just a memory leak.