Search

The Online Encyclopedia and Dictionary

 
     
 

Encyclopedia

Dictionary

Quotes

 

Name binding

In computer science, binding is associating objects and implementations with names in programming language so that those objects and implementations can be accessed by the names. An object's names are said to be "bound" to them.

Deep binding and shallow bindings are not kinds of binding, but ways to implement binding.

The simplest example is defining subprograms:

def sum (x, y)
  x + y
end

sum (2, 3)

In this Ruby code, an implementation returning the sum of given two inputs is bound to a name, sum.

Contents

Binding time

Because objects in computer programs are usually resident in the computer memory, binding time is almost the same as the time of allocation of memory space for objects. Bind can be done either at compile-time or link-time (static binding) or at run-time (dynamic binding). Also, scope rules might define binding time of objects. Local variables, for example, are usually bound at run-time while global variables at compile-time.

For example,

static int n;
int main ()
{
  n = 12;
  return n;
}

In this C code, a global variable n is bound to certain location in the memory of the computer.

Dynamic binding for polymorphism

In object-oriented programming, an object can respond to the same message with a different implementation. Dynamic binding, also called dynamic dispatch is a common solution for this problem.

For example there may be an object that contains the name (data) 'Socrates', and which is of the class (or type) Person.

Now suppose all Persons are mortal. In object oriented programming, we can say that the Person class must implement the Mortal interface, which contains the method die().

Persons and Plants die in different ways, for example Plants don't stop breathing. 'Dynamic binding' is the practice of figuring out which method to invoke at runtime. For example, if we write

void kill(Mortal m) {
  m.die();
}

it is not clear whether m is a Person or a Plant, and thus whether Plant.die() or Person.die() should be invoked on the object. With dynamic binding, the m object is examined at runtime, and the method corresponding to its actual class is invoked. (This implies that the actual representation of an object in memory is just its data and doesn't include the methods.)

Deep binding v.s. shallow binding

In computer science, deep binding is the most common implementation of bindings. Each environment consists of variables names and values. The environments are linked according to the scoping rules. When a name is used in a procedure the environments are searched from the inner to the outer nesting level.


In computer science, shallow binding associates bindings for each variable and then searches the bindings associated with the variable to find the binding for the current environment. The major advantage is that it reduces the search time for variables that are bound in the global environment.

In shallow binding, lexical variables mentioned in anonymous subroutines are whichever variables with the same names happen to be in scope when the subroutine is called. In deep binding, they are the same ones that were in scope when the subroutine was created.

The above contains the text from perlfaq7

See also

The contents of this article are licensed from Wikipedia.org under the GNU Free Documentation License. How to see transparent copy