What is circular dependency and why is it bad?

Circular dependency is when two pieces of code each depend on each other.  Circular dependency is usually considered a sign of bad design.

We typically develop software by:

  • Dividing our code into components (i.e piece of code by class, namespace, assemblies).
  • Making sure that there are no dependency cycles between our components.

Below is a standard 3-tier Layered Architecture.  As a rule, each layer should only depend on the ones beneath it.


Circular dependency happens when higher layers depend on lower layers and lower layers depend on higher layers.

3-Tier Circular DependencyPNG

Why circular dependencies should be avoided:

  • It causes tight coupling between layers.
  • Code with cross-dependencies will be harder to understand and maintain than with a clean, layered structure.
  • They are difficult to unit test because they cannot be tested in isolation from one another.
  • .Net projects cannot have circular references as it is impossible to build only one of them without the other.  Even if you do find a way to build each without error, it doesn’t change the fact that together they represent one single super component.
  • Can result in memory leaks.   For garbage collectors that use reference counting, when each object holds on to the other one, their reference counts will never reach zero.  Therefore, they will never become candidates for  automatic collection and cleanup.

To understand further, you can read Uncle Bob’s article on designing packages.  Refer to “The Acyclic Dependencies Principle (ADP)“. http://www.objectmentor.com/resources/articles/granularity.pdf.


Speak Your Mind