It’s better to favor duplication over the wrong abstraction. The wrong abstraction starts out as a way to remove duplication and then gets altered over time to handle more and more conditions to the point where it’s unique to all the callers.
When this happens, the best solution is to ‘go backwards’ and inline the relevant code to the callers (with their respective special cases).
Read The Wrong Abstraction by Sandi Metz.
See also:
- Novice software engineers tend to optimize code by decreasing duplication
- All problems in computer science can be solved by another layer of indirection
Links to this note
-
Optimize for Reducing State, Coupling, Complexity, and Code
To optimize code, reduce state, coupling, complexity, and code, in that order. Reducing state makes the program easier to reason about (e.g. pure functions are easy to test). Be willing to add more coupling if that means less state, more complex if it decreases coupling, and duplication if it makes code less complex.
-
How to Make All Your Code Twice as Complicated
The way to make everything in your codebase twice as complicated is to make data nullable.