Clean up unwanted code C#

2019-02-13 16:03发布

I got a C# application with Visual Studio 2005. The code has some 300 KLOC, which has been abused over 3 years.

When I was going through code found out that lot of unused functions/methods/properties. Its not possible to clean the code manually (Requires checking each member and if found no references delete it.)

I am looking to automate this process, by VS macro, which will walk through each member in code, if it does not have any references it should delete it, if found any references, check its calling member's references if calling member, does not have any reference it should delete both and so on.

I am sure some one would have cracked it earlier.

5条回答
The star\"
2楼-- · 2019-02-13 16:11

Be careful of code that is invoked via reflection. A lot of refactoring tools will flag this code as not being accessed when in fact they are.

The safest is to run your unit tests (you do have these already right?) before and after the refactoring to ensure that everything still works.

查看更多
Luminary・发光体
3楼-- · 2019-02-13 16:12

1st use re-sharper as other have suggested.

Resharper assumes public methods are used somewhere by an external assembly, even if they are not, so search and replace "public " with "private " and recompile.

查看更多
够拽才男人
4楼-- · 2019-02-13 16:22

Resharper has a Clean Code function and gives pretty good indication of which methods/classes aren't being used.

查看更多
贪生不怕死
5楼-- · 2019-02-13 16:28

I have used Gendarme (like FxCop) to determine which code wasn't called. AFAIK it can't be automated, but at least you don't need to go line by line.

查看更多
地球回转人心会变
6楼-- · 2019-02-13 16:34

You can query your code base with NDepend using CQL to find out which methods and classes are not being used.

查看更多
登录 后发表回答