8

We have a 10+ years old ASP.NET website project, with lots of unused / legacy user controls and pages.

Is there a tool which can assist in the process of identifying these elements of the solution, so we could refactor them out?

I know the issue is complicated because not all types are referenced as types which a compiler / tool might recognize. e.g. objects instantiated by reflection are instantiated using a string representing the class name. In addition asp.net pages are generally not referenced as types, but as page identifier strings in markup.

It still seems like someone would have created a tool which handles these cases and creates a candidate list of unused classes / pages / user controls

Matt Evans
  • 7,113
  • 7
  • 32
  • 64

2 Answers2

2

You may use this regex to find unused registered controls

<%@\s*Register\s+TagPrefix\s*=\s*"(?<tagprefix>[^"]+)"\s+TagName\s*=\s*"(?<tagname>[^"]+)"\s+Src\s*=\s*"[^"]+"\s*%>(?!.*?\k<tagprefix>:\k<tagname>\s+)

I used powergrep to run this regex. The "Dot matches newline" checkbox should be enabled to work properly.

vicneanschi
  • 468
  • 1
  • 4
  • 13
  • Looks interesting. Can you describe what it does? – Matt Evans May 03 '12 at 13:35
  • Sure. The first part looks for controls registration template <%@ Register TagPrefix="Myprefix" TagName="Mytag" Src="~/mypath.ascx" %> and collects tagprefix tagname in the named groups **(?blabla>)**. The second part of regex uses negative lookahead **(?!blabla)** So we can read this regex as: please find all controls registration that are not used later in the text. Our project is also 10+ years old :) – vicneanschi May 04 '12 at 10:20
  • I used SublimeText to execute this. I had to add an inline flag to the very beginning of the regex to dot-match newlines: `(?s)`. See http://stackoverflow.com/questions/11992596/regex-in-sublime-text-match-any-character-including-newlines – Scott Stafford Nov 20 '14 at 15:15
2

ReSharper is a refactoring tool which might help you to identify clouds of unused classes and methods. It can also delete them safely. It allows you to delete a method or class and adjust all it's usages. See: Safe delete

It has also appropriate support of ASP.NET. See: ASP.NET support

George Mamaladze
  • 7,593
  • 2
  • 36
  • 52
  • Thanks - we already use Resharper, but I don't think it can identify unused pages and user controls. – Matt Evans Nov 29 '11 at 13:27
  • Have you tried [find usages](http://www.jetbrains.com/resharper/webhelp/Navigation_and_Search__Finding_Usages.html) functionality. I am sure you can use it to find at least unused user controls. – George Mamaladze Nov 29 '11 at 13:30
  • yes - this is ok for individual namespaces, and methods, types on a manual case-by-case basis, but I'm hoping to find a tool which can generate a solution wide report – Matt Evans Nov 29 '11 at 15:00
  • ok. you can do it also using Resharper see Q&A [Find unused code](http://stackoverflow.com/questions/245963/find-unused-code) – George Mamaladze Nov 29 '11 at 15:13