As an avid fan of CodeRush, I am awed at the extensibility that this product provides. One area is in the extensibility of creating custom plugins. I took the time this weekend to understand how to build a custom metric after seeing a training video up at the DevExpress site. The metric I chose to build was one that counted the number of returns in a method. This metric will be useful in code reviews to see if a method may be too complex based upon the number of returns.
Here are my high level steps:
1. Create a new plugin project.
2. Drag and drop a CodeMetricProvider from the toolbox and set the following properties
3. Implement the method for getting the metric value, it should look something like this:
private void metricReturnsCounter_GetMetricValue(object sender, DevExpress.CodeRush.Extensions.GetMetricValueEventArgs e)
if (e.LanguageElement is Member)
Member member = (Member)e.LanguageElement;
int counter = 0;
foreach (LanguageElement element in member.AllFlowBreaks)
if (element.ElementType == LanguageElementType.Return)
e.Value = counter;
That’s it, the final result looks like this in the metrics window of CodeRush:
As you can see, it looks like there is a lot of work that can be done on a couple of these methods, to clean the number of returns up.