cover-img

Code Smell 54 - Anchor Boats

The code is there. Just in case. We might need it soon.

5 March, 2023

5

5

0

The code is there. Just in case. We might need it soon.

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

<?

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

<?

final class DatabaseQueryOptimizer {

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

Detection

Using some mutation testing variants we can remove the dead code and see if the 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.

How to Squeeze Test-Driven Development on Legacy Systems

Also Known as

  • Speculative Generality

Relations

Code Smell 09 - Dead Code

More Info

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


Software Engineering Great Quotes


This article is part of the CodeSmell Series.

How to Find the Stinky Parts of your Code

5

5

0

Maxi Contieri

Buenos Aires, Argentina

🎓Learn something new every day.📆 💻CS software engineer 👷coding👨🏽‍🏫teaching ✍🏾writing 🎨Software Design 🏢SOLID 🌉TDD 👴Legacy 💩Code Smells

More Articles

Showwcase is a professional tech network with over 0 users from over 150 countries. We assist tech professionals in showcasing their unique skills through dedicated profiles and connect them with top global companies for career opportunities.

© Copyright 2024. Showcase Creators Inc. All rights reserved.