This Eclipse plug in calculates various metrics for your code during build cycles and warns you, via the Task List, of 'range violations' for each metric. This allows you to stay continuously aware of the health of your code base. You may also export the metrics to HTML for public display or to CSV format for further analysis.
Note that although metrics can be useful in a software development effort, they should not take the place of good taste and experience. Also, metrics are more useful as indicators of unhealthy code than they are as indicators of healthy code, i.e. a Task List with many range violation warnings is probably an indication that the code needs to be refactored but a Task List with no range violation warnings does not necessarily mean that the code is good.
Using the Plugin
Download the latest version of the plugin from here, unzip it into your plugins directory and (re)start Eclipse.
If you find any bugs, please report them to metrics@teaminabox.co.uk.
Enabling / Disabling
The metric plugin is enabled and disabled per Java project. In any Java Perspective, open the project's Properties. Select the 'Metrics' tab and check or uncheck the 'Enable Metrics Gathering' checkbox. Clicking 'OK' now causes the project to recompile (assuming your preferences are set up to perform automatic builds) and metrics will be calculated (or removed). If any metrics are out of range, warnings will be added to the Task List and the code will also be marked up as usual.
Exporting Metrics in HTML or CSV Format
To export the metrics for a project, pop up the context sensitive menu on any resource in the project and select 'Export...'. Choose the 'Metrics' export wizard and select the directory you want to export to and the export formats you require. Click 'OK'.
When exporting HTML, you can elect to create a histogram for each metric (does not work on Mac OSX). The appropriate metric's histogram will be displayed on each page. See here for an example of some exported metrics. If this feature is enabled, a 'Dashboard' will also be produced, containing the collected histograms for the project.
Metrics do not have to be enabled on the project in order to export.
Excluding Java Files
If you have Java files for which you do not want to have metrics calculated, you can exclude them. To do this, open the project's Properties in any Java Perspective and select the 'Metrics' tab. You can either choose the files to ignore by browsing the file system (press 'Add Files...') or you can enter a (Perl5) regular expression that matches the file path (press 'Add Regex...').
Regular expressions are particularly useful when you have a naming convention for classes such as Mocks, Visitors, Mementos, etc. As an example, if you want to exclude all classes with 'Mock' in the path , you could specify the regex ".*Mock.*".
Setting Preferences
Open the Window/Preferences dialog. Select the 'Metrics' tab. You can now:
- enable or disable each metric
- set the maximum preferred values for each of the available metrics
- some metrics allow you to tune how they work
Once the preferences have been set, clicking 'Apply' or 'OK' causes the project to recompile (assuming your preferences are set up to perform automatic builds) and metrics will be calculated. If any metrics are out of range, warnings will be added to the Task List and the code will also be marked up as usual.
Supported Metrics
- McCabe's Cyclomatic Complexity
- Efferent Couplings
- Lack of Cohesion in Methods
- Lines of Code in Method
- Number Of Fields
- Number Of Levels
- Number Of Parameters
- Number Of Statements
- Weighted Methods Per Class
More metrics will be added over time.
Coming Soon
These are ideas for features to add, some of which have been kindly donated by users of the Metrics Plugin. If you have any requests, please submit them to metrics@teaminabox.co.uk. I plan to do a release every few weeks, so keep an eye on this site or Eclipse Plugins.
Top of my list are:
- Exclusions for particular metric calculations for particular files/directories/packages
- Suppression of particular metric line items in the task list
- Allow metrics to be presented in a view other than the Task List
Some more, in no particular order...
- More metrics - particularly those to do with class cohesion and coupling. My focus is on metrics that relate to maintainability and quality (which is admittedly subjective). Essentially, if a 'good' upper or lower bound is not meaningful for the metric, it doesn't grab my attention in the context of this plugin - that doesn't mean I won't implement it if you ask.
- Exclusion of certain constructs from the
Cyclomatic Complexity
calculation. An example is the commonly used
if (log.isDebugEnabled()) { log.debug(<lots of concatenation>); }
- Overriding preferred upper bounds for particular packages.
Many thanks for using the Metrics Plugin - Lance Walton, Team in a Box Ltd