How to: Enforce Maintainable Code with Code Analysis Check-in Policy

Developers can use Code Metrics to measure the complexity and maintainability of their code but they cannot invoke Code Metrics as part of a check-In policy. However, they can enable Code Analysis rules that verify the compliance of their code with Code Metrics standards and enforce the rules through check-in policies. For more information about Code Metrics, see the Code Metrics Values.

Depth of Inheritance, Class Coupling, Maintainability Index and Complexity are the rules that developers can enable to enforce maintainable code through Code Analysis check-in policies. All four of these rules are found under the "Maintainability Rules" category in the Code Analysis policy editor.

Administrators of Team Foundation version control can add the Code Analysis Maintainability Rules to the check-in policy requirements. These check-in policies require the user to run Code Analysis based on these rule changes before initiating a check-in.

To open the Code Analysis Policy Editor

  1. In Team Explorer, right-click your Team project, click Team Project Settings, and then click Source Control.

    The Source Control Dialog box appears.

  2. Click the Check-in Policy tab and then click Add.

    The Add Check-in Policy dialog box appears.

  3. In the Check-in Policy list, select the policy Code Analysis then click OK.

  4. The Code Analysis Policy Editor dialog box appears.

To enable Code Analysis Maintainability Rules

  1. Expand the Maintainability Rules Node under the Rule Settings section of the Code Analysis Policy Editor dialog box.

  2. Check the following rules:

    • Depth of Inheritance: CA1501 AvoidExcessiveInheritance - Threshold: Warning at above 5 levels deep

    • Complexity: CA1502 AvoidExcessiveComplexity - Threshold: Warning at above 25

    • Maintainability Index: CA1505 AvoidUnmaintainableCode - Threshold: Warning at below 20

    • Class Coupling: CA1506 AvoidExcessiveClassCoupling - Threshold: Warning at above 80 for class and above 30 for a method

    • In addition, if you want a rule violation to prevent a build, select the check box Treat Warning As An Error next to the rule description.

  3. Select OK. The new check-in policy now displays with future check-ins.

See Also

Concepts

Code Metrics Values

Other Resources

Code Analysis Check-in Policies