# Code Smell 54 - Anchor Boats


> TL:DR; 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

[Gist Url]: # (https://gist.github.com/mcsee/22deb44d4e95b4a3ca8ce9ba18d0f908)
```php
<?

final class DatabaseQueryOptimizer {
  
  public function selectWithCriteria($tableName, $criteria) {
    //Make some optimizations manipulating criteria
  }
  
  private function sqlParserOptimization(SQLSentence $sqlSentence): SQLSentence {
    //Parse the SQL converting it to a 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. 
  }  
}
```

## Right

[Gist Url]: # (https://gist.github.com/mcsee/7d520c7f266d0180f42c3fc12b41fddc)
```php
<?

final class DatabaseQueryOptimizer {
  
  public function selectWithCriteria($tableName, $criteria) {
    //Make some optimizations manipulating criteria
  } 
}
```

# Detection

Using some [mutation testing](https://en.wikipedia.org/wiki/Mutation_testing) variants we can remove the dead code and see if 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.

%[https://maximilianocontieri.com/how-to-squeeze-test-driven-development-on-legacy-systems] 

# Also Known as

- Speculative Generality

# Relations

%[https://maximilianocontieri.com/code-smell-09-dead-code]

# More info

- [Exception not Found](https://exceptionnotfound.net/boat-anchor-the-daily-software-anti-pattern)

- [Wikipedia](https://en.wikipedia.org/wiki/You_aren%27t_gonna_need_it)

- [Refactoring.guru](https://refactoring.guru/smells/speculative-generality)

# Credits

Photo by <a href="https://unsplash.com/@kmkr">Kris Mikael Krister</a> on <a href="https://unsplash.com/s/photos/anchor">Unsplash</a>

Thanks to @[Apoorv Tyagi](@apoorvtyagi) for pointing this out.

* * *

> It is very hard to predict, especially the future.

_Niels Bohr_ 
 
* * *
 
%[https://maximilianocontieri.com/software-engineering-great-quotes]

* * *

This article is part of the CodeSmell Series.

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

*Last update: 2021/06/13*
