Stellarion
Quality

Unused Code Detection

Unused code—also known as dead code—refers to code that exists in your codebase but is never executed or referenced. This includes unreachable functions, unused variables, obsolete imports, and orphaned exports. While harmless at runtime, unused code degrades maintainability, increases bundle sizes, and creates confusion for developers.

What Stellarion Detects

Stellarion identifies several categories of unused code:

Unused Functions and Methods

Functions that are defined but never called anywhere in the codebase:

  • Private methods with no internal references
  • Exported functions with no importers
  • Helper functions that were replaced but not removed

Unused Variables and Constants

Declared values that are never read:

  • Variables assigned but never used
  • Constants defined for features that were removed
  • Parameters that functions never access

Unused Imports

Import statements that bring in unused symbols:

  • Imports for removed features
  • Over-imported modules (importing more than needed)
  • Type imports in JavaScript files

Unused Exports

Exports that no other module imports:

  • Public API surface that grew stale
  • Exports created "just in case"
  • Refactored code where consumers moved on

Unreachable Code

Code paths that can never execute:

  • Code after unconditional return, throw, or break
  • Conditions that are always true or false
  • Branches handling impossible states

How It's Measured

Stellarion reports unused code in lines:

Unused LinesSeverityImpact
0-100LowMinimal cleanup needed
100-300ModerateSchedule cleanup
300-500HighPrioritize removal
>500CriticalSignificant maintenance burden

The metric accounts for:

  • Actual lines of unused code (not just declarations)
  • Associated comments and documentation
  • Related test code that may also be obsolete

Why It Matters

Bundle Size Impact

Unused code ships to users, increasing:

  • Download times
  • Parse and compile time
  • Memory usage

Modern bundlers can tree-shake some unused code, but not all patterns are shakeable.

Maintenance Burden

Dead code creates ongoing costs:

  • Confusion: Developers waste time understanding code that doesn't matter
  • False positives: Searches return irrelevant results
  • Update overhead: Unused code still needs dependency updates
  • Test maintenance: Tests for dead code are pure waste

Security Exposure

Unused code can harbor:

  • Vulnerabilities in dead dependencies
  • Exposed attack surface that's not monitored
  • Debugging code with sensitive information

Cognitive Load

Every line of code is a line developers must process:

  • Reading through unused functions wastes mental energy
  • Dead code paths make control flow harder to follow
  • "What does this do?" questions slow down development

Common Sources of Unused Code

1. Feature Removal

Features get removed but their implementation remains:

// Feature flag removed, code stayed
export function legacyCheckout() { ... } // Never called

2. Refactoring Residue

Code that was replaced but not deleted:

// Old implementation kept "just in case"
function processDataV1() { ... } // Superseded by processDataV2

3. Copy-Paste Artifacts

Copied code brought along unnecessary pieces:

// Copied from another module, helper never used here
function unusedHelper() { ... }

4. Over-Engineering

Code written for requirements that never materialized:

// "We might need this someday"
export function futureFeature() { ... } // YAGNI violation

5. Defensive Coding Gone Wrong

Edge cases that can't actually occur:

if (typeof window === 'undefined' && typeof global === 'undefined') {
  // This can never execute in any JS environment
}

How to Remove Unused Code Safely

1. Verify with Stellarion

Run Stellarion's unused code detection to identify candidates:

# Generate report of unused code
stellarion analyze --focus unused

2. Check for Dynamic Usage

Before removing, verify the code isn't:

  • Called via eval() or dynamic import()
  • Referenced in configuration files
  • Used by external consumers (for libraries)
  • Called via reflection or string-based lookups

3. Search for String References

Check for string-based usage:

# Search for function name as string
grep -r "functionName" --include="*.ts" --include="*.json"

4. Remove in Small Batches

  • Delete one function or module at a time
  • Run tests after each removal
  • Commit frequently for easy rollback

5. Update Tests

Remove or update tests for deleted code:

  • Delete tests that only covered removed code
  • Update integration tests that referenced removed functions

Prevention Strategies

Tooling

  • ESLint: no-unused-vars, no-unreachable
  • TypeScript: noUnusedLocals, noUnusedParameters
  • Bundler analysis: Webpack Bundle Analyzer, Rollup Visualizer

Process

  • Code review: Catch dead code before it merges
  • Regular audits: Schedule quarterly cleanup sprints
  • Feature flags: Properly remove flag and code together

Culture

  • YAGNI: Don't write code for hypothetical futures
  • Boy Scout Rule: Leave code cleaner than you found it
  • Ownership: Teams own their dead code cleanup

For the complete metrics reference, see Stellarion Quality Metrics.