Dead Code
Source code that exists in a program but is never executed, has no effect on the output, or is unreachable.
Also known as: Unreachable Code, Zombie Code, Unused Code
Category: Software Development
Tags: software-engineering, code-quality, maintenance, anti-patterns, programming
Explanation
Dead code is any code in a software system that is never executed, cannot be reached, or has no effect on the program's output. It is a common form of technical debt that increases complexity without providing value.
## Types of dead code
1. **Unreachable code** - Code after a return statement, inside impossible conditions, or in branches that can never execute
2. **Unused declarations** - Functions, methods, classes, variables, or constants that nothing references
3. **Commented-out code** - Code that has been disabled but left in the source file
4. **Redundant code** - Code that executes but whose results are never used
5. **Feature remnants** - Code from features that were removed or never completed
6. **Dead imports** - Imported modules or packages that are never used
## Why dead code is harmful
- **Cognitive load** - Developers must read and mentally parse code that does nothing
- **Misleading** - Future developers may assume the code serves a purpose and work around it
- **Maintenance burden** - Dead code still needs to compile, may trigger warnings, and requires updates when APIs change
- **Larger binary size** - In some languages, dead code increases build artifacts
- **Security surface** - Unused code paths may contain vulnerabilities
- **Test overhead** - Code coverage tools may flag dead code as untested, obscuring real gaps
## Detection
- **Static analysis tools** - ESLint, SonarQube, IntelliJ inspections, etc.
- **Compiler warnings** - Many compilers flag unreachable code
- **Code coverage** - Identify code that is never executed in tests
- **IDE features** - Most IDEs highlight unused imports, variables, and methods
- **Tree shaking** - Bundlers like webpack and Rollup identify unused exports
## Removal strategy
1. Use static analysis to identify candidates
2. Verify the code is truly dead (not used via reflection, dynamic dispatch, etc.)
3. Delete it - version control preserves history
4. Run tests to confirm nothing breaks
5. Resist the urge to comment out instead of deleting
The best approach is preventing dead code from accumulating in the first place through code reviews, the Boy Scout Rule, and team norms that favor deletion over commenting out.
Related Concepts
← Back to all concepts