Code Simpler - Complexity Reducer
Simplify overly complex code while preserving correctness.
When to Use
- Insight 리포트에서 복잡도 지적이 나왔을 때
- 중첩이 깊은 코드 (3단계 이상)
- 함수가 50줄을 초과할 때
- 불필요한 추상화가 감지될 때
- 조건문이 과도하게 복잡할 때
Simplification Techniques
1. Early Return (Guard Clause)
// Before: deeply nested
function process(data) {
if (data) {
if (data.isValid) {
if (data.items.length > 0) {
// actual logic
}
}
}
}
// After: flat
function process(data) {
if (!data) return;
if (!data.isValid) return;
if (data.items.length === 0) return;
// actual logic
}
2. Extract Method (Long Functions)
Split when a function does more than one thing:
- Each function = one responsibility
- Name describes WHAT, not HOW
- Max 50 lines per function
3. Remove Unnecessary Abstraction
// Before: premature abstraction
class UserValidatorFactory {
create(type) { return new UserValidator(type); }
}
// After: direct usage (if only one validator exists)
function validateUser(user) { ... }
4. Simplify Conditionals
// Before: complex boolean
if (user.age >= 18 && user.hasLicense && !user.isBanned && user.country === 'KR')
// After: named condition
const canDrive = user.age >= 18 && user.hasLicense && !user.isBanned;
const isKorean = user.country === 'KR';
if (canDrive && isKorean)
5. Replace Loop with Declarative
// Before: imperative
const results = [];
for (const item of items) {
if (item.active) {
results.push(item.name);
}
}
// After: declarative
const results = items.filter(i => i.active).map(i => i.name);
6. Flatten Callback Hell
Replace nested callbacks/promises with async/await or pipeline.
Process
1. Identify complexity (insight report or manual scan)
↓
2. Measure: nesting depth, function length, cyclomatic complexity
↓
3. Apply simplification technique (one at a time)
↓
4. Verify: behavior unchanged, tests still pass
↓
5. Repeat until within limits
Complexity Limits
| Metric | Limit | Action | |--------|-------|--------| | Nesting depth | 3 levels | Apply early return | | Function length | 50 lines | Extract method | | Cyclomatic complexity | 10 | Split into smaller functions | | File length | 200 lines | Split into modules | | Parameters | 4 | Use object parameter |
Principles
- One change at a time - simplify incrementally
- Preserve behavior - never change what the code does
- Verify with tests - run tests after each simplification
- Don't over-simplify - 3 similar lines > premature abstraction
- KISS - the simplest correct solution is the best