Code is there. Just in case. We might need it soon.
TD:LR; Don't leave code for future use.
Problems
Complexity
Coupling
Solutions
Remove dead code.
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.
}
}
Right
<?
final class DatabaseQueryOptimizer {
public function selectWithCriteria($tableName, $criteria) {
//Make some optimizations manipulating criterias
}
}
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.
![mcsee](https://res.cloudinary.com/practicaldev/image/fetch/s--by_E26Wt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://res.cloudinary.com/practicaldev/image/fetch/s--XvnZDWAZ--/c_fill%2Cf_auto%2Cfl_progressive%2Ch_150%2Cq_auto%2Cw_150/https://dev-to-uploads.s3.amazonaws.com/uploads/user/profile_image/366059/69feb7c3-ab01-4bcd-a6f5-ec38e9300da3.jpeg)
How to Squeeze Test Driven Development on Legacy Systems
Maxi Contieri ・ Nov 26 '20
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
![mcsee](https://res.cloudinary.com/practicaldev/image/fetch/s--by_E26Wt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://res.cloudinary.com/practicaldev/image/fetch/s--XvnZDWAZ--/c_fill%2Cf_auto%2Cfl_progressive%2Ch_150%2Cq_auto%2Cw_150/https://dev-to-uploads.s3.amazonaws.com/uploads/user/profile_image/366059/69feb7c3-ab01-4bcd-a6f5-ec38e9300da3.jpeg)
Software Engineering Great Quotes
Maxi Contieri ・ Dec 28 '20
This article is part of the CodeSmell Series.
![mcsee](https://res.cloudinary.com/practicaldev/image/fetch/s--by_E26Wt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://res.cloudinary.com/practicaldev/image/fetch/s--XvnZDWAZ--/c_fill%2Cf_auto%2Cfl_progressive%2Ch_150%2Cq_auto%2Cw_150/https://dev-to-uploads.s3.amazonaws.com/uploads/user/profile_image/366059/69feb7c3-ab01-4bcd-a6f5-ec38e9300da3.jpeg)
How to Find the Stinky parts of your Code
Maxi Contieri ・ May 21 '21
Last update: 2021/06/13
Top comments (2)
Why "Anchor Boats"? 🤣
"Dead Code" was not catchy enough?
I like this series btw.
They are different smells. The name is not mine