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.
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'' 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 collectors (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 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
* 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