Everybody has a pet thing about software design that they absolutely hate. Examples might include: use of multiple inheritance, over abstraction, leaky abstractions etc. My pet hate is circular dependencies. I believe they are evil, and Java makes it all to easy to do.

An example of a circular dependency is having three artifacts (could be a Java class, interface or package) with the following dependencies:

  • A depends on B
  • B depends on C
  • C depends on A

This means that implicitly that A depends on C, and C depends on A. So what I hear you say? Well, IMHO all good software design is broken into components/layers/tiers with very clear hierarchical one-way dependencies. In the example above, it is not possible to make a change to any artifact without (potentially) affecting the others. Or from a configuration management perspective, it is not possible to release artifacts separately.

Well I discovered a really cool plug-in today for Eclipse called Metrics. It produces many metrics about the Java code in your project. The really nice feature is being able to analyse for circular dependencies - something it calls a "tangle" (nice name). Here is an example graph it produces. Certainly a lot easier that using JDepend, so I give it the big "Thumbs Up".

Side note, I used to take my hate of circular dependencies to the extreme - I would not have circular dependencies between classes and interfaces in the same package. I had the pleasure of working for several years with Matt, who is one of the most talented developers I know. Eventually he convinced me to relax this, primarily because Java does not have the notion of forward declarations (like C**).