Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best Practices for Removing Unused Code

Tags:

c#

I'd like to know what people's best practices are for removing unused code. Personally I'm a fan of deleting (not just commenting) anything that's not currently being used. But I'm unsure of how far to go.

Take this as an example (although I'm interested in general discussion). In my project I have a dozen or so UserControls. For a feature that later got canned, I implemented a couple of methods and properties on one of the UserControls. The additional code is not specific to the feature, but needed to support it. It may potentially be useful later on.

  • Should I remove the code because we're not using it at the moment, and the less code there is the easier it is to read? The problem with this is, how do future developers know that this work has already been done?
  • Or should I keep it there, so another developer can find it easily if they need to use it later (they're not going to think to go through source control to see if anyone had done this and deleted it)?
  • Or is there another option?

The same applies to UserControls not currently being used. Should I remove them or keep them?

Edit: It goes without saying (or I thought it would) that we're using source control.

like image 938
Ray Avatar asked Feb 23 '09 22:02

Ray


People also ask

Should unused code be removed?

“You should be constantly looking to improve your codebase, including removing dead code.” “If you have those processes in place, you should be constantly looking to improve your codebase, including removing dead code,” he said.

Which removes inefficiencies and improves code?

Refactoring is a systematic process of improving code without creating new functionality that can transform a mess into clean code and simple design.

Does linker remove unused code?

The Compiler takes this feedback file from the prior build and uses it to place unused functions into their own ELF section in the object file. Then linker can place them in the unused sections and removes them from build.


3 Answers

The first thing to remember is all your code should be in source control.

With that in mind, of course you want to delete obsolete code rather than just comment it out. Commented code blocks of any length are dangerous, for at least two reasons:

  1. There's a tendency to assume that the comments were maintained with the rest of the code. This isn't true and can lead to problems like bug regressions.
  2. It's easy to miss an uncommented closing curly brace (for example) in the middle of a long block.

The deleted code is still available if you really need it, but it's no longer cluttering up your working copies. If you're really concerned about discoverability for the old code you can leave a comment indicating code was removed and the revision number you need to find it. At one line, that's a lot better than what the actual code block was using. It's also much clearer this code has been allowed to lapse, and for exactly how long.

like image 126
Joel Coehoorn Avatar answered Oct 07 '22 01:10

Joel Coehoorn


If you are using a source control system, deleting the code is my preferred option.

It won't get in your way when working with the current code, and you'll always have the code in the repository if you ever need it again.

like image 45
VBNight Avatar answered Oct 07 '22 03:10

VBNight


A piece of code can have two states.
Either it is active, functioning and tested, in which case it should be in the source control
Or it is obsolete in a way that you can't imagine anyone ever wanting to use it anymore, simply because it is obsolete. In this case it should be deleted.

Not erasing code so that "another developer can find it easily" is a perfectly good reason to keep the code active and compiling. Don't worry about the size of your libraries, the linker removes anything that isn't used.

If you're erasing code and want to warn others of the code that was there and for the reason it was deleted so they won't do the same mistake again, a good comment can be put in place.

like image 5
shoosh Avatar answered Oct 07 '22 02:10

shoosh