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.
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.
“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.
Refactoring is a systematic process of improving code without creating new functionality that can transform a mess into clean code and simple design.
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.
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:
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With