In Visual Studio 2008, how can I easily find unused code in a source file?
I've got a source file with 2800+ lines of code in it, and doing an individual 'find all references' would get a bit tedious.
In Visual Studio 2008, how can I easily find unused code in a source file?
I've got a source file with 2800+ lines of code in it, and doing an individual 'find all references' would get a bit tedious.
If you were using resharper (for c#) it would hilight what you did not use (private mehtods)
Perhaps Resharper could help. When you have a method which is never used, or a variable that is only declared and assigned, but never used, they're greyed out.
Next to that, you could also use Fxcop do to a static code analysis. It will find such methods and variables as well.
Resharper is the way to go, but as far as built in mechanisms, there are two that will help you along a little bit:
1) VS 2008 can help by removing unused Using statements
2) You can use the [Obsolete] attribute to mark obsolete functions. If they are truly no longer used, you won't see a compiler warning and then you'll know its safe to delete.
[Obsolete]
public void MyOldCSharpFunction() { }
FxCop (built into vs.net 2008) has rules to find uncalled private or internal methods, uninstaniated internal types, unused member variables, unused paramters and unused local variables. It doesn't apply to any methods visible outside a single assembly though.
I didn't see it mentioned here, so I'll include a link to it:
NDepend looks like it's a helpful tool for this.
The tool NDepend can help you to find unused code. You can edit the following 3 Code query Language (CQL) queries to detect unused methods, fields and types. These 3 queries can be customized to your exact need. Notice also how these queries are prefixed with WARN IF Count > 0 IN, that transform them from CQL queries to CQL rules. These CQL rules can be checked in your CI environment, or even, interactively in VS itself while you are coding:
// <Name>Potentially unused methods</Name>
WARN IF Count > 0 IN SELECT METHODS WHERE
MethodCa == 0 AND // Ca=0 -> No Afferent Coupling -> The method is not used in the context of this application.
!IsPublic AND // Public methods might be used by client applications of your assemblies.
!IsEntryPoint AND // Main() method is not used by-design.
!IsExplicitInterfaceImpl AND // The IL code never explicitely calls explicit interface methods implementation.
!IsClassConstructor AND // The IL code never explicitely calls class constructors.
!IsFinalizer AND // The IL code never explicitely calls finalizers.
!IsVirtual AND
!IsEventAdder AND
!IsEventRemover
// <Name>Potentially unused types</Name>
WARN IF Count > 0 IN SELECT TYPES WHERE
TypeCa == 0 AND // Ca=0 -> No Afferent Coupling -> The type
// is not used in the context of this application.
!IsPublic AND // Public types might be used by client applications of
// your assemblies.
!NameIs "Program"
// <Name>Potentially unused fields</Name>
WARN IF Count > 0 IN SELECT FIELDS
WHERE
FieldCa == 0 AND // Ca=0 -> No Afferent Coupling -> The field is not used in the context of this application.
!IsPublic AND // Although not recommended, public fields might be used by client applications of your assemblies.
!IsLiteral AND // The IL code never explicitely uses literal fields.
!IsEnumValue AND // The IL code never explicitely uses enumeration value.
!NameIs "value__" // Field named 'value__' are relative to enumerations and the IL code never explicitely uses them."