AlgoViz

Circular dependencies

medium 45 min

Two classes or modules that need each other, directly or through others, form a cycle.

Watch lesson4:48

Where it shows up

The same pattern appears across everyday software.

  • Spring Boot

    Since 2.6 circular references between beans are prohibited by default. The release notes advise updating the application to break the cycle, with a property as a temporary way back.

  • Spring Framework

    The reference explains that constructor injection can create an unresolvable ring, detected at run time as BeanCurrentlyInCreationException, and that setter injection can configure it but is not recommended.

  • Go

    The language specification makes it illegal for a package to import itself, directly or indirectly. Teams feel a ring on the day it is written.

  • NestJS

    Documents `forwardRef` for rings between providers and modules, and advises avoiding circular dependencies where possible.

  • Angular

    Reports a circular dependency in dependency injection as error NG0200, with the path to look at.

  • Python standard library

    `graphlib.TopologicalSorter` orders tasks and raises CycleError with the ring. The programming FAQ answers how modules can import each other, and recommends where to put imports.

  • Node.js

    The CommonJS documentation shows a ring of two files and explains that the second one receives an unfinished copy of the exports object.

  • ArchUnit

    Java tests for architecture: `slices().matching(...).should().beFreeOfCycles()` makes a package ring a failing test.

  • Bazel

    Targets form a directed acyclic graph. The build tool builds a target after its dependencies, which a ring would make impossible.

  • C++ Core Guidelines

    Rule SF.9 says to avoid cyclic dependencies among source files.