Eliminate unused or Dead code using Visual Studio Code Analysis

Eliminate unused or Dead code using Visual Studio Code Analysis

Dead code

It could be inaccessible code, excess code, and unused code. By using Code analysis feature of Visual studio we can focus the dead code.

Why to eliminate dead code?

1.   Sometime we waste part of time deduction why the breakpoint not hitting method/class.
2.   To increase the code coverage result.
3.    Code maintainability
4.   Improve performance



What are those Rules?

•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).


Create a new rule set file via File –> New –> File. In this example, I've given the rule set the name DeadCodeDetectionRules.ruleset

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.

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.

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

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.

now run the code analysis for the solution and in the result windows you would be able to see the unused codes.

comments powered by Disqus

Related Articles