Interview frequency: …
Short answer
Extract Method moves a cohesive block of code into a separate function with a name that explains its intent. Replace Conditional with Polymorphism replaces complex switch/case blocks with classes that implement a shared interface.
Explanation
If a long function contains a comment explaining what the next block does, that is a good time to apply Extract Method and turn that block into a separate method named after the comment. If switch conditions based on account types recur in five different functions, Replace Conditional with Polymorphism lets each class implement its own behavior, eliminating the switches.
Code example
// Replace switch with polymorphism:
interface EmployeeType {
calculateBonus(salary: number): number;
}
class ManagerType implements EmployeeType {
calculateBonus(salary: number) { return salary * 0.3; }
}Common mistakes
- Refactoring without automated, fast unit tests that were all passing before you began.