Circular dependency
In software engineering, a circular dependency (also called a cyclic dependency) is a connection between two or more modules that depend on each other, either directly or indirectly, in order to work properly.
Modules like these are also known as mutually recursive.
Overview
Circular dependencies are natural in many domain models, where certain objects of the same domain depend on each other. However, in software design, circular dependencies between larger software modules are seen as an anti-pattern because they can cause problems. Even so, these circular dependencies are common among the source files of real-world software. Mutually recursive modules are somewhat common in functional programming, where recursive ideas are often encouraged.
Problems
Circular dependencies can cause many unwanted effects in software programs. The biggest problem is the tight coupling of the mutually dependent modules, which reduces or makes it impossible to reuse a single module on its own.
Circular dependencies can cause a domino effect, where a small local change in one module spreads into other modules and causes unwanted problems across the program, such as errors. They can also lead to infinite recursions or other unexpected failures. They may cause memory leaks by stopping certain automatic garbage collectors (those that use reference counting) from clearing away unused objects.
Causes and solutions
In very large software designs, software engineers may lose track of the bigger picture and accidentally create circular dependencies. There are tools that can check software and find these unwanted loops.
Circular dependencies can appear when adding callback features. This can be avoided by using design patterns such as the observer pattern.
This article is a child-friendly adaptation of the Wikipedia article on Circular dependency, available under CC BY-SA 4.0.
