Find Dead Code

Dead code means block of code that is not reachable in application. They can cause noisy in code debugging and confuse developers. Sometimes I might spend a few hours to figure out why a breakpoint never hit.

Most of code defect is result of old function no longer used and referenced. Remove them will prevent developers from wasting time investigating wrong code. Thanks to FxCop in visual studio, now finding dead code is pretty straight forward.

1. Open application project and in project properties, click Code Analysis tab. Change Rule Set to Microsoft All Rules as in screenshot.

image

2. Click Menu Analyze –> Run Code Analysis

image

3. Visual Studio will rebuild projects. FxCop will detect all kind of dead code in build time and report them as following 5 type of warnings. Double click the warning will bring your to the source.

  • Private methods that are not called from any other code (CA1811)
  • Unused local variables (CA1804)
  • Unused private fields (CA1823)
  • Unused parameters (CA1801)
  • Internal classes that are not instantiated from any other code (CA1812)

image

Now have fun getting dead code removed.


Comments

21 responses to “Find Dead Code”

Leave a Reply