Skip to content

typescript.halstead_complexity

Maintainability Low

Analyzes Halstead complexity metrics to identify hard-to-maintain functions.

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
// ❌ 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 };
}
  • Functions with high difficulty scores
  • Deeply nested conditionals
  • Long functions with many operations

Unfault flags complex functions, but refactoring is manual.