Dev Practices

What is Dead Code?

Dead code is source code that is never executed at runtime — unreachable branches, unused functions, or obsolete logic left behind after refactoring or feature removal.

By the Hyrax team·4 min read·May 1, 2026
TL;DR
  1. 1.Definition
  2. 2.Types of Dead Code
  3. 3.Why Dead Code Accumulates
  4. 4.Finding Dead Code
  5. 5.The Security Dimension

Definition

Dead code is source code that exists in a codebase but is never executed during normal program operation. It includes unreachable branches (code after a return statement), unused functions and methods, obsolete conditional paths, commented-out code left in place, and entire modules that were built but never called.

Dead code is not always harmless. It increases cognitive load (developers must read it to understand the system), inflates binary size, creates maintenance overhead (it must be updated when interfaces change), and can obscure security vulnerabilities that exist in code paths no one is reviewing.

Types of Dead Code

Unreachable code

Code that can never be executed due to program control flow. Classic examples: code after a return, break, or throw statement; branches of a conditional that can never be true; code in a try block after an always-throwing statement.

Unused functions and methods

Functions defined but never called anywhere in the codebase. In dynamic languages, this can be hard to detect statically because functions can be called by name at runtime. In compiled languages, compilers often detect and warn about these.

Unused variables and imports

Variables assigned but never read, imports included but never referenced. Most linters catch these, but they accumulate quickly in large codebases.

Feature flag dead code

Code behind feature flags that were enabled permanently but never cleaned up. The flag evaluates to true, the else branch is never executed, but the code remains. This is one of the most common sources of dead code in mature products.

Commented-out code

Code left in comments "just in case." Commented-out code provides no value (it doesn't run), creates confusion (why was this removed?), and is never updated as the surrounding code evolves.

Why Dead Code Accumulates

  • Feature removal — features are removed but the code is left "in case we need it"
  • Refactoring — new implementations replace old ones, old code remains
  • API changes — callers are updated but old function signatures remain
  • Conditional logic — flags, environment checks, or version guards that no longer apply
  • Lack of automated detection — without tooling, dead code is invisible

Finding Dead Code

Static analysis tools can detect dead code with high accuracy in typed, compiled languages. In dynamic languages, detection requires combining static analysis with runtime coverage data:

  • Static analysis — tools like ESLint (JavaScript), Pylint (Python), FindBugs/SpotBugs (Java) flag unreachable code and unused symbols
  • Code coverage data — coverage reports show which lines are never executed across the test suite
  • Language-specific tools — TypeScript's compiler flags unused variables and imports; Go's compiler requires all imported packages to be used

The Security Dimension

Dead code creates a specific security risk: it expands the attack surface without providing functionality. If a deprecated API endpoint remains in the codebase but is never called, a security review might miss it — but an attacker scanning for endpoints won't. Dead code that handles authentication, file access, or network calls is particularly dangerous.

Connection to Autonomous Code Governance

Autonomous code governance systems detect and remove dead code as part of continuous codebase health maintenance. Unlike manual cleanup sprints (which happen infrequently), autonomous detection identifies dead code as it accumulates and generates removal PRs that keep the codebase lean and auditable. Every line of code that doesn't exist is a line that can't contain a vulnerability.

Frequently Asked Questions

Is commented-out code considered dead code?

Yes. Commented-out code is never executed and has all the downsides of dead code: it creates confusion, goes stale, and must be maintained when interfaces change. Most style guides prohibit it; version control systems make it unnecessary.

Can dead code affect performance?

In compiled languages, compilers often remove dead code during optimization, so runtime performance impact is minimal. The larger costs are cognitive (developers reading unused code), build time (compiling unused modules), and binary size.

How do I safely remove dead code?

For function-level dead code: verify with static analysis and code coverage that the code is truly unreachable, then delete it. For module-level dead code: check for dynamic invocations (eval, reflection, plugin systems) before deleting. Always remove dead code in its own commit with clear documentation of why it was removed.

What is the difference between dead code and unused code?

Dead code is unreachable by design — the program's control flow can never reach it. Unused code is technically reachable but simply not called in practice (e.g., a utility function no one invokes). Both should be removed, but the detection methods differ.

Stop flagging. Start fixing.

Hyrax reviews your pull requests, remediates issues autonomously, and closes the ticket.

Join the waitlist