Michael Feathers describes how to safely remove code aligned with the business’ needs.
Here is what I took away:
- Everybody likes to delete code, but nobody does it.
- Code needs no real space, but the features in the software need also space.
- Code, that is not used, is just a lie, existing in the code base.
- Test coverage can be misleading. Ask yourself: What is your test coverage for the code that is used in production?
- There is not only unused code but also code that gets executed but has no influence at the result at all.
- We still don’t correlate the revenue that the used code base produces. You could introduce special logging mechanisms to identify the executions of the code of your features.
What you can do to delete code:
- Remove unreachable code immediately
- Use mutation testing to detect dead code
- Implement feature probes, stack sampling or coverage to get deletion discussions started
- Don’t rewrite code because you don’t understand the code anymore. Identify the main line of execution for most use cases and improve the code base by creating a Strangler Application.
- Use characterization testing to re-engineer the functionality of hard to understand, but important code. The goal of these tests is to document behavior, not the correctness of the code. In the end, these tests should tell you the story of your tested feature.
- In the end, we can delete code, but it requires some analysis before. We need to learn more about the code that really gets executed – the actual usage.
Video: Michael Feathers – Strategic Code Deletion