views:

826

answers:

18

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.

+56  A: 
  1. Check old project into Source Control.
  2. Delete unused code.
  3. Profit!!
John Gietzen
+1. Absolutely!
bloparod
you forgot step 3.1.) Check old project into Source Control.2.) Delete unused code.3.) ...4.) Profit!!
Caladain
+1, I love to delete code....yum!
kenny
@Caladain I beleive it should be 3.) ??? 4.) Profit!!
Thqr
+4  A: 

I believe deleting is a good way to keep the code cleaner.

If you dont delete, eg: comment out, or create obselete entities to store them, i have seen some mess like this.

and this is why you have repositories, branches, tags, etc..

I find it better to delete it. if the code is already in repository.

+14  A: 

I always delete unused code. This is one of the benefits of source control.

ChaosPandion
+1  A: 

Personally, I delete the code I don't use and rely on source control to keep track of it. I don't use VSS though and wouldn't.

There is one exception to this rule though ... Sometimes, if I deleted code in a spot where it's too tempting for somebody else to think it needs to be there, I leave it commented with a quick note so they don't redo my undone work.

Steve Sheldon
Do you mean leave it in there or delete?
CodeToGlory
Well, if he's using VSS then it's a crap shoot. VSS might just delete it for him. :/
Chris Lively
+4  A: 

If I have code I no longer use, I sell it on Craigslist.

Jen
:)) do you delete the code after you sell it? :))
+27  A: 
  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!

code4life
+1 for the obsolete part which is .net specific
davidsleeps
+1 for Obsolete tag. There are times i wish i was working in C#. :-(
Caladain
I would skip step 1 and 2 and the first part of step 3.
kenny
+1 for the obsolete thing! I'm on Delphi and we call obsolete code "deprecated" - so it's not just an c# / .net thing. I've just deprecated a few routines myself.
Cosmin Prund
Never knew about the obsolete attribute before - I do now :-)
BENBUN Coder
There is a parameter to the Obsolete attribute that will block compilation. So, I would suggest that for the second release.
John Gietzen
@John, good point!
code4life
Don't comment out the code. Check it into source control and delete it. The source control system will keep the old code.
chilltemp
Well, the commenting is really for communication purposes. I had a personal experience w/ one developer where we kept flip flopping one small block of code (couple of lines... and neither of us catching on) for a couple cycles. Then we realized maybe we should make put comments in to prevent these kinds of innocuous events...
code4life
+4  A: 

The best way to mark unused legacy code is to delete it. It serves no purpose other than to confuse new developers to the team.

In my old team we actually turned this into a competition we named the Grim Repear competition. Who could find and delete the largest amount of unused / dead code in the code base. Negative points were awarded for deleting dead code and positive points for adding dead code. Lowest score wins.

JaredPar
And what happens if they delete something that was still on life support? You know, that bit of code which is only important to one person in the basement who complains about his stapler? Is that like a +2?
Chris Lively
@Chris depends on which person is in the basement. If it's Milton then yeah that's a big penalty.
JaredPar
Delete the code and hide the stapler to keep him busy. +1 for Grim Reaper reward.
kenny
A: 

What if the code for some functionality which is not needed, but might be needed in future, is scattered throughout a program? Using #ifdef to control such code (keeping in one file a master list of all #define labels used for that purpose) may allow the feature to be enabled or disabled at will; trying to reincorporate all the little bits of code from a version-control system would seem more difficult.

supercat
That's kind of the point of using a source control system. If you need it in the future, just pull up the old code, copy out what you need, and put it back in.
Chris Lively
If all the code is in one neat section, that's certainly true. On the other hand, if re-enabling a feature would require grabbing and re-inserting code in many different places, I would think that using #ifdef's would be preferable as long as the code remained in a state where changing the #ifdef would result in something that actually worked. If stuff changed such that the #if'ed out code would require maintenance, then it's probably not worth keeping.
supercat
On a related note, what would you think about routines which form a "family", of which some but not all members are actually used (e.g. get unsigned byte from stream, get signed byte, unsigned word, signed word, etc.)? Even if an application doesn't use all versions, it would seem easier to write other code assuming all versions exist (even if they have to be #if'ed back in for things to actually compile) than deal with a hodge-podge of things that are available and unavailable.Note that my embedded-controller compiler includes unused functions in its generated code.
supercat
Doesn't doing this cause the following problems:1. Makes code really hard to follow 2. Invalidates the QA test plan when you flick a switch.I am pretty sure I would get my knuckles wrapped if I did this in any project I have ever worked on.
Steve Sheldon
+10  A: 

Take some advice from Michael Jackson:

Just delete it... delete it... delete it... delete it...

No one wants to be defeated

Showin' how funky and strong is your fight

It doesn't matter who's wrong or right

Just delete it... delete it... just delete it... delete it...

Alex M.
+1 for humorous content
Jeroen Huinink
+4  A: 

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.

Chinjoo
+2  A: 

A good place to apply a lesson from the book Pragmatic Programmer.

Backup and then delete all unused code. Trust me it will save developers from future headache. If you hesitate thinking you might need some future functionality based on commented out code, it's not even worth the pain of trying to understand unused code or logic. Better start with clean state of mind without the need to decipher obviously half-baked, untested code anyway.

pymendoza
+1  A: 

I think the best way is doing the version control of your project for example SVN, CVS. And then delete the unused code . To reduce the source file size being compile and avoid to get messy with unused code.

Russell Wong
+2  A: 

I prefer to delete it. Source control will keep the history of what was there (and who removed it). I hate searching code and seeing results in commented out code - especially when code is commented using /*...*/ which you can't see on in the find files result instead of \\ which at least gives me a chance of seeing that the result is commented out.

JLWarlow
+1  A: 

Delete unused code in your legacy application.

If you have source control deleting the unused code will not hurt anything because its history is keep in whatever version control software you are using. Clean code helps the next developer when he or she has to go in and make changes. Old code only confuses the issue when changes have to be made.

Chuckie
+1  A: 

I delete the code, but first I make sure that it won't cause any troubles later. If I need it later I get it from the source control and I use one nice feature - code compare and I can see what is what is changed. I don't comment it the unused code, because if other developer is looking at the code it is hard to understand why it is not used (especially if the developer doesn't put comment for the reason). So deleting the code will keep it simple and clear.

Teddy
+1  A: 

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.

Kragen
+1  A: 

I believe this was answered at the Refuctoring conference - http://www.waterfall2006.com when discussing the "Rainy Day Module" --you should write spare code just in case somebody needs it later...

class SpareCode {
  private int spareInteger;
  private String luckyString;
  private bool youNeverKnow ;
public void spareLogic() {
  spareInteger = 1;
  if ( youNeverKnow ) {
    spareInteger++;
  }
  System.out.println( luckyString);
}
geographika
A: 

I comment it out the same way I comment out my System.out.println's before going live.

GreenieMeanie