Dependency Analysis

If you are doing any serious Java development, especially if you are using third-party libraries, you will want to keep track of your dependencies. Not only does a successful deployment requires all components, but interdependencies help determine the maintainability of a system.

Java jar files are not as strict as other deployment methods, such as DLLs. A jar file will load even if one of its dependents is not in the class path. An exception will only be thrown at run time when and if a jar accesses another that is not available. Without good dependency analysis, you will have to test every feature to ensure that you haven't missed one.

In addition to ensuring a smooth deployment, dependency analysis can identify parts of the system that are resiliant to change. Components that others depend upon cannot be easily changed, while those that no others depend upon can be modified. Robert Martin, one of my favorite authors, describes this phenomenon better than I can in OO Design Quality Metrics.

Kirk Knoernschild has created JarAnalyzer to discover dependencies among jar files. Not only does it help you to ensure that you package all prerequisite components, but it also uses dependency to calculate metrics according to Robert Martin's methods. I have incorporated this tool into the build process at Handmark as one more measure of quality.

One caveat. Dependency analysis tools cannot easily discover relationships through reflection. A common pattern for loading a class is:

Class.forName("com.adventuresinsoftware.example.MyClass");

This runs the static initializer for the class. But since the class name is in a string, anayslis tools cannot trace it. I use this pattern instead:

com.adventuresinsoftware.example.MyClass.class.getName();

It has the same effect, it is tracable, and as an added bonus you get compile-time validation and intellisense.

One Response to “Dependency Analysis”

  1. patrick smacchia Says:

    I figured out that you were interested into dependencies analysis and I wanted to let you know about our tool NDepend.
    http://www.NDepend.com

    NDepend analyses source code and .NET assemblies. It allows controlling the complexity, the internal dependencies and the quality of .NET code.

    NDepend provides a language (CQL Code Query Language) dedicated to query and constraint a codebase.

    It also comes from with advanced code visualization (Dependencies Matrix, Metric treemap, Box and Arrows graph...), more than 60 metrics, facilities to generate reports and to be integrated with mainstream build technologies and development tools.

    NDepend also allows to compare precisely different versions of your codebase.

Leave a Reply

You must be logged in to post a comment.