Metrics analyzer is analyzer extension to check if you code follow metrics rules. Code metrics is applied only to members (constructor, destructor, property getter and setter, indexer getter and setter and methods). Following metrics could be used:
- Maintainability index
- Cyclomatic complexity index
- Lines of code
- Number of local variables
- Number of parameters
- Lines of comment
In default three rules are applied only:
- Maintainability rule checks if your code is maintainable.
- Value above 20 is maintainable code and there is not any message.
- Value between 10 and 20 is code which need attention and it is hardly to maintain. It is indicated by warning message.
- Value under 10 is code which is not maintainable. It is indicated by error message.
- Cyclomatic complexity checks if your code is not complicated.
- Value under 10 is code which is not too much complicated. There is not any message.
- Value between 10 and 15 is code which is harder to undestand. It is indicated by warning message.
- Value above 15 is code which is very complicated and can lead to brain overload. It is indicated by error message.
- Lines of code checks if there are members with empty body. If lines of code is zero warning message is shown.
Maintainability index and cyclomatic complexity rules are documented in:
You can easily write and adopt values as you want. This extension works as plugin system. It means you must create library which inherits class from metrics analyzer and implement evaluation of metrics.
I have installed this extension into C:\Users\username\AppData\Local\Microsoft\VisualStudio\14.0\Extensions\njd3wzuw.xnz. Directory can be different at another version of Visual Studio and user settings. You can see there is DefaultAnalyzer.dll. It is library which implements default two rules evaluation.
There is file MetricsAnalyzer.dll.
- Create class library project.
- Add it into your references.
- Create class YourMetricAnylyzer inherits from MetricAnalyzer class in assembly MetricsAnalyzer.
- Implement Initialize method.
There is example how to create rule for lines of code.
public class DefaultAnalyzer : MetricsAnalyzers.MetricAnalyzer
{
public override void Initialize(MetricsContext context)
{
context.RegisterCalculated(Calculated);
}
private void Calculated(Node obj)
{
forach(Metric metric in obj.Metrics)
{
if (metric is LinesOfCodeMetric)
{
MetricsAnalyzers.Metrics.LinesOfCodeMetric loc = metric as LinesOfCodeMetric;
loc.Severity = Severity.Hidden;
if (loc.Value >= 25 && loc.Value < 35)
loc.Severity = Severity.Warning;
if (loc.Value >= 35)
loc.Severity = Severity.Error;
}
}
}
}
Now build library and put it beside DefaultAnalyzer.dll. Now there are two analyzers. First checks maintainability index and cyclomatic complexity, second checks lines of code. You can remove DefaultAnalyzer.dll and create your own analyzer as you want.
Analyzer provides maintainability index, cyclomatic complexity and lines of code metrics but only maintainability index and cyclomatic complexity are checked by default. I will update it to provide more metrics as class coupling, lines of comments, number of parameters, etc...
- [2018-03-22] : There was null reference exception in some cases. This error was fixed.
- [2018-04-29] : Three new metrics has been added:
- Number of parameters
- Number of local variables
- Lines of comment
Default analyzer has been extended to check if there are members with empty body. If number of code is zero warning message is shown.