Enforcing Code Metric Policies on Check-in

So I have had at least a half dozen customers ask about this recently, and found a great article on the Code Analysis Team blog.  Yoinked from their post...

Technorati Tags: Code Metrics,Check-in Policy,TFS 2008

Can a check-in policy be defined based on code metrics in TFS?

Well, code metrics itself does not have a check-in policy but fortunately the Maintainability Index, Class Coupling, Depth of Inheritance and Complexity metrics are all available as rules in Code Analysis in Visual Studio.  This means you can use code analysis check-in policy enabled with those rules.  The rules are grouped under the "Maintainability Rules" Category and are configured as follows:

Metric
Corresponding Rule
Threshold

Depth of Inheritance
CA1501 AvoidExcessiveInheritance
Warning at above 5 levels deep

Complexity
CA1502 AvoidExcessiveComplexity
Warning at above 25

Maintainability Index

CA1505 AvoidUnmaintainableCode
Warning at below 20

Class Coupling
CA1506 AvoidExcessiveClassCoupling

Warning at above 80 for class and above 30 for a method

In the policy editor this would look like the following:

Code Metrics as a check-in policy

This allows you to enforce that code meets these thresholds before check-in.  In future we are looking in to allowing the threshold values to be changed.