Assignment is an operator that is ubiquitous in imperative programming languages such as C or Java.

Assignment is a data movement operator. That is, it moves data (value) from one variable to another. For example, let us say that we have two variables A and B, with values 1 and 2 repectively. That is:

    ---------+-------
    Variable | Value
    ---------+-------
         A   |   1
    ---------+-------
         B   |   2
    ---------+-------

the assigment statement "A := B" (*) will have the following effect:

    ---------+-------
    Variable | Value
    ---------+-------
         A   |   2
    ---------+-------
         B   |   2
    ---------+-------

As you can see, A now has the value that B previously had, and B retains its old value. That is, A receives a copy of the value of B.

Assignment is ubiquitous partially because of its direct mapping into how to make machines transfer data from one memory cell into another. However this copying semantics shows some problems when the data stored in a variable is more than just a simple data type.

For example, if A and B were large matrices instead of simple integers, the assigment statement would have to copy the whole matrix, element by element. This clearly is inefficient. The solution to this problem is usually to use assigment to copy pointers or references instead. Now, instead of copying the whole matrix, we need to copy only a pointer to the memory location where the matrix resides.

The gain in efficiency, however, causes another problem: aliasing. Using reference smeantics, the statement A := B will make A and B refer to the same data. Thus when the data A refers to is changed, B will also change. This can cause unexpected problems.

Say for example, A and B are linked lists, and we call a procedure append(A,B) which will append the list B to the end of list A. What happens when A and B refer to the same list?

There are many ways in which these problems are addressed. One way is to simply be careful with efficiency when copying values, and careful with reasoning when copying references.

Another option is to eliminate assignments totally! Some logic and functional languages do this, like Haskell. Others, like Lisp have assignment but its use is generally discouraged.

A novel way of dealing with the problem is to replace assignment with swapping, such that in our original example, the effect of a swapping operation will be:

    ---------+-------
    Variable | Value
    ---------+-------
         A   |   2
    ---------+-------
         B   |   1
    ---------+-------

Since swapping exchanges the values of A and B, it can be efficient for large data structures (internally implemented as swapping references, instead of copying every value). And since swapping does not have the effect of making two variables refer to the same value, it cannot have the problem of aliasing.


(*) C and Java use the symbol "=" as the assignment operator, but I've chosen to use ":=" as the former may be mistaken as the equality operator.
Reference and Further Reading:
Bruce W. Weide, Scott M. Pike, and Wayne D. Heym, Why Swapping?, Proceedings of the RESOLVE 2002 Workshop, online version available at: http://people.cs.vt.edu/~edwards/RESOLVE2002/proceedings/Weide2.html

As*sign"ment (#), n. [LL. assignamentum: cf. OF. assenement.]

1.

An allotting or an appointment to a particular person or use; or for a particular time, as of a cause or causes in court.

2. Law (a)

A transfer of title or interest by writing, as of lease, bond, note, or bill of exchange; a transfer of the whole of some particular estate or interest in lands.

(b)

The writing by which an interest is transferred.

(c)

The transfer of the property of a bankrupt to certain persons called assignees, in whom it is vested for the benefit of creditors.

Assignment of dower, the setting out by metes and bounds of the widow's thirds or portion in the deceased husband's estate, and allotting it to her.

Assignment is also used in law as convertible with specification; assignment of error in proceedings for review being specification of error; and assignment of perjury or fraud in indictment being specifications of perjury or fraud.

 

© Webster 1913.

Log in or register to write something here or to contact authors.