Removing unused code in Visual Studio [duplicate]
Asked Answered
R

3

32

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

Replenish answered 8/10, 2012 at 23:23 Comment(1)
You can think of using ReSharper. It provides great supports for code cleanup.Uzbek
J
17

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

Jumbled answered 9/10, 2012 at 1:34 Comment(4)
I tried Resharper overnight (as it said it was going to take over 5 hours) but for some reason I when looked at it in the morning it seemed to have crashed and failed to clean my code, however I will investigate this further. Thanks! +1 for effort.Replenish
In latest version of Resharper(7.1.3), you can analyze referneces directly by going ReSharper -> Find -> Optimize ReferencesAbednego
@BillYang 7.1.3 is not the latest version now. but anyway it's good to know!Jumbled
It's not free and especially for a poor country it costs quite a lot.Crosslegged
P
13

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.

Philtre answered 8/10, 2012 at 23:40 Comment(1)
I tried Resharper. It said it was going to take over 5 hours to perform a code cleanup. I am not sure if it is supposed to take that long. I guess it depends on the size of the project...anyway it crashed at some point during the five hours so I may need to do it again!...but thanks...+1 for effort!Replenish
J
1

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 2022, 2019, 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.

Jackhammer answered 13/6, 2017 at 8:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.