.NET find dead code across multiple solutions
Asked Answered
S

3

8

We have a product with ~15 solutions with each a number of projects.

The question is quite simple: Which tool will enable us to search the entire codebase for dead code?

Searching within a single solution is easy enough (Lots of answers on SO for that one).

But what about determining if "public void Foo()" in project AlphaProj of solution AlphaSol ,which is not used within AlphaSol itself, is actually used in e.g. BetaSol ?

Sophocles answered 25/5, 2011 at 8:26 Comment(0)
P
4

Although you have 15 solutions nothing stops you from creating another solution that will have all the projects referenced, let's say All.sln. So whenever you need to find external references you open this All.sln solution and look for the references.

We probably have about 100 solutions, and one All.sln that references all the projects from those solutions. It is easy to add all the projects from one solution to All.sln: you just select Add Existing Projects and select one of those 15 solution files. You need adjust file type in Add Existing Projects dialog to be able to select solution file. Also, to keep this big solution organized, you can use solution folders.

Patin answered 25/5, 2011 at 8:31 Comment(0)
C
1

You might also want to checkout NDepend.

Since the other answer is quiet sufficient for your immediate need, and I don't want to sound like a bad commercial, I leave it to the interested reader to find out more about that tool.

Clutch answered 25/5, 2011 at 10:30 Comment(0)
D
0

To elaborate a bit on the answer of Christian, the tool NDepend can indeed help find unused code in a .NET code base. Disclaimer: I am one of the developer of this tool.

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:

NDepend is integrated in Visual Studio, 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.

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.

Durazzo answered 5/7, 2011 at 8:31 Comment(1)
Can it detect method references across interfaces?Neuropsychiatry

© 2022 - 2024 — McMap. All rights reserved.