Interview frequency: …
Short answer
Keep code open to extension (adding features) and closed to modification (without changing previously tested code) by relying on interfaces and abstract strategies.
Explanation
The goal is to add a feature by writing a new class that implements a specific interface, without opening existing files and adding long switch-case chains. Apply this carefully only to confirmed axes of change, not every line in anticipation of an imaginary future (YAGNI).
Code example
interface PaymentGateway {
process(amount: number): Promise<void>;
}
class StripeGateway implements PaymentGateway { /*...*/ }
class PayPalGateway implements PaymentGateway { /*...*/ }
class PaymentProcessor {
constructor(private gateway: PaymentGateway) {}
execute(amount: number) { return this.gateway.process(amount); }
}Common mistakes
- Modifying tested production code with new if/else conditions for every new client instead of using the Strategy pattern.