views:

5917

answers:

8

Are there any good tools or tricks for determining if there are any referenced but unused dependencies (such as dlls) in a project?

My specific case is C# .net3.5.

+2  A: 

Well a duct-tape solution would be to remove the reference and attempt to compile.

Quintin Robinson
That was always my plan of attack too, but in some case the project size/build time gets too extreme for this method.
Timothy Carter
+1  A: 

NDepend may help you here:

http://ndepend.com/

Dan
+4  A: 

Isn't this functionality built right into Visual Studio? Project Properties -> References -> Find Unused?

Nicholas H
I think that only works with VS2005 and above. (But that's certainly the case if you're using .Net 3.5)
Tadmas
As far as I can tell this only works in Visual Basic projects. Not C#.
Lawrence Johnston
Sorry, I should've noticed that. Yeah, VB only. I wish they would make stuff equivalent, sheesh.
Nicholas H
+7  A: 

I would recommend ReSharper from JetBrains. It will first help you to see what using statements are not necessary in your code and you can also find out what references are not used in your project (Find Usage and if the result is none, then you know the reference is not necessary). I have to admit that this part is manual, but of the other code cleaning actions can be automated using code formatting.

David Pokluda
ReSharper works great for this! Just watch out for extension methods which require System.Core.dll. ReSharper (at the moment) doesn't recognize this, so (unless something else references it) it tells you that you can remove it. But if you do, and you have some extension methods around, it won't build.
Svish
True, ReSharper finds code dependent on a referenced assembly, and if there's no such code, you're free to delete the reference altogether. However, you can vote for a feature request at http://youtrack.jetbrains.net/issue/RSRP-6312 to streamline this kind of inspection and make it more automated.
gorohoroh
+5  A: 

Yes you can use NDepend for this because the tool comes with the Afferent Coupling metric for Assemblies, Namespaces, Types, Methods and Fields. For example, the Afferent Coupling for a particular method is the number of methods that depends directly on it.

Thus code elements with Afferent Coupling set to 0 are likely not used. I say likely because it is mathematically not possible to statically know which code element is used or not at runtime. But you can use the flexibility of Code Query Language like for example asking for method with no Afferent Coupling and that are not the Main() method (Entry Point), a static ctor or a finalizer.

// <Name>Potentially unused methods</Name>
WARN IF Count > 0 IN SELECT METHODS WHERE
  MethodCa == 0 AND            // Afferent Coupling is 0
  !IsEntryPoint AND            // Main() method is not used by-design.
  !IsClassConstructor AND      // The IL code never explicitely calls class constructors.<br>
  !IsFinalizer                 // The IL code never explicitely calls finalizers.
Patrick Smacchia - NDepend dev
I love NDepend and, yes, it is the right tool for the job. However, I think that the question was how you would go about identifying individual references that can be deleted. Merely looking at afferent coupling metric of assembly C will not show that B has a redundant reference to C as long as D is using C. Or did I misunderstand either of you?
Tormod
+5  A: 

ReSharper helped! Context menu of the reference -> "Find dependent Code". The nice thing is that Resharper also shows you the places where you use the specific reference (in a nice treeview). Anyways, it would be nice to have a cleanup function as there is for VB users built in visual studio.

uli78
+2  A: 

Removing unused references is a feature Visual Studio 2008 already supports. Unfortunately, only for VB .NET projects.

I have opened a suggestion on Microsoft Connect to get this feature for C# projects too:

http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=510326

If you like this feature as well then you might vote my suggestion.

jbe
+1  A: 

It's lame that VS doesn't support this for C#, and that seems to be the case in 2010 too. Would have voted up jbe's suggestion, but the link is broken. Also, I dislike ReSharper so that isn't an option for me.

Anyway, my method has been to:

  • open the DLL in question inside Reflector
  • expand the references node
  • remove any references present in VS that aren't present in Reflector

This works because a reference in VS is just a hint to the compiler that you might need that DLL. If you don't actually use any types in the referenced assembly, the compiler won't include the reference in the assembly's metadata. Thus, Reflector will report on those assemblies that you actually refer to in IL.

Of course, you might be reflectively using assemblies, or you might have added the reference in VS just to get the DLL copied to the same directory as the referencing assembly. Or you may have added the reference because the compiler told you it needed it because you use a dependent assembly. Keep these issues in mind, don't blindly delete references, and be sure to regularly rebuild each project as you go.

HTH,
Kent

Kent Boogaart