DEV Community

Cover image for Code Smell 54 - Anchor Boats
Maxi Contieri
Maxi Contieri

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

Code Smell 54 - Anchor Boats

Code is there. Just in case. We might need it soon.

TD:LR; Don't leave code for future use.

Problems

  • Complexity

  • Coupling

Solutions

  1. Remove dead code.

  2. Leave covered and real tested code.

Sample Code

Wrong

<?

final class DatabaseQueryOptimizer {

  public function selectWithCriteria($tableName, $criteria) {
    //Make some optimizations manipulating criterias
  }

  private function sqlParserOptimization(SQLSentence $sqlSentence): SQLSentence {
    //Parse the SQL converting it to an string and then working with their nodes as strings and lots of regex
    //This was a very costly operation overcoming real SQL benefits.
    //But since we made too much work we decide to keep the code. 
  }  
}
Enter fullscreen mode Exit fullscreen mode

Right

<?

final class DatabaseQueryOptimizer {

  public function selectWithCriteria($tableName, $criteria) {
    //Make some optimizations manipulating criterias
  } 
}
Enter fullscreen mode Exit fullscreen mode

Detection

Using some mutation testing variants we can remove the dead code and see it test fails.

We need to have good coverage to rely on this solution.

Tags

  • YAGNI

Conclusion

Dead code is always a problem.

We can use modern development techniques like TDD to ensure all code is alive.

Also Known as

  • Speculative Generality

Relations

More info

Also Known as

  • Speculative Generality

Credits

Photo by Kris Mikael Krister on Unsplash

Thanks to @Apoorv Tyagi for pointing this out.


It is very hard to predict, especially the future.

Niels Bohr



This article is part of the CodeSmell Series.

Last update: 2021/06/13

Top comments (2)

Collapse
 
dakujem profile image
Andrej Rypo

Why "Anchor Boats"? 🤣
"Dead Code" was not catchy enough?

I like this series btw.

Collapse
 
mcsee profile image
Maxi Contieri

They are different smells. The name is not mine