51
Substitute Algorithm
Goal
An opaque or convoluted algorithm gets replaced by a clearer one (often from a library or well-known pattern) that produces the same outputs.
Before the refactoring
function found(people, n) {
for (const p of people) if (p.name === n) return p;
return null;
}After the refactoringfunction found(people, n) {
return people.find(p => p.name === n) ?? null;
}Savings
Future maintainers read the well-known pattern instead of decoding the bespoke implementation; performance and correctness usually improve.
Note
Swapping algorithms wholesale forfeits behavioral safety — characterize the function with tests at every input boundary you care about before substituting.