What is Dead Code, what forms exist and why should it be avoided?
Code that is not executed
The term dead code is used in software development to describe lines of code that are not executed or whose results are not used after execution. Lines of code are not executed because they
- are no longer needed,
- are not called,
- cannot be called.
Forms of Dead Code
Some publications differentiate between different forms of dead code:
- Dead code exists when, for example, the result of a function is never used.
- Redundant code exists when several lines of code do the same thing. If lines of code are unintentionally redundant, the redundancy should be removed. However, if the redundancy is intended, e.g. when temporarily preserving lines of code, it can be retained – at least temporarily.
- Unattainable code occurs when lines of code cannot be called, e.g. because a condition of another line, which always occurs, prevents this.
- Unnecessary code occurs when, for example, a query is formulated in such a way that the result is always true. The query therefore makes no sense.
Reasons for Dead Code
Dead code can arise for different reasons: A developer may have forgotten to delete superfluous lines of code. Or the developer has decided not to delete because the code is mixed with functional code. Or the code is intentionally not removed, so it can be reactivated at a later time. Or a programming error has been overlooked, etc.
Dead code in itself does not affect the execution of program parts. Since many developments in practice have to deal with “living” but faulty, functionally insufficient or non-performing code, Dead Code often only gets a low priority. Nevertheless, it should be avoided for various reasons:
- it does not offer any benefit, but it may cause costs and effort if it is adapted in the course of further development.
- It makes maintenance and support more difficult if it cannot be identified beyond doubt as dead, redundant, unattainable or unnecessary code. A refactoring could provide a remedy.
- it increases the memory requirement.
- it has an impact on code coverage, either because it is tested unnecessarily, or because it is not tested, it provides poor coverage.
In principle, it is therefore recommended to identify and eliminate dead code at an early stage. The corresponding term is: Dead Code Elimination.
Here you will find additional information from our Smartpedia section: