typescript.halstead_complexity
Maintainability
Low
Analyzes Halstead complexity metrics to identify hard-to-maintain functions.
Why It Matters
Section titled “Why It Matters”Complex code has real costs:
- Bug density — Complexity correlates with defects
- Onboarding time — New developers struggle
- Review burden — Reviewers miss issues
- Refactoring risk — Changes have unexpected effects
Example
Section titled “Example”// ❌ Before (high complexity)function processOrder(order: Order, user: User, inventory: Inventory) { if (order.status !== 'pending') return { error: 'invalid_status' }; if (!user.verified) return { error: 'user_not_verified' }; if (user.balance < order.total) return { error: 'insufficient_balance' };
for (const item of order.items) { if (!inventory[item.id] || inventory[item.id] < item.qty) { return { error: 'out_of_stock', item: item.id }; } inventory[item.id] -= item.qty; if (inventory[item.id] < 10) notifyRestock(item.id); }
if (!processPayment(user.id, order.total)) { // Rollback... return { error: 'payment_failed' }; } return { success: true };}// ✅ After (decomposed)function processOrder(ctx: OrderContext): Result { const validationError = validateOrder(ctx.order, ctx.user); if (validationError) return validationError;
const reservationResult = reserveInventory(ctx.order, ctx.inventory); if (!reservationResult.success) return reservationResult.error;
const paymentResult = processPayment(ctx.user.id, ctx.order.total); if (!paymentResult.success) { releaseInventory(reservationResult.reservations); return paymentResult.error; }
return { success: true };}What Unfault Detects
Section titled “What Unfault Detects”- Functions with high difficulty scores
- Deeply nested conditionals
- Long functions with many operations
Auto-Fix
Section titled “Auto-Fix”Unfault flags complex functions, but refactoring is manual.