Note: The template referred to by this post does not ship with CodeRush. To install it, see instructions towards the end of the post.
What is a Metric
A metric is a measurement of some quality of your code. In this case it is a per-method measurement. CodeRush displays the metric for each member, on left of the member in question.
CodeRush ships with 3 such metrics:
- Lines of Code
Exactly what it sounds like. This metric measures the number of lines occupied by the method. This is a pretty basic metric which includes the signature and braces of the method. - Cyclomatic Complexity
A measurement of the number of paths through your code. The higher this value, the more tests will be needed to cover the method’s behaviours correctly. - Maintenance Complexity
This metric weights each element of the method according to how it affects the maintainability of said method. You can read more about this metric here
You can choose which metric CodeRush displays, by clicking the current value and choosing another metric from the list.
The NewMetric Template
As you’ll have guessed based on previous posts in this series, the purpose of the NewMetric template is the quickest way to create your own metric?
Usage
As with other plugin templates, this one is intended to be expanded (NewMetric<space>) in the body of your plugin class.
It will produce code that looks like this:
As with other plugin templates, you’ll have to give your Metric 3 names.
As a quick reminder, these are:
- Your own reference.
The first name is your own reference. The phrase you type here, is used to name the objects and associated methods of your Metric. - Internal name
This value is used internally by CodeRush. It should be unique, since it will be used to add your Metric to an internal dictionary. Later you can use this name to get a reference your Metric from another plugin. - Display Name
This value is used for the text displayed when your provider is added to the drop down list of metrics provided inline.
Next up, you’ll have to call the registerXXXX method (just generated and named) from within your plugin’s InitializePlugin method.
Finally you need to implement your plugin’s new metric. This is achieved by adding code to the GetMetricValue handler (also stubbed out by the template).
Typically you’ll examine the source of your project and perform some sort of calculation based on the LanguageElement passed to you in e.LanguageElement. Then you’ll assign the result of this calculation to e.Value.
So if you were looking to replicate the lines of code metric, you might use code similar to:
..and that’s it.
In Summary…
- Expand NewMetric template
- Call RegisterXXXX from InitializePlugin
- Implement your metric (possibly in a single line of code).
…and you’re done.
Where do I get this template?
As usual this template is available from the CodeRush Plugin Templates repository on GitHub
- You can download and import this template using these instructions…
- Or if you are using the Template Importer plugin, then you can point it at this url and import the template straight off the web.
If you have any suggestions to improve any of the templates in this series, feel free to contact me via email or on twitter.
If you so inclined you could even fork the project, make some changes and send me a pull request.