JDepend |
Summary | |
|
|
Mike Clark Clarkware Consulting, Inc. |
Table Of Contents |
Overview |
JDepend traverses a set of Java class and source file directories and generates design quality metrics for each Java package, including:
The number of concrete and abstract classes (and interfaces) in the package is an indicator of the extensibility of the package.
The number of other packages that depend upon classes within the package is an indicator of the package's responsibility.
The number of other packages that the classes in the package depend upon is an indicator of the package's independence.
The ratio of the number of abstract classes (and interfaces) in the analyzed package to the total number of classes in the analyzed package.
The range for this metric is 0 to 1, with A=0 indicating a completely concrete package and A=1 indicating a completely abstract package.
The ratio of efferent coupling (Ce) to total coupling (Ce / (Ce + Ca)). This metric is an indicator of the package's resilience to change.
The range for this metric is 0 to 1, with I=0 indicating a completely stable package and I=1 indicating a completely instable package.
The perpendicular distance of a package from the idealized line A + I = 1. This metric is an indicator of the package's balance between abstractness and stability.
A package squarely on the main sequence is optimally balanced with respect to its abstractness and stability. Ideal packages are either completely abstract and stable (x=0, y=1) or completely concrete and instable (x=1, y=0).
The range for this metric is 0 to 1, with D=0 indicating a package that is coincident with the main sequence and D=1 indicating a package that is as far from the main sequence as possible.
Package dependency cycles are reported along with the hierarchical paths of packages participating in package dependency cycles.
Why Use JDepend? |
Before using JDepend, it is important to understand that good design quality metrics are not necessarily indicative of good designs and bad design quality metrics are not necessarily indicative of bad designs. The design quality metrics produced by JDepend are intended to be used by designers to measure the designs they create, understand those designs, and automatically check that the designs exhibit expected qualities while undergoing continuous refactoring. Refactoring will undoubtedly lead to some adjustment of these metrics as the shape of the design changes. Most importantly, the design quality metrics produced by JDepend should not be used as yard sticks by which all designs are measured.
The quality of a design can be measured in part by quantifying its degrees of extensibility, reusability, and maintainability. These qualities are all influenced by the inter-package dependencies of the design. Designs are more extensible when they are independent of implementation details, allowing them to adapt to new implementations without internal modification or breaking their existing contracts. This same independence tends to increase the reuse potential of portions of the design. Independent portions of the design containing high-level abstractions can be extracted from portions containing implementation details. The maintainability of a design is improved when changes can easily be made without propagating to other parts of the system. JDepend allows you to automatically measure the quality of a design in terms of its extensibility, reusability, and maintainability to effectively manage and control package dependencies.
The goal of using JDepend is to ultimately invert package dependencies such that low-abstraction packages depend upon high-abstraction packages. This inversion of dependencies allows the high-abstraction packages to be reused independently while being extensible to an open set of implementations. In general, dependencies upon stable packages are desirable, while dependencies upon instable packages are undesirable. JDepend allows dependencies to be iteratively examined and refactored as an integral part of software design and development.
Packages that are stable should be the centerpieces of a loosely coupled application so the speed of the development team is not adversely affected by the propagation of software changes. Stable packages form design-by-contract facades to other subsystems, allowing teams to develop in parallel at an extreme pace. Moreover, by measuring the software design quality, the overall impact of proposed software changes can be accurately estimated. JDepend allows teams to identify and use desirable dependencies in the system and avoid those dependencies that cause changes to ripple throughout the system.
Third-party package dependencies can be easily identified and isolated by examining the afferent couplings to those packages. Once the dependency on these third-party packages has been measured with JDepend, the dependency can be managed by effectively designing abstract and stable packages that encapsulate the third-party package implementation details.
Packages that are cohesive and independent can be released as autonomous modules with their own release schedules and version numbers. Single packages, or groups of related packages collaborating in a framework, that are candidates for independent release can be harvested by evaluating their design quality metrics using JDepend.
Packages participating in a package dependency cycle are in a deadly embrace with respect to reusability and their release cycle. Package dependency cycles can be easily identified by reviewing the textual reports of dependency cycles. Once these dependency cycles have been identified with JDepend, they can be broken by employing various object-oriented techniques.
Downloading JDepend |
JDepend 2.2 is the latest release.
The distribution contains a JAR file, source code, sample application, API documentation, and this document.
Installing JDepend |
To install JDepend, follow these steps:
jdepend.zip
distribution file to
a directory referred to as %JDEPEND_HOME%
.
set CLASSPATH=%CLASSPATH%;%JDEPEND_HOME%\lib\jdepend.jar
To install JDepend, follow these steps:
jdepend.zip
distribution file to
a directory referred to as $JDEPEND_HOME
.
chmod -R a+x $JDEPEND_HOME
export CLASSPATH=$CLASSPATH:$JDEPEND_HOME/lib/jdepend.jar
Building And Testing JDepend |
The JDepend distribution includes the pre-built classes in the
$JDEPEND_HOME/lib/jdepend.jar
file.
An Ant
build file is included in $JDEPEND_HOME/build.xml
to
build the $JDEPEND_HOME/lib/jdepend.jar
file
from the included source code.
To build JDepend, use:
cd $JDEPEND_HOME ant jar
The JDepend distribution includes JUnit test cases to validate the integrity of JDepend.
To test JDepend, use:
cd $JDEPEND_HOME ant test
Running JDepend |
JDepend provides a graphical, textual, and XML user interface to visualize Java package metrics, dependencies, and cycles.
The graphical user interface displays a hierarchical tree for both the afferent and efferent couplings of each analyzed Java package.
To run JDepend with the graphical user interface, use the following syntax:
java jdepend.swingui.JDepend <directory> [directory2 [directory 3] ...]
For example, to analyze all the Java class and/or source files in the
$JDEPEND_HOME/sample
directory, use:
java jdepend.swingui.JDepend $JDEPEND_HOME/sample
The textual user interface displays detailed metrics, dependencies, and cycles for each analyzed Java package. For the convenience of importing these metrics into other applications, the summary section contains comma-separated metrics for each Java package. Alternatively, the XML user interface can be used for easier integration with other tools.
To run JDepend with the textual user interface, use the following syntax:
java jdepend.textui.JDepend [-file <output file>] <directory> [directory2 [directory 3] ...]
For example, to analyze all the Java class and/or source files in the
$JDEPEND_HOME/sample
directory, use:
java jdepend.textui.JDepend $JDEPEND_HOME/sample
Alternatively, the text report can be written to file using:
java jdepend.textui.JDepend -file report.txt $JDEPEND_HOME/sample
Example output from the textual
UI shows the analysis of the sample application, an example electronic payment
framework. The relevant source for the sample application is distributed in
$JDEPEND_HOME/sample
.
The XML user interface displays detailed metrics, dependencies, and cycles for each analyzed Java package in an XML format for easier integration with other tools.
To run JDepend with the XML user interface, use the following syntax:
java jdepend.xmlui.JDepend [-file <output file>] <directory> [directory2 [directory 3] ...]
For example, to analyze all the Java class and/or source files in the
$JDEPEND_HOME/sample
directory, use:
java jdepend.xmlui.JDepend $JDEPEND_HOME/sample
Alternatively, the XML report can be written to file using:
java jdepend.xmlui.JDepend -file report.xml $JDEPEND_HOME/sample
Example output from the XML
UI shows the analysis of the sample application, an example electronic payment
framework. The relevant source for the sample application is distributed in
$JDEPEND_HOME/sample
.
Graphical UI Navigation |
The graphical user interface displays the afferent and efferent couplings of each analyzed Java package, presented in the familiar Java Swing tree structure.
Figure 1 shows the analysis of the sample application, an
example electronic payment framework. The relevant source for
the sample application is distributed in $JDEPEND_HOME/sample
.
Figure 1 (Click to view full-scale)
The root of each tree displays a branch for each analyzed Java package, annotated with the following metrics:
For organizational purposes, package metrics are only displayed at the root of each tree. For convenience, selecting any node of the tree displays the currently selected package's metrics in the status bar.
The top tree displays the efferent couplings of each analyzed Java package. Branches of the tree can be opened up to explore packages that the currently selected package depends upon (Figure 2).
For the epayment.adapters
package, we see that it depends upon
4 other packages: the com.abc.epayment
, com.xyz.epayment
,
epayment.framework
, and the epayment.response
packages. Furthermore, it's completely concrete (A=0) and completely
instable (I=1). This balance earns it a spot squarely on the main
sequence (D=0). We can conclude from these metrics that dependencies on
this package are undesirable because it's both dependent and irresponsible.
It's sensitive to modifications made to any of it's efferent couplings and
not accountable to any other package. Therefore, it's important that other
packages in the system not become dependent on this package, as they'll in turn
become fragile by any modifications made to the details of the
epayment.adapters
package and its dependencies. As a concrete
package, it's not capable of being extended without being modified.
For the epayment.framework
package, we see that it does not
depend on any other packages in the application (Ce=0). However, it is
responsible to every other package (Ca=5) while exhibiting a high degree
of abstractness (A=0.83) and stability (I=0). While not completely balanced,
this package is very near the main sequence (D=0.17). We can conclude from
these metrics that dependencies on this package are desirable because it's both
independent and responsible. It's abstractness also indicates that it's
capable of being extended to accomodate new implementations without being modified.
Packages that were imported, but not analyzed, are not shown in the efferent dependency tree. Third-party software packages that weren't analyzed, for example, will not be shown in the efferent tree, as their efferent dependencies are not available.
The bottom tree displays the afferent couplings of each analyzed Java package. Branches of the tree can be opened up to explore packages that use the currently selected package (Figure 3).
For the epayment.adapters
package, we see that it is
not used by any other package in the application. This confirms
our observations of the efferent dependency tree.
For the epayment.framework
package, we see that it's used by
all the other user-defined packages in the framework. However, it does not
have any efferent couplings (Ce=0) and exhibits a high degree of abstractness
(A=0.83) and stability (I=0). This is a requirement of a software framework -
we want it to be heavily used, thereby making it very responsible to its clients,
yet be highly abstract to allow extensibility without modification.
For the com.abc.epayment
package, a third-party software
package, we see that it's used by the epayment.adapters
package.
There are no metrics displayed for this package however, as it's a third-party
package that was not analyzed by JDepend. It was imported by a
user-defined package (epayment.adapters
), so it is shown in the
afferent dependency tree.
Using the afferent dependency tree, it's easy to identify which user-defined packages are dependent upon third-party software packages.
Interpreting Dependency Cycles |
Package dependency cycles are best observed using the textual or XML user interface. In general, all packages dependencies that intersect a dependency cycle are reported. This includes packages directly participating in a cycle and packages that depend on packages directly participating in a cycle.
The intent is to identify sets of packages that must be reused and released together. To break reported cycles, focus on those packages directly participating in a cycle.
Here's an example of a two-package cycle, as reported by the textual UI:
com.xyz.ejb | | com.xyz.servlet |-> com.xyz.ejb
This indicates that the com.xyz.ejb
package depends on the
com.xyz.servlet
package, which in turn depends on the
com.xyz.ejb
package. These two package must be released and
reused together.
Here's an example of a package that depends on the two-package cycle described above, as reported by the textual UI:
com.xyz.client | |-> com.xyz.ejb | com.xyz.servlet |-> com.xyz.ejb
This indicates that the com.xyz.client
package depends on the
com.xyz.ejb
package, which in turn forms a cyclic dependency with
the com.xyz.servlet
package. The com.xyz.client
package
itself isn't part of the cycle, but since it depends on a package in the cycle, it
can't be reused/released without it.
Customizing JDepend |
JDepend can be customized by creating a jdepend.properties
file in the user's home directory or any directory in the classpath.
By default, JDepend ignores the java.*
and javax.*
package
names. JDepend will also ignore all package name prefixes specified as values to the
ignore
property name prefix in the jdepend.properties
file.
The following example jdepend.properties
file will ignore all package
names prefixed by com.xyz.tools
, com.xyz.util
,
com.xyz.tests
, or com.xyz.collections
:
ignore=com.xyz.tools,com.xyz.util ignore.tests=com.xyz.tests ignore.deprecated=com.xyz.collections
Using JDepend With JUnit |
In the spirit of extreme programming, metrics can be automatically collected by JDepend so that they never go stale or require visual inspection. Tolerances for any collected metrics, for example the distance from the main sequence (D), can be codified in a JUnit test case that automatically checks the metrics for conformance to a desired result and provides immediate visual feedback. Tests can also be written to fail if any package dependency other than those declared in a dependency constraint are detected. The existence of package dependency cycles can also be automatically checked by a JUnit test. As the software evolves through refactorings, the design quality test cases can be run as a sanity check to ensure that the design has not formed too many undesirable dependencies.
The following example JUnit test case tests whether a package dependency constraint is met. This test fails if any package dependency other than those declared in the dependency constraint are detected:
JUnit Package Dependency Constraint Test | |
|
The following example JUnit test case tests for the existence of package dependency cycles:
JUnit Package Dependency Cycle Test | |
|
The following example JUnit test case tests the conformance of packages to a distance from the main sequence (D) within project-defined tolerances:
JUnit Main Sequence Distance Test | |
|
Using JDepend With Ant |
Ant includes a task for automatically running JDepend.
Java source and class file directories to analyze are defined by the nested
<sourcespath>
element.
The following example Ant task runs JDepend on the classes
directory
and writes the text report to the docs/jdepend-report.txt
file:
JDepend Ant Task | |
|
Ant 1.5 and above includes a format
attribute for the JDepend Ant task
and a default XSL stylesheet to transform a JDepend XML report into an HTML report.
The following example Ant task runs JDepend on the classes
directory,
writes the XML report to the docs/jdepend-report.xml
file, and
generates the jdepend.html
file using the jdepend.xsl
stylesheet:
JDepend Ant Task | |
|
Limitations |
JDepend has the following known limitations:
Support |
If you have any questions, comments, enhancement requests, success stories, or bug reports regarding JDepend, or if you want to be notified when new versions of JDepend are available, please email mike@clarkware.com. Your information will be kept private.
A mailing list is also available to discuss JDepend or to be notified when new versions of JDepend are available.
After experimenting with these metrics on your own designs, feel free to share your findings. Ultimately, if better and different ways to measure designs are discovered, JDepend will support them.
Consulting Services |
As the Principal Consultant for Clarkware Consulting, Inc., I offer pragmatic design, development, and mentoring services to help you and your team deliver high-quality software rapidly and predictably. Architecture and design review services are also available to help you effectively build high-quality systems based on today's business needs rather than speculation. My services are always tailored and scaled to your specific needs to deliver maximum business value.
Contact me by email or
call 720.851.2014 for more details.
License |
JDepend is licensed under the BSD License.
Acknowledgments |
Many thanks to Robert Martin for originally describing these design quality metrics and writing the C++ dependency analyzer from which the JDepend framework was adapted. I am especially grateful that he allowed me to "stand on the shoulders of giants" in adapting his work for the Java community.
Resources |
Release History |
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||
|
Copyright © 1999-2002
Clarkware Consulting, Inc.
All Rights Reserved