In a previous post, I explained how to use Code Analysis in Visual Studio 2010 to detect dead code. In that post, I described the following five Code Analysis rules in detail and how these set of rules form the basis for detecting dead code:

  • Private methods that are not called from any other code (CA1811)
  • Unused local variables (CA1804)
  • Unused private fields (CA1823)
  • Unused parameters (CA1801)
  • Internal classes that are not instantiated from any other code (CA1812)

To take this one step further, it would make sense to be able to treat this set of rules as one entity so I can manage all the rules for dead code detection as a single entity. In Visual Studio 2010, we have introduced the notion of a rule set that allows you to do exactly that.

What is a "rule set"?

Think of a rule set as a container or collection of rules. A rule set allows you to bundle one or more Code Analysis rules into an arbitrary category for easier management of those rules. For example, in Team System 2010, we are providing a number of rule sets out of the box:

  • Microsoft Security Rules
  • Microsoft Basic Correctness Rules
  • Microsoft Globalization Rules
  • etc.

How to create a rule set

For our walkthrough, we want to create a rule set called Dead Code Detection Rules that contains the five dead code detection rules mentioned above. The first step in creating a rule set is to create a new rule set file (*.ruleset). You can create a new rule set file via File –> New –> File. In this example, I've given the rule set the name DeadCodeDetectionRules.ruleset, as shown below.

New file dialog

After creating the rule set, set the name and description of the rule set using the Properties window, as shown in the following screen shot.

Rule set Properties window

Since the rule set doesn't contain any rules yet, the rule set editor shows none of the rules as being checked. The rule set editor (shown below) is a new window in Visual Studio Team System 2010 that allows you manage rules and rule sets.

Dead Code Rules rule set

To start adding rules to the DeadCodeDetectionRules rule set, you can search for a rule using either the rule number or its name, as shown below. You can also simply expand the rule categories and select the rules that you are interested in.

Search in Rule set editor

After adding all five dead code detection rules, the rule set editor should look something like this:

Rule set editor with added rules

Applying a rule set to a project

Once the rule set has been created, the next step is to apply the rule set to a project. This can be done via the Project Properties window. On the Code Analysis tab, click the dropdown next to the [Open] button and select "Browse..." to browse to the location of the rule set file. After selecting the rule set, the name and description of the rule set are shown in the summary page.

Applying a rule set.

So now, all the dead code detection rules are part of a single rule set which make it much easier to manage, modify and apply the rules at both the project level as well as TFS policy level.

Habib Heydarian.