---
title: "Circular dependency"
type: "antipattern"
slug: "circular-dependency"
url: "http://localhost:3000/en/antipatterns/circular-dependency.md"
description: "Problematic interdependence of software modules"
---
# Circular dependency

> Problematic interdependence of software modules

In [software engineering](https://en.wikipedia.org/wiki/Software%5Fengineering "Software engineering"), a **circular dependency** (or **cyclic 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](https://en.wikipedia.org/wiki/Mutual%5Frecursion "Mutual recursion").

## Overview

Circular dependencies are natural in many [domain models](https://en.wikipedia.org/wiki/Domain%5Fmodel "Domain model") where certain objects of the same domain depend on each other. However, in [software design](https://en.wikipedia.org/wiki/Software%5Fdesign "Software design"), circular dependencies between larger software modules are considered an [anti-pattern](https://en.wikipedia.org/wiki/Anti-pattern "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](https://en.wikipedia.org/wiki/Functional%5Fprogramming "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](https://en.wikipedia.org/wiki/Coupling%5F%28computer%5Fprogramming%29 "Coupling (computer programming)")_ 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](https://en.wikipedia.org/wiki/Domino%5Feffect "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](https://en.wikipedia.org/wiki/Memory%5Fleak "Memory leak") by preventing certain automatic [garbage collectors](https://en.wikipedia.org/wiki/Garbage%5Fcollection%5F%28computer%5Fscience%29 "Garbage collection (computer science)") (those that use [reference counting](https://en.wikipedia.org/wiki/Reference%5Fcounting "Reference counting")) 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.

Circular dependencies can be introduced when implementing [callback](https://en.wikipedia.org/wiki/Callback%5F%28computer%5Fprogramming%29 "Callback (computer programming)") functionality. This can be avoided by applying [design patterns](https://en.wikipedia.org/wiki/Software%5Fdesign%5Fpattern "Software design pattern") like the [observer pattern](https://en.wikipedia.org/wiki/Observer%5Fpattern "Observer pattern").
