DEV Community

Cover image for Code Smell 09 - Dead Code
Maxi Contieri
Maxi Contieri

Posted on • Edited on • Originally published at maximilianocontieri.com

2

Code Smell 09 - Dead Code

Code that is no longer used or needed.

Problems

  • Maintainability

Solutions

  • Remove the code
  • KISS

Examples

  • Gold plating code or Yagni code.

Exceptions

  • Avoid metaprogramming. When used it is very difficult to find references to the code.

Sample Code

Wrong

class Robot {   
  walk(){
    //...
    }
  serialize(){
    //..  
  }
  persistOnDatabase(database){
    //..  
  }
}

Enter fullscreen mode Exit fullscreen mode

Right

class Robot {   
  walk(){
    //...
    }  
}
Enter fullscreen mode Exit fullscreen mode

Detection

Coverage tools can find dead code (uncovered) if you have a great suite of tests.

Tags

  • Unnecessary

Conclusion

Remove dead code for simplicity.
If you are uncertain of code you can temporary disable it using Feature Toggle.
Removing code is always more rewarding than adding.

More info

Credits

Photo by Ray Shrewsberry on Pixabay


This article is part of the CodeSmell Series.

Last update: 2021/06/11

Image of Datadog

The Future of AI, LLMs, and Observability on Google Cloud

Datadog sat down with Google’s Director of AI to discuss the current and future states of AI, ML, and LLMs on Google Cloud. Discover 7 key insights for technical leaders, covering everything from upskilling teams to observability best practices

Learn More

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay