Circular Reference - in Computer Programming

In Computer Programming

For circular references between objects or resources, see Reference counting.

Circular references can appear in computer programming when one piece of code requires the result from another, but that code needs the result from the first.

For example:

Function A will show the time the sun last set based on the current date. Function B will calculate the date based on the number of times the moon has orbited the earth since the last time Function B was called. So, Function B asks Function C just how many times that is. Function C doesn't know, but can figure it out by calling Function A to get the time the sun last set.

The entire set of functions is now worthless because none of them can return any useful information whatsoever. This leads to what is technically known as a livelock. It also appears in spreadsheets when two cells require each others' result. For example, if the value in Cell A1 is to be obtained by adding 5 to the value in Cell B1, and the value in Cell B1 is to be obtained by adding 3 to the value in Cell A1, no values can be computed. (Even if the specifications are A1:=B1+5 and B1:=A1-5, there is still a circular reference. It doesn't help that, for instance, A1=3 and B1=-2 would satisfy both formulas, as there are infinitely many other possible values of A1 and B1 that can satisfy both instances.)

A circular reference represents a big problem in computing. A deadlock occurs when two or more processes are each waiting for another to release a resource. Most relational databases such as Oracle and SQL Server do not allow circular referencing because there is always a problem when deleting a row from a table having dependencies to another row from another table (foreign key) which refers to the row being deleted. From the technical documentation at Microsoft: The FOREIGN KEY constraints cannot be used to create a self-referencing or circular FOREIGN KEY constraint.

For Oracle and PostgreSQL the problem with updating a circular reference can be solved by defining the corresponding foreign keys as deferrable (See CREATE TABLE for PostgreSQL and DEFERRABLE Constraint Examples for Oracle). In that case the constraint is checked at the end of the transaction not at the time the DDL statement is executed. To update a circular references two statements can be issued in a single transaction that will satisfy both references once the transaction is committed.

Only inner joins are supported and are specified by a comparison of columns from different tables. Circular joins are not supported. A circular join is a SQL query that links three or more tables together into a circuit. Oracle uses the term Cyclic to designate a circular reference.

A distinction should be made with processes containing a circular reference between those that are incomputable and those that are an iterative calculation with a final output. The latter may fail in spreadsheets not equipped to handle them but are nevertheless still logically valid.

Read more about this topic:  Circular Reference

Famous quotes containing the words computer and/or programming:

    What, then, is the basic difference between today’s computer and an intelligent being? It is that the computer can be made to see but not to perceive. What matters here is not that the computer is without consciousness but that thus far it is incapable of the spontaneous grasp of pattern—a capacity essential to perception and intelligence.
    Rudolf Arnheim (b. 1904)

    If there is a price to pay for the privilege of spending the early years of child rearing in the driver’s seat, it is our reluctance, our inability, to tolerate being demoted to the backseat. Spurred by our success in programming our children during the preschool years, we may find it difficult to forgo in later states the level of control that once afforded us so much satisfaction.
    Melinda M. Marshall (20th century)