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

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?


Solution 1:

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.

Solution 2:

@suspectus has done an excellent job of presenting the reasons for deleting code; I would like to address your individual bullets for keeping code.

  • Code is already written, and efforts are spent

But if the already-written code is not in use, this is cost only without (future) value. It is effort invested in vain, and preserving the unused product of those efforts does not validate those efforts. We keep code because it is useful, now, not as some kind of memorial to the efforts of the authors.

  • Code may be tested on syntetical and real environment

I'm sorry, I don't know what you mean by this.

  • If organized well (grouped, separate package, loosely coupled etc) it doesn't disturbs you on overall code analysis or refactoring

If it exists in the code base, no matter how well organized, it contributes to the maintenance and comprehension burden. True, it can be organized so as to be less of a burden, but if it's gone, it's no burden at all.

  • Code may be used in future

In the Agile school, we say YAGNI: You Ain't Gonna Need It. Yes, you might possibly have a use for it in the future, but we can't know enough today about tomorrow's needs to be able to predict that with any kind of reliability. To think otherwise is arrogance tending toward hubris. What we can know about tomorrow is: we want our code base to be easy to modify, and unused code detracts from that characteristic.

  • When deleted, author may feel uncomfortable

Author must get over it. We've all written things that turned out not to be useful - far better to be able to point to a body of code that is all being used (because the unused cruft was deleted) than to a body of code wherein you can say of a few methods, "and that one's actually in use!"

Solution 3:

Isn't it tough enough to pick up some code and figure out the intent, but now you have to figure out which parts are not in use?