There are times when you find dependencies of some legacy code in a method that is never used. I personally always have a feeling that one day I need to find a way to search for all these unused methods and just get rid of them. Here I want to show you how to do it with Resharper.
There is a nice inspection tool built into Resharper that can show us different code issues found in the solution. Among them we have a metric called “Type or type member is never used”. By default it presents private types and type members (methods, fields, properties, etc) that are never used. To make R# show us also public (or in general ‘non-private’) types and members you need to enable “Solution-Wide Analysis”.
1. Go to bottom-right corner of visual studio, right click the circle and click “Analyze Errors in Solution”
2. You should see a message that solution-wide analysis could take a while, but don’t be scared, for me analysis of a big project (more than 60 000 statements) took less than 2 minutes.
3. Go to “Resharper –> Inspect –> Code issues in Solution” (or current project if you wish).
4. After code analysis it should show the inspection result. To filter only the unused methods (type and type members) click “Filter issues” button.
5. In the “Filter Issues” window “Uncheck All” options and then go to “Redundancies in Symbol Declarations” and check “Type or type member is never used” (here you can specify that you want to see only private or non-private stuff).
6. After that you can group the report by some additional criteria – I prefer project structure. From now on you can click each and every issue and do some actions on it (mainly “Remove unused …”.
Although we have a full list of unused types and type members, we cannot bulk remove the issues and forget about them. Some of the issues should be treated specially because they can be used in ASP.NET markup, WCF communication, serialization and any other way that cannot be figure out by Resharper. All in all it’s good that unused interface implementations will not be taken as issues.
Happy refactoring. And don’t forget to turn off Solution-Wide Analysis.