Alex Rivera | Logout

What do you do with unused code in your legacy applications?

Asked 2010-06-25T02:38:11.473
19

On huge legacy applications it is fairly common to see change in business rules leading to unused code. Is the deleting the best way? or Are there any standards of marking the unused code? SCM does help to get the old code back if needed. Also this is specific to .NET code bases.

Edit
Report

3 Answers

37
  1. Mark it obsolete. http://msdn.microsoft.com/en-us/library/aa664623%28VS.71%29.aspx
  2. Next cycle, comment the code out & make appropriate remarks. Set a region around the code block so that it collapses nicely & doesn't eat up a lot of visual space.
  3. Next cycle, delete the code.

This way you don't surprise your teammates too badly if you need to deprecate some code that you're assigned to but which they need to make calls to. But allows you to make those needed changes!

answered 2010-06-25T02:48:49.060
5

I usually mark that obsolete:

    /// <summary>
    /// Purpose of this method
    /// </summary>
    /// <param name="args">Argument 1</param>
    [Obsolete("This method is obsolete, use NewMethod instead")]
    public void SampleMethod(string args)
    {
        //code
    }

    /// <summary>
    /// Purpose of this method
    /// </summary>
    /// <param name="args">Argument 1</param>
    public void NewMethod(string args)
    {
        //code
    }

Now the compiler will provide a warning whenever the method marked as Obsolete is used.

answered 2010-06-25T05:33:58.883
2

Definitely delete unused code. In my experience the biggest problems with legacy codebases is understanding whats going on code - less code means less to understand, making your job easier.

Like you say you can always get it back if you are using source control (you are using source control, right?) and unless you are doing anything a little odd (like dynamically compiling code or loading code through reflection), you are probably going to find out pretty quickly if you deleted the wrong code as your build will fail.

answered 2010-07-01T05:04:55.487

Your Answer