Agile Developer’s Toolbox: Metrics for Eclipse
Who cares about software metrics? As a Java developer, I do. Measuring certain aspects of my code lets me quantify my schedule, work effort, product size, project status, and code quality. Oh, and then my project manager cares too. If I don’t measure my current status (number of classes, dependencies to other modules, complexity, code coverage, defects as well as code smells - yes, we all know when we have code smells) and use the data to improve my code and my future work estimates, those estimates will just be guesses. And my project manager expects a little better than guesses.
Metrics is an Eclipse plugin that helps me take a holistic view of various aspects of my code. I use it on a daily basis to see how my project grows and if there are any deviations from the reference architecture. The plugin lets me take a closer look at:
-
Number of Classes
-
Method Lines of Code
-
Number of Methods
-
Nested Block Depth - Is nice, I like!
-
Depth of Inheritance Tree - nobody wants to traverse a 16-node deep tree trying to find what they need. 1-5 maybe, but 16….too many levels…
-
Afferent Coupling, Efferent Coupling - yea, whatever… we saw this here…same story…
-
Cyclomatic Complexity - If my code is simpler than Paul’s does that make me look “simpler” too?
-
…and more…
Quick Tutorial
-
Download and start Eclipse 3.2
-
Navigate to Help | Software Updates | Find and Install
-
Select Search for new features to install
-
Add a new remote site Metrics and point it to http://metrics.sourceforge.net/update
-
Finish the wizard and restart Eclipse
-
Download the sample solarsystem project from http://www.testearly.com/resources/agile/solarsystem.zip. Open the project in Eclipse. Note that this sample requires Java SDK 6.0.
-
Build the project
-
In the Package Explorer, right-click the solarsystem project and open the Properties dialog. Locate Metrics and check Enable Metrics
-
Rebuild solarsystem
-
Navigate to Window | Show View | Metrics | Metrics View
-
You are looking at a table of various metrics covering the solarsystem project
-

-
Feel free to dig down on each metric as it goes from source folder to package to class down to the method
-
Notice all metrics for this simple project are in blue and there are no exclamation marks in the source code indicating any problems. Let’s change that and make life difficult for ourselves.
-
Open the preferences page for Metrics and check Enable out-of-range warnings. Drill one level down to Safe Ranges, locate Nested Block Depth and enter 5 as the Max value. Similarly, enter 10 as the Max value for McCabe Cyclomatic Complexity
-
In a Java Perspective open PlanetUtil.java and add the following method:
public void thisIsAComplexMethod() { if (true) { if (false) { if (true) { if (false) { if (true) { System.out.println("This is a deeply nested if sequence."); } } else { } } } } if (true) { if (false) { if (true) { if (false) { if (true) { System.out.println("This is a deeply nested if sequence."); } } else { } } } } } -
Rebuild solarsystem and return to the Metrics View. You now see two red lines in the results table and an exclamation mark in the code editor. Clearly the new code failed the cyclomatic complexity and nested statements rules of Metrics and therefore was marked red. The cyclomatic complexity of the method is 11, while the if statements nest for 6 levels. Both values are out of the acceptable ranges we defined earlier.
-

-
Locate the Dependency Graph View icon in the Metrics View. It can be found on the upper right corner and it is denoted by four dots connected in a square. Click the icon to open the graphical dependency view of Metrics.
-

- The packages in the project are denoted by boxes in different colors. Blue means normal relationship; red means cycles and orange denotes the selected node. There is also a circle in the middle, the tangle center, which displays the number of packages in a particular tangle and the longest walk. In general, the longer the walk the more layers are involved and therefore there may be refactoring opportunities. Similarly, the existence of red nodes and tangles indicates cycles in the package dependencies and that’s also something that may be avoided with a little care. Finally, every node and the tangle center provide right-click menus to drill-down for more details.
- The reason for this tangle (cycle) is the JUnit test PlanetUtilTest, which is located in com.solarsystem.jupiter. Let’s do something about this.
- In the Package Explorer, locate PlanetUtilTest, right-click and select Refactor | Move. Move the test into a new package com.solarsystem.test.jupiter.
- Rebuild the project. Open the Metrics View. Click the graph icon. Everything should be blue. There should be no tangles. No tangle centers. Blue is better than red. Life is good…

- Enjoy.
