Home > Plug-in Development > How to use CodeMetricProvider control to build you own code metric

How to use CodeMetricProvider control to build you own code metric

December 9th, 2010

Let’s implement the Comment Density code metric using the CodeMetricProvider DXCore component. This code metric provides the ratio of comment lines to all lines and indicates the comments coverage of your source code. We are going to calculate it in percents for all type declarations like this:

Comment Density = (comment lines / code lines) * 100

The density of comments metric value will be between 0 and 100 percents, and can be used as a quality indicator to see how much of the code is commented. So, if the comment density value for instance is below a certain value defined by the project manager, it indicates that the code is under commented. Let’s assume that what’s recommended is a commenting of at least 15% of the code and a maximum of 30%.

Follow these steps to get the CodeMetricProvider component working:

  • Create a new DXCore plug-in if you haven’t done it yet
  • Drop the CodeMetricProvider control on the plug-in designer surface
  • Fill the ProviderName property of the CodeMetricProvider
  • Fill the Description property of the CodeMetricProvider if needed
  • Set the MetricGoal property to Types

DXCore Creating new code metric

  • Then, subscribe to the GetMetricValue event:

CSharp code:

    private void codeMetricProvider1_GetMetricValue(object sender, GetMetricValueEventArgs e)
    {
    }

Visual Basic code:

  Private Sub codeMetricProvider1_GetMetricValue(ByVal sender As Object, ByVal e As GetMetricValueEventArgs) Handles codeMetricProvider1.GetMetricValue

  End Sub
  • Next, add a code for metric calculation into the GetMetricValue event handler:

CSharp code:

private void codeMetricProvider1_GetMetricValue(object sender, GetMetricValueEventArgs e)
{
  // determine if we are working with types...
  TypeDeclaration type = e.LanguageElement as TypeDeclaration;
  if (type == null)
    return;

  // counters...
  float typeCodeLines = type.Range.LineCount;
  float typeCommentLines = 0;

  // enumerating all comments in the given type...
  ElementEnumerable enumerable = new ElementEnumerable(type, LanguageElementType.Comment, true);
  foreach (Comment comment in enumerable)
    typeCommentLines += comment.Range.LineCount;

  // return result...
  e.Value = (int)(typeCommentLines / typeCodeLines * 100);
}

Visual Basic code:

Private Sub codeMetricProvider1_GetMetricValue(ByVal sender As Object, ByVal e As GetMetricValueEventArgs) Handles codeMetricProvider1.GetMetricValue
  ' determine if we are working with types...
  Dim type As TypeDeclaration = CType(e.LanguageElement, TypeDeclaration)
  If (type Is Nothing) Then
    Return
  End If

  ' counters...
  Dim typeCodeLines As Single = type.Range.LineCount
  Dim typeCommentLines As Single = 0

  ' enumerating all comments in the given type...
  Dim enumerable As ElementEnumerable = New ElementEnumerable(type, LanguageElementType.Comment, True)
  For Each comment As Comment In enumerable
    typeCommentLines += comment.Range.LineCount
  Next

   ' return result...
   e.Value = (typeCommentLines / typeCodeLines * 100)
End Sub

That’s it.

  • Compile the plug-in
  • Open a new instance of the Visual Studio IDE
  • Load IDE tools if they’re not loaded automatically
  • Open any project and check how the new code metric works inside the Refactor! Metrics tool window (DevExpress | Tool Windows | Metrics ):

If you have any issues or need any further assistance, please post a comment here or contact DevExpress Support Services.

—–
Products: DXCore, Refactor! Pro
Versions: 10.2 and up
VS IDEs: any
Updated: Dec/10/2010
ID: D045

Similar Posts: