Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Code that we no longer use builds up. So what should one do when one doesn't need it anymore?

Deleting it feels wasteful, especially large chunks, and there's always the chance the exact same thing will be needed again later.

Commenting the code out is nice because it leaves it in place for later, but then the code can start to get cluttered.

Version control keeps everything clean and in the right place without the fear of deleting anything, but finding the right code in the version histories is often a project of its own. It's also not great practice to commit code that doesn't work just because you might need it later.

Q: Does anyone have a good solution to managing obsolete code?

share|improve this question

migrated from stackoverflow.com Sep 13 '11 at 15:22

This question came from our site for professional and enthusiast programmers.

1  
I agree with the others; keep it in version control. You don't want obsolete code hanging around, because later on, when you want to change something and you ask "what depends on this thing?", you don't want all your obsolete code showing up as a dependency that needs to be maintained. –  Vanessa MacDougal Sep 13 '11 at 15:03
2  
Not that I particularly care, but I find it interesting that you picked the worst voted answer of the lot. –  Craige Sep 13 '11 at 17:07
2  
Old code rarely do what you need. –  user1249 Sep 13 '11 at 17:25
3  
Commenting out code is NOT nice. It is a worst practice. –  JohnFx Sep 13 '11 at 18:55
2  
Deleting [code] feels wasteful? How long have you been developing? –  Jim G. Sep 13 '11 at 19:02
show 3 more comments

10 Answers

up vote 6 down vote accepted

I vote for one of two options:

Deleting it - you should be able to view previous versions in your VCS. However this doesn't give you the opportunity to search it.

Place it in a deprecated directory? (mine's called zz_deprecated_components) and then you can exclude it from your build and people only have to go and look at it if they are looking for something in particular.

In the case where commenting out might be more appropriate for a code snippet that you want to keep you could branch, or copy with history the file in question to the deprecated area.

share|improve this answer
add comment

Keep it in version control. If you think this code will be useful for something else, you can either tag the commit where it last existed, or it should be kept somewhere more available (as it is apparently not actually obsolete).

share|improve this answer
2  
+1 especially since.. YAGNI. –  Matthieu M. Sep 13 '11 at 17:32
    
You may want to have a corner with README.txt files listing discarded functionality and where to locate it. If you erase all tracks of it in the current source tree, noone will know where to look so nobody will. –  user1249 Sep 14 '11 at 9:14
add comment

Code that is obsolete, is first marked with the Obsolete attribute. After a while, it gets deleted from the code-base.

If -for some dark reason- you'll need the code later on, you can retrieve it from version-control. (You can label the version where you've deleted the obsolete code, so finding it back is a little bit less cumbersome).

share|improve this answer
1  
And if for some other reason you are not using version control, start using version control. –  Tom Sep 13 '11 at 17:34
add comment

It should be blown away from your current source files. Having commented-out clutter or worse, "unused" code, lingering in your projects imposes a cognitive obstacle course on whoever is working on it. This can really add up in 100+ KLOC sized projects.

If the obsolete code represents a truly major feature set, it will be remembered and folks will be motivated to dig it out of source control when it really matters. Otherwise, most developers actually enjoy starting a feature from scratch every once in a while.

share|improve this answer
add comment

I would create a separate project for the old code (in source control, obviously). This is hard to separate if the dead code is part of a monolithic project that isn't published to repository. That's why I am a fan of modularized, dependent projects continously publishing to a Ivy/Maven repository that is shared publically or across your organisation. If the obsolete code has dependencies on live code then this sort of set-up can help you slice off dead code into a separate project that is published to the repo. Maybe you'll forget about it or maybe you or someone else in your organisation will find a new use for it.

I believe this approach encourages good design with a natural separation of concerns. Having obsolete code bloating these projects unnecessarily increases complexity and decreases code readability for people coming to projects for the first time. As is commenting huge swathes of code, which I'm not a fan of either.

share|improve this answer
add comment

Here's possible things that can be done:

  1. move it to another library/project
  2. comment it out
  3. delete it and store it in version control
  4. keep it there, but ignore it / comment it from public interface
  5. don't do anything to it

When considering obsolete code, it's good to remember that any code change actually has a cost involved, and it might not be worth the effort to even comment it out.

share|improve this answer
add comment

Branch the code out of the current trunk. Delete the old code on the trunk. Optional: Create a second branch for re-use where the code is reviewed for repurposing elsewhere. Commit everything.

This only works for methods which are not public or previously marked depreciated. If public and still current, they need depreciation first. (Different topic.)

share|improve this answer
    
No need to branch. Just tag it. You can always branch off the tag later if you need to for some reason. –  Adam Jaskiewicz Sep 14 '11 at 16:39
add comment

Sounds like you need a better version control tool if you can't easily find what you need. Don't adopt bad practices (like commenting out code) to overcome limitations of the tool.

Keeping historical versions of code (including deleted code) is the job of the source control tool.

Deleting code that no one uses is NOT wasteful. It is obsolete because you found a better way to solve the problem. If that code was worthy of re-use it wouldn't be replaced.

share|improve this answer
add comment

Delete it from your code. Put a tag in your repository so you can find it later.

The more useless code you keep around the more likely you are to have bugs as a result of it.

share|improve this answer
add comment

I generally use c# for my code and what I do is I comment out the code and put them in #region tags. This way, you can minimize the code you aren't using so that It doesn't take up valuable screen space.

share|improve this answer
5  
Nooooooooooooooooooooooooooooooooooooooo –  Tom Sep 13 '11 at 17:33
1  
I would downvote this twice or more if I could. –  Matthew Rodatus Sep 13 '11 at 17:36
    
(continuing Tom's comment) oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo‌​ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo –  MetaFight Jan 7 at 22:35
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.