Best Practices for Removing Unused Code

后端 未结 8 1317
花落未央
花落未央 2021-02-07 17:56

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.

8条回答
  •  耶瑟儿~
    2021-02-07 18:32

    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.

提交回复
热议问题