Detect dead code in C#
Asked Answered
G

4

11

How can I detect dead code in my C# application?

Gove answered 3/12, 2010 at 18:18 Comment(4)
Do you use Resharper? )Virtu
Follow the smells.Adulteress
@The_Smallest:I d'nt use ResharperGove
There is smell everywhere...Oestrone
R
7

Compile your code and check the warnings in the Error List. The following code:

    public ActionResult Index() {
        ViewData["Message"] = "Welcome to ASP.NET MVC!";
        return View();
        return null;  // unreachable
    }

produces this warning:

Warning 11  Unreachable code detected   <fullpath>\HomeController.cs    13  13  <prjname>

Tools like JetBrains ReSharper (http://jetbrains.com/resharper)* can also perform this analysis on the fly and highlight dead code.

* ReSharper is a commercial tool.

Romberg answered 3/12, 2010 at 18:24 Comment(2)
You can set Visual Studio to treat warnings as errors. Project Properties... Build... Treat warnings as errors... Specific warnings: 0162. Then any dead code will result in a compiler error, which you can easily browse to. (I'm assuming that you consider VS free and/or C# Express supports this. I haven't checked.) I don't know of a free VS add-in that will highlight dead code.Romberg
Visual Studio 2010 and the Microsoft C# compiler reliably highlight unreachable code within methods. They ignore unused private methods though.Preglacial
B
9

ReSharper can handle that. You could also check out NDepend.

If you don't feel like paying for either of those, I believe you can analyze your project with FxCop and it will also identify dead code.

Bilingual answered 3/12, 2010 at 18:21 Comment(0)
R
7

Compile your code and check the warnings in the Error List. The following code:

    public ActionResult Index() {
        ViewData["Message"] = "Welcome to ASP.NET MVC!";
        return View();
        return null;  // unreachable
    }

produces this warning:

Warning 11  Unreachable code detected   <fullpath>\HomeController.cs    13  13  <prjname>

Tools like JetBrains ReSharper (http://jetbrains.com/resharper)* can also perform this analysis on the fly and highlight dead code.

* ReSharper is a commercial tool.

Romberg answered 3/12, 2010 at 18:24 Comment(2)
You can set Visual Studio to treat warnings as errors. Project Properties... Build... Treat warnings as errors... Specific warnings: 0162. Then any dead code will result in a compiler error, which you can easily browse to. (I'm assuming that you consider VS free and/or C# Express supports this. I haven't checked.) I don't know of a free VS add-in that will highlight dead code.Romberg
Visual Studio 2010 and the Microsoft C# compiler reliably highlight unreachable code within methods. They ignore unused private methods though.Preglacial
R
2

Resharper identifies dead code and unused parameters/locals and so does FxCop.

Rickyrico answered 3/12, 2010 at 18:21 Comment(0)
M
0

Mind that these tools do not detect the dead code in the comments. For example, the following:

// var a = 123;
// DoSomething(a);

will not be detected as dead code.

As of July 2020, I could not find any code inspection tool that could detect dead code in the comments. Therefore I developed one on my own (based on Roslyn) and published it under MIT license: https://github.com/mristin/dead-csharp.

Mlle answered 23/7, 2020 at 21:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.