Could someone explain the pros of deleting (or keeping) unused code?

Alex Turbin picture Alex Turbin · Mar 29, 2013 · Viewed 25.1k times · Source

I have heard many times that unused code must be deleted from the project. However it is not clear for me "why?".

My points for not deleting that are:

  • Code is already written, and efforts are spent
  • Code may be tested on syntetical and real environment
  • If organized well (grouped, separate package, loosely coupled etc) it doesn't disturbs you on overall code analysis or refactoring
  • Code may be used in future
  • When deleted, author may feel uncomfortable

Could someone explain the pros of deleting (or keeping) unused code?

Answer

suspectus picture suspectus · Mar 29, 2013

Here are some reasons why unused code should be removed:

  • For anyone new working on a project, they not only have to understand the working code, they have to understand unused material also. This is wasted time and creates confusion.

  • There is a danger that at sometime someone will make a change which inadvertently involve the 'dormant' code and can introduce bugs. I know it's happened on projects I've worked on.

  • The maintenance of any code is an administrative burden. By preserving old redundant code that burden is increased. For example, merging changes in the main branch becomes harder because there is more code to work through and more possibility to make a mistake.

  • What happens over time is that more and more old unused code is added to the codebase. This increases the confusion, potential misunderstanding and administrative overhead.

  • The chances that the unused code will ever be used again is very unlikely. With time that possibility of re-use diminishes. If code is to be removed and is considered important enough then the code can be branched off and documented.

  • Any personal feelings that a coder may have about code they may have worked hard on are understandable. But part of being professional requires that those thoughts have to be put to one side for the better good. Time stands for no-one and there is no place for preserving historical code in a working codebase.