Code Smell 46 - Repeated Code
DRY is our mantra. Teachers tell us to remove duplication. We need to go beyond.
Find repeated patterns (not repeated code).
Create an abstraction.
Parametrize abstraction calls.
Use composition and never inheritance.
Unit test new abstraction.
Linters can find repeated code.
There are not very good finding similar patterns.
Maybe soon machine learning will help us find such abstractions automatically.
For now, it is up to us, humans.
Repeated code is always a smell.
Copying and pasting code is always a shame.
With our refactoring tools, we need to accept the duplication remove challenge trusting our tests as a safety net.
Copy and paste is a design error.
This article is part of the CodeSmell Series.
Full agreement with general idea. But given example that should fix supposed duplication is a regression from readability perspective.
TextReplacer()->replace() API is awful. First it's more code not less and most important it's way harder to understand. I would classify this as clever code or overengineering. Refactoring like this make sense when repeated code have more logic, some local variables, in general you have to spend time to understand what it's doing. In this case abstracting and giving method descriptive name make total sense. In your example it's making simple code complicated.