Two functions having the same implementation are suspicious. It might be that something else was intended. Or the duplication is intentional, which
becomes a maintenance burden.
function calculateCode() {
doTheThing();
doOtherThing();
return code;
}
function getName() { // Noncompliant: duplicates calculateCode
doTheThing();
doOtherThing();
return code;
}
If the identical logic is intentional, the code should be refactored to avoid duplication. For example, by having both functions call the same
function or by having one implementation invoke the other.
function calculateCode() {
doTheThing();
doOtherThing();
return code;
}
function getName() { // Intent is clear
return calculateCode();
}
Exceptions
- Functions with fewer than 3 lines are ignored.
- This rule does not apply to function expressions and arrow functions because they don’t have explicit names and are often used in a way where
refactoring is not applicable.
list.map((item) => ({
name: item.name,
address: item.address,
country: item.country
}));