Code Smell 09 - Dead Code

Code Smell 09 - Dead Code

Leader posted 4 min read

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

  1. Remove the code
  2. KISS
  3. Shrink codebase
  4. Test behavior only
  5. Trust version control

Refactorings ⚙️

https://maximilianocontieri.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) {
    // ..
  }
}
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://maximilianocontieri.com/laziness-i-meta-programming

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 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
ChatGPT ChatGPT
Claude Claude
Perplexity Perplexity
Copilot Copilot
You You
Gemini Gemini
DeepSeek DeepSeek
Meta AI Meta AI
Grok Grok
Qwen Qwen

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://maximilianocontieri.com/code-smell-54-anchor-boats

More Information

https://maximilianocontieri.com/laziness-i-meta-programming

Credits

Photo by Ray Shrewsberry on Pixabay


This article is part of the CodeSmell Series.

https://maximilianocontieri.com/how-to-find-the-stinky-parts-of-your-code

2 Comments

1 vote
0 votes
1

More Posts

Code Smell 319 - Hardcoded Stateless Properties

Maxi Contieri - Apr 9

Code Smell 17 - Global Functions

Maxi Contieri - Feb 28

Code Smell 16 - Ripple Effect

Maxi Contieri - Feb 19

Code Smell 15 - Missed Preconditions

Maxi Contieri - Jan 30

Code Smell 14 - God Objects

Maxi Contieri - Jan 23
chevron_left

Related Jobs

View all jobs →

Commenters (This Week)

1 comment
1 comment
1 comment

Contribute meaningful comments to climb the leaderboard and earn badges!