Code that is no longer used or needed.
TL;DR: Do not keep code "just in case I need it".
Problems π
- Maintainability
- Extra reading
- Broken intent
- Wasted effort
Solutions π
- Remove the code
- KISS
- Shrink codebase
- Test behavior only
- Trust version control
Refactorings βοΈ
https://hackernoon.com/refactoring-021-remove-dead-code
Examples π
- Gold plating code orΒ YagniΒ code.
Context π¬
Dead code appears when you change requirements, and you fear deleting things.
You comment logic, keep old branches, or preserve unused methods just in case.
When you do that, you lie about what the system can actually do.
The code promises behavior that never happens.
Sample Code π
Wrong π«
class Robot {
walk() {
// ...
}
serialize() {
// ..
}
persistOnDatabase(database) {
// ..
}
}
Right π
class Robot {
walk() {
// ...
}
}
Detection π
Coverage tools can find dead code (uncovered) if you have a great suite of tests.
Exceptions π
- Avoid metaprogramming. When used, it is very difficult to find references to the code.
https://hackernoon.com/laziness-chapter-i-meta-programming-6s4l300e
Tags π·οΈ
- YAGNI
Level π
[x] Beginner
Why the Bijection Is Important πΊοΈ
Your program must mirror theΒ MAPPERΒ with a clearΒ bijection.
Dead code breaks that mapping. The domain has no such behavior, yet the code claims it exists.
When you do that, you destroy trust.
Readers cannot know what matters and what does not.
AI Generation π€
AI generators often create dead code.
They add defensive branches, legacy helpers, and unused abstractions to make it look complete.
When you do not review the result, the smell stays.
AI Detection π§²
AI tools can remove this smell with simple instructions.
You can ask them to delete unreachable code and align logic with tests.
They work well when you already have coverage.
Try Them! π
Remember: AI Assistants make lots of mistakes
Suggested Prompt: correct=remove dead code
Without Proper Instructions π΅
With Specific Instructions π©βπ«
Conclusion π
Remove dead code for simplicity.
If you are uncertain of your code, you can temporarily disable it usingΒ Feature Toggle.
Removing code is always more rewarding than adding.
Relations π©ββ€οΈβπβπ¨
https://hackernoon.com/how-to-find-the-stinky-parts-of-your-code-part-xi-sit35t1
More Information π
Credits π
Photo byΒ Ray ShrewsberryΒ onΒ Pixabay
This article is part of the CodeSmell Series.
