Friday, February 17, 2012

“Dead code”, a very common code smell.

Remove dead code from you code files

Today we are going to take a look at a simple code smell named “Dead code”. You would be surprised to know how many dead code exists in your project when you take a close look at the project file. of course you wouldn’t know just by looking at it. some of them are pretty hard to find.

What is dead code?

The code that is no longer needed and no longer used by any function or procedure is a dead code.

The Tool

The easiest way to identify the dead code is to use a tool that will save thousands of milliseconds by providing some indication that the code is no longer used. And I am talking about none other than Resharper, the awesome tool that solve lot of our problem and yet so cheap!. Of course I am talking about only Microsoft Platform, eclipse has its owe set of cool feature that have all the refactor and code notation. But if you are a “IntelliJ IDEA” user, then probably you have all the necessary help you need.

Simple dead code

The bellow screen shot shows some portion of the code file and the red rectangles are dead code. that’s the simple version I would say. If you have Resharper plugin with your VS2010 IDE this tool would grayed out the dead to let user know what are the dead codes.


Here is possible things that can be dead code.

  • private variables
  • private methods
  • private properties
  • redundant quantifier
  • redundant directives
  • public methods
  • public properties
  • Even a class
  • Unused method parameter
  • Unused local variable

Remove dead code Techniques

#1 :  Use the full cleanup .

Just right click on your code file and select full cleanup from context menu. This would remove lots of problem from your code.



Yet you have some dead code after full clean up. you can follow the bar of VS code window and click imageon each item to remove the problems. to remove the dead field or method just put the cursor on the gray text and then hit key ctrl+Enter this will pop up a tool window with some option menu with it. Select Remove unused method to get rid of the method.



#2 : Find the uses of the method

This is a hard part and can lead to a chain where you would get rid of a couple of methods Tree. How do you quickly identify that the procedure is unused. Its pretty easy to identify the private methods and fields as its been grayed out by the reharper. but what about public method and public properties or fields.

Generally when you get rid of a dead code for instance a private method, any method that is been used in that private method also become dead if the method is not been used some where else, luckily reharper automatically detects it and gray out the methods in turn.

Now lets talk about public methods, first check if the method has a reference or not. To do that simply right click on a method and select “find usages” if it shows that it does not have any uses, you can probably get rid of the public method,field and property.


One easy way to use the modifier, change the modifier from public to private. if you don’t see any compile error it means this method would be grayed out. so no uses. But this fails in case of WCF Service interfaces. Since the service is been uses some where else and all methods that we want to expose needs to be public. But that is the whole idea of a WCF service, so we would expose only necessary methods That would be used.

After find Usages if any usages found it would be listed on a tool window. Just click on the item. It would take the user to the method or line of code where its been used. Check again if that method has any Usage. And so on , I think you got the idea what needs to be done here to identify where is the dead code.


A common problem of our code is that is the class is been used anywhere? One simple technique is simply exclude the class from the project and then build the project see if it creates any compile error or not. but we can also use find usage technique with the class file also, to find if its been used some where or not.

Always use “Safe Delete”

While removing class or methods from project always use safe delete operation of reharper, this would help us not to create unnecessary problem for our codes. To use safe delete simple right click on the object you want to delete, i.e. parameter, variable , method and class. Then select Safe Delete from context menu, or you can also use Ctrl+R, D to lunch the safe delete window.


When the window pop’s up you can also check enable undo if you don’t feel safe. Then click next to get rid of the code. If the code that we ware trying to delete has some reference some where it would not perform the operation rather it would show a error window showing where the usage is.


There are many more thing to discuss about dead code. And couldn’t be completed in a single post. I would try to put together different platform in future.

No comments:

Post a Comment