Sunday, June 28, 2015

Why Circular Dependencies Between Java Packages are Bad?

No doubt, Circular dependencies between "Modules" (projects, jars, etc..) is BAD.
It has all the consequences which lead to problem in the areas like,

  • Maintainability
  • Re-usability
  • Testability
  • Compilation problems
  • Release and build problems
  • Domino defect
  • Memory leaks

But does it really matter when there are a circular dependencies between packages?


I was searching and reading about this, but i could not find an exact reasonable answer.
So the my opinion is, it's a design concern.

In the OO design principles (SOLID[1] principle) dependency is a major concern[2]. So when we design according to the OO model we should avoid circular dependencies.

Just imagine we are going to apart some packages form the program and maintain the program as a two different modules. Then the circular dependencies would make all the problem i mentioned for module dependencies. Therefore we can say circular dependencies reduces the extensibility of the program.

Packages are used to organize classes belonging to the same category or providing similar functionality[3]. When there is a circular dependency, according to my experience what i can say is, there should be components which represent a certain functionality or a category which can be re-organized to single package. Therefore if we are really following Java OO design guidelines, the circular dependencies should not be present.

Some programming languages (Eg: Go) don't allow circular dependencies (circular imports). But Java allows it. But we should pay the attention on proper OO design and avoid circles.


References:
[1] https://en.wikipedia.org/wiki/SOLID_(object-oriented_design)
[2] https://en.wikipedia.org/wiki/Dependency_inversion_principle
[3] https://en.wikipedia.org/wiki/Java_package

No comments:

Post a Comment