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...
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:
MetricCorresponding RuleThreshold
Depth of InheritanceCA1501 AvoidExcessiveInheritanceWarning at above 5 levels deep
ComplexityCA1502 AvoidExcessiveComplexityWarning at above 25
Maintainability Index
CA1505 AvoidUnmaintainableCodeWarning at below 20
Class CouplingCA1506 AvoidExcessiveClassCoupling
Warning at above 80 for class and above 30 for a method
In the policy editor this would look like the following:
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.