Software Metrics in Eclipse


Laurie Williams, Dright Ho, and Sarah Heckman. [Contact Authors]
CSC 326 - Software Engineering
Department of Computer Science
North Carolina State University

Back to Software Engineering Tutorials


0.0 Outline
1.0 Introduction to Software Metrics
2.0 Installing Metrics Plug-in
3.0 Viewing Metrics
4.0 Discussion of Metrics
5.0 Interpretation of Metrics
6.0 Dependency Graph View
7.0 Exercise
8.0 Resources

1.0 Introduction to Software Metrics

Software metrics are used to give programmers feedback about their program. Metrics can be used as guidelines for when to refactor. They provide a way to measure the progress of code during development. In this tutorial, we are going to use Metrics, an open-source Eclipse plug-in, to gather metrics on a project. The plug-in should already be installed into Eclipse for you, but if it is not, installation instructions are provided for you in Section 2. Metrics version 1.3.6 requires Eclipse 3.1.

Top | Contents

2.0 Installing Metrics Plug-in

To install Metrics, you will need to create a new Eclipse update site.

2.1 Find the URL of the update site for the plug-in: http://metrics.sourceforge.net/update.

2.2 Create an update site for the plug-in.

2.1.1 Select Help > Software Updates > Find and Install. Select Search for new features to install. Click Next.

2.1.2 Click the New Remote Site button. Give the update site a name, such as the name of the plug-in, and type in the update site URL . Click OK. A new update site will be added to the list.

2.1.3 Check the update site that you want to update from (you may select more than one!). Click Finish.

2.1.4 If the update site has several mirrors (like the Eclipse update site) select the mirror you wish to update from. Click OK.

2.1.5 Press the + button next to the plug-in you want to install. This will list all possible features you can install. Select the features that you want to install. Click Next.

2.1.6 You will need to accept the terms of the licence agreement(s) of the feature(s) that you are installing. Click Next.

2.1.7 A list of feature(s) you are installing will be provided. Click Finish to start the install.

2.1.8 You will need to verify the features that you installed. Click Install or Install All to finish the installation.

2.1.9 You will now be asked to restart the workbench. Click Yes.

Top | Contents
3.0 Viewing Metrics

To view metrics on a project, first open up the Metrics view by selecting Window > Show View > Other.... Open up the Metrics tab and select Metrics.

In order to display metrics about a project you must enable the project to calculate metrics. Make sure you are in the Java Perspective and have your project open in the Package Explorer. Right click on a project and select Properties. Select Metrics on the left side, and check the Enable Metrics checkbox. Rebuild the project by selecting Project > Build Project (make sure you have the project that you want to build selected in Package Explorer.) If you have Build Automatically enabled, you will not be able to build your project in this manner. Just change a file and save. The project will be rebuilt for you, and the metrics will be calculated. The metrics will be displayed in the Metrics view.

There are 23 metrics that are displayed in the metrics view, and they are discussed in section 4 . If the metric is out of optimal range, then the metric is highlighted in red. Otherwise the metric is highlighted in blue if they are in range, and black if the metric cannot be calculated. Each metric has a plus box beside it that expands so that you can study the metrics on all source folders, packages and, where applicable, on class files and methods. Below are some of the metrics for the Coffee Maker example.

pictures#step3_0Metrics for the CoffeeMaker Example

Top | Contents

4.0 Discussion of Metrics

The metrics information given below was obtained directly from the Metrics plugin website: http://metrics.sourceforge.net/. The author of the metrics plug-in used "Object-Oriented Metrics, measures of Complexity" by Brian Henderson-Sellers and "OO Design Quality Metrics, An Analysis of Dependencies" by Robert C. Martin to obtain the definitions of the metrics used for this plug-in.

  • Lines of Code (LOC): Total lines of code in the selected scope. Only counts non-blank and non-comment lines inside method bodies.
  • Number of Static Methods (NSM): Total number of static methods in the selected scope.
  • Afferent Coupling (CA):The number of classes outside a package that depend on classes inside the package.
  • Normalized Distance (RMD): | RMA + RMI - 1 |, this number should be small, close to zero for good packaging design.
  • Number of Classes (NOC): Total number of classes in the selected scope
  • Specialization Index (SIX): Average of the specialization index, defined as NORM * DIT / NOM. This is a class level metric
  • Instability (RMI): CE / (CA + CE)
  • Number of Attributes (NOF): Total number of attributes in the selected scope.
  • Number of Packages (NOP): Total number of packages in the selected scope.
  • Method Lines of Code (MLOC): Total number of lines of code inside method bodies, excluding blank lines and comments.
  • Weighted Methods per Class (WMC): Sum of the McCabe Cyclomatic Complexity for all methods in a class.
  • Number of Overridden Methods (NORM):Total number of methods in the selected scope that are overridden from an ancestor class.
  • Number of Static Attributes (NSF): Total number of static attributes in the selected scope.
  • Nested Block Depth (NBD): The depth of nested blocks of code.
  • Number of Methods (NOM): Total number of methods defined in the selected scope.
  • Lack of Cohesion of Methods (LCOM): A measure for the Cohesiveness of a class. Calculated with the Henderson-Sellers method: If m(A) is the number of methods accessing an attribute A, calculate the average of m(A) for all attributes, subtract the number of methods m and divide the result by (1-m). A low value indicates a cohesive class and a value close to 1 indicates a lack of cohesion and suggests the class might better be split into a number of (sub)classes.
  • McCabe Cyclomatic Complexity (VG): Counts the number of flows through a piece of code. Each time a branch occurs (if, for, while, do, case, catch and the ?: ternary operator, as well as the && and || conditional logic operators in expressions) this metric is incremented by one. Calculated for methods only. For a full treatment of this metric see McCabe.
  • Number of Parameters (PAR): Total number of parameters in the selected scope.
  • Abstractness (RMA): The number of abstract classes (and interfaces) divided by the total number of types in a package.
  • Number of Interfaces (NOI): Total number of interfaces in the selected scope.
  • Efferent Coupling (CE): The number of classes inside a package that depend on classes outside the package.
  • Number of Children (NSC): Total number of direct subclasses of a class.
  • Depth of Inheritance Tree (DIT): Distance from class Object in inheritance hierarchy.
Top | Contents

5.0 Interpretation of Metrics

The safe range for each metric can be changed in the Window > Preferences menu. However, some safe ranges of metrics are provided for you.

  • Lines of Code (Method Level): Max 50 - If a method is over 50 lines of code it is suggested that the method be broken up for readability and maintainability.
  • Nested Block Depth (Method Level): Max 5 - If a block of code has over 5 nested loops, break up the method.
  • Lines of Code (Class Level): Max 750 - If a class has over 750 lines of code split up the class and delegate it's responsibilities.
  • McCabe Cyclomatic Complexity (Method Level): Max 10 - If a method has over 10 different loops, break up the method.
  • Number of Parameters (Method Level): Max 5 - A method should have no more than 5 parameters. If it does create an object and pass the object to the method.
Top | Contents

6.0 Dependency Graph View

The Metrics plugin also has a dependency graph view that allows you to see the dependency connections among various packages and classes in your project. To open the dependency graph, click the 5th red/gray square button in the upper right hand corner of the Metrics view (hover over the buttons until the tooltip tells you that the button opens the dependency graph.) The dependency graph allows you to see all the connections between various parts of your project. However, dependency graphs for small projects like CoffeeMaker are boring since the project is so small, but dependency graphs for a large project like Monopoly can be very interesting.


Dependency Graph View

Top | Contents
7.0 Exercise
Download the CoffeeMaker example or open the CoffeeMaker example that you used in earlier tutorials, and run the metrics calculations on the project. Show your TA.
Top | Contents

8.0 Resources
Top | Contents

Back to Software Engineering Tutorials
Metrics in Eclipse Tutorial ©2003-2009 North Carolina State University, Laurie Williams, Dright Ho, Sarah Heckman
Email the authors with any questions or comments about this tutorial.
Last Updated: Friday, September 2, 2005 3:13 PM