Beginner LevelSoftware Engineering Principles
What is Code Refactoring and what are common Code Smells (Long Method, God Object, Feature Envy)?
❓ Interview Prompt: Explain Code Refactoring, identifying Code Smells (God Class, Feature Envy, Long Method, Primitive Obsession), and disciplined refactoring.
Code Refactoring is the disciplined process of restructuring existing computer code without changing its external observable behavior:
Common Code Smells:
•
God Object (Large Class): A single class that knows or does too much, accumulating hundreds of methods.
•
Long Method: A function spanning 50+ lines doing multiple unrelated tasks.
•
Feature Envy: A method that makes excessive calls to another class's getters to process data rather than moving the logic into that target class.
•
Primitive Obsession: Using basic primitive types ('string', 'number') instead of small domain objects ('EmailAddress', 'Money').
Code Implementation Example
typescript
// ❌ Primitive Obsession Code Smell
function processPayment(amount: number, currency: string) {}
// ✅ Refactored domain Value Object
class Money {
constructor(readonly amount: number, readonly currency: string) {
if (amount < 0) throw new Error('Amount cannot be negative');
}
}
function processPaymentRefactored(money: Money) {}💡Refactoring Primitive Obsession smell into a typed Money Value Object.
🎯 Interview Key Takeaway
Identify code smells early to refactor rigid code structures into clean, modular abstractions without altering external behavior.
#Refactoring#Code Smells#God Object#Beginner
Back to all Software Engineering Principles Questions→