Removing unused code in Visual Studio

2020-03-08 07:15发布

In relation to this question: "Remove unused references (!= "using")", I would like to know if there is a tool for removing unused classes, structs, delegates, etc from a Visual Studio solution.

Scenario:

I have an unorganised Visual Studio Solution which consists of 1000's of:

  • Native method imports
  • Structures
  • Delegates
  • Enumerations

Rather than trawling through each file clicking "Find All References" and determining if the code is being used somewhere, is there any mechanism by where I can simply remove redundant code files easily?

Example:

//This class contains a method called getRandomValue which returns type RANDOM
public class NativeMethods
{
    [DllImport("random.dll")]
    public static extern RANDOM getRandomValue();
}

//This is the RANDOM object as referenced by getRandomValue();
[StructLayout(LayoutKind.Sequential)]
public struct RANDOM
{
    uint a;
    uint b;
    uint c;
}

//This is redundant since nothing is referencing it.
[StructLayout(LayoutKind.Sequential)]
public struct MESSAGE
{
    IntPtr sender;
    IntPtr recipient;
    char[] mText;
}

Note to self:

My gut feeling is that this is going to be tricky since unlike Java, object names do not have to be identical to the file name, and multiple object declarations can reside within a single file, however in this instance (my scenario) every object is declared within its own file (with an identical name).

3条回答
何必那么认真
2楼-- · 2020-03-08 07:55

There are several tools that you can use to do this:

FxCop will only find unused internal and private code. Of course if you make sure you only publicly expose code that needs to be accessible outside your assembly, then that should be good enbough.

查看更多
Rolldiameter
3楼-- · 2020-03-08 08:04

ReSharper is the best choice to clean up your code.

You can use it for free thanks to ReSharper Early Access Program.

enter image description here

查看更多
Luminary・发光体
4楼-- · 2020-03-08 08:05

As pointed @Ergwun the tool NDepend can help to find unused methods, fields and types.

To elaborate a bit, NDepend proposes to write Code Rule over LINQ Query (CQLinq). Around 200 default code rules are proposed, 3 of them being dedicated to unused/dead code detection

Basically such a rule to detect unused method for example looks like:

// <Name>Dead Methods</Name>
warnif count > 0 
from m in Application.Methods where !m.MethodsCallingMe.Any()
select m

NDepend rule to find unused methods (dead methods)

But this rule is naive and will return trivial false positives. There are many situations where a method is never called yet it is not unused (entry point, class constructor, finaliser...) this is why the 3 default rules are more elaborated:

NDepend integrates in Visual Studio 2017,2015, 2013, 2012, 2010, thus these rules can be checked/browsed/edited right inside the IDE. The tool can also be integrated into your CI process and it can build reports that will show rules violated and culprit code elements. NDepend has also a VS Team Services extension.

If you click these 3 links above toward the source code of these rules, you'll see that the ones concerning types and methods are a bit complex. This is because they detect not only unused types and methods, but also types and methods used only by unused dead types and methods (recursive).

This is static analysis, hence the prefix Potentially in the rule names. If a code element is used only through reflection, these rules might consider it as unused which is not the case.

In addition to using these 3 rules, I'd advise measuring code coverage by tests and striving for having full coverage. Often, you'll see that code that cannot be covered by tests, is actually unused/dead code that can be safely discarded. This is especially useful in complex algorithms where it is not clear if a branch of code is reachable or not.

Disclaimer: I work for NDepend.

查看更多
登录 后发表回答