Why are circular references considered harmful? [closed]

Circular dependencies between classes are not necessarily harmful. Indeed, in some cases they are desirable. For example, if your application dealt with pets and their owners, you would expect the Pet class to have a method to get the pet’s owner, and the Owner class to have a method that returns the list of pets. Sure, this can make memory management more difficult (in a non-GC’ed language). But if the circularity is inherent in the problem, then trying to get rid of it is probably going to lead to more problems.

On the other hand, circular dependencies between modules are harmful. It is generally indicative of a poorly thought-out module structure, and/or failure to stick to the original modularization. In general, a code-base with uncontrolled cross-dependencies will be harder to understand and harder to maintain than one with a clean, layered module structure. Without decent modules, it can be much harder to predict the effects of a change. And that makes maintenance harder, and leads to “code decay” resulting from ill-conceived patching.

(Also, build tools like Maven won’t handle modules (artefacts) with circular dependencies.)

Leave a Comment