Circular Dependencies
   HOME

TheInfoList



OR:

In
software engineering Software engineering is a branch of both computer science and engineering focused on designing, developing, testing, and maintaining Application software, software applications. It involves applying engineering design process, engineering principl ...
, 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 program ...
.


Overview

Circular dependencies are natural in many
domain model In software engineering, a domain model is a conceptual model of the domain (software engineering), domain that incorporates both behavior and data.Fowler, Martin. "P of EAA - Domain Model"/ref> In ontology engineering, a domain model is a Knowl ...
s where certain objects of the same domain depend on each other. However, in
software design Software design is the process of conceptualizing how a software system will work before it is implemented or modified. Software design also refers to the direct result of the design process the concepts of how the software will work which co ...
, circular dependencies between larger software modules are considered an
anti-pattern An anti-pattern in software engineering, project management, and business processes is a common response to a recurring problem that is usually ineffective and risks being highly counterproductive. The term, coined in 1995 by computer programmer An ...
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 In computer science, functional programming is a programming paradigm where programs are constructed by Function application, applying and Function composition (computer science), composing Function (computer science), functions. It is a declarat ...
, 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 A coupling is a device used to connect two shafts together at their ends for the purpose of transmitting power. The primary purpose of couplings is to join two pieces of rotating equipment while permitting some degree of misalignment or end mo ...
'' 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 A domino effect is the cumulative effect produced when one event sets off a series of similar or related events, a form of chain reaction. The term is an analogy to a falling row of dominoes. It typically refers to a linked sequence of events ...
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 leak In computer science, a memory leak is a type of resource leak that occurs when a computer program incorrectly manages memory allocations in a way that memory which is no longer needed is not released. A memory leak may also happen when an objec ...
s by preventing certain automatic
garbage collector A waste collector, also known as a garbage man, garbage collector, trashman (in the U.S), binman or dustman (in the UK), is a person employed by a public or private enterprise to collect and dispose of municipal solid waste (refuse) and recycla ...
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, refere ...
) 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 Java is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea (a part of Pacific Ocean) to the north. With a population of 156.9 million people (including Madura) in mid 2024, proje ...
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), callable (i.e. function) that is passed as data and expected to be called by another callable. * Callback (telecommunication ...
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 fore ...
like the
observer pattern In software design and software engineering, the observer pattern is a software design pattern in which an object, called the ''subject'' (also known as ''event source'' or ''event stream''), maintains a list of its dependents, called observers (a ...
.


See also

*
Acyclic dependencies principle The acyclic dependencies principle (ADP) is a software design principle defined by Robert C. Martin that states that "''the dependency graph of packages or components should have no cycles''". This implies that the dependencies form a directed acy ...
*
Dependency hell Dependency hell is a colloquial term for the frustration of some software users who have installed software packages which have dependencies on specific versions of other software packages. The dependency issue arises when several packages ha ...


References

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