HOME

TheInfoList



OR:

In software engineering, a circular dependency is a relation between two or more modules which either directly or indirectly depend on each other to function properly. Such modules are also known as
mutually recursive In mathematics and computer science, mutual recursion is a form of recursion where two mathematical or computational objects, such as functions or datatypes, are defined in terms of each other. Mutual recursion is very common in functional progr ...
.


Overview

Circular dependencies are natural in many domain models where certain objects of the same domain depend on each other. However, in
software design Software design is the process by which an agent creates a specification of a software artifact intended to accomplish goals, using a set of primitive components and subject to constraints. Software design may refer to either "all the activity ...
, circular dependencies between larger software modules are considered an anti-pattern because of their negative effects. Despite this, such circular (or cyclic) dependencies have been found to be widespread among the source files of real-world software. Mutually recursive modules are, however, somewhat common in functional programming, where inductive and recursive definitions are often encouraged.


Problems

Circular dependencies can cause many unwanted effects in software programs. Most problematic from a software design point of view is the ''tight coupling'' of the mutually dependent modules which reduces or makes impossible the separate re-use of a single module. Circular dependencies can cause a domino effect when a small local change in one module spreads into other modules and has unwanted global effects (program errors, compile errors). Circular dependencies can also result in infinite recursions or other unexpected failures. Circular dependencies may also cause memory leaks by preventing certain very primitive automatic
garbage collector A waste collector, also known as a garbageman, garbage collector, trashman (in the US), binman or (rarely) dustman (in the UK), is a person employed by a public or private enterprise to collect and dispose of municipal solid waste (refuse) and r ...
s (those that use
reference counting In computer science, reference counting is a programming technique of storing the number of references, pointers, or handles to a resource, such as an object, a block of memory, disk space, and others. In garbage collection algorithms, referenc ...
) from deallocating unused objects.


Causes and solutions

In very large software designs, software engineers may lose the context and inadvertently introduce circular dependencies. There are tools to analyze software and find unwanted circular dependencies.JDepend
for Java
Circular dependencies can be introduced when implementing
callback Callback may refer to: * Callback (comedy), a joke which refers to one previously told * Callback (computer programming), executable code that is passed as a parameter to other code * Callback (telecommunications), the telecommunications event tha ...
functionality. This can be avoided by applying
design patterns ''Design Patterns: Elements of Reusable Object-Oriented Software'' (1994) is a software engineering book describing software design patterns. The book was written by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides, with a foreword ...
like the observer pattern.


See also

* Acyclic dependencies principle * Dependency hell


References

{{reflist Programming language topics C++ Articles with example C++ code