Course contentsShow
← Software Engineering
Lesson 341 of 1,93310. RefactoringFree lesson

Dispensables: Dead Code and Speculative Generality

Identifying unused code and over-engineered abstractions that add complexity without providing value.

Dispensables: Dead Code and Speculative Generality

You'll learn: How to recognize code that clutters your codebase without adding any real value.

What Are Dispensables?

Dispensables are pieces of code you can remove without losing functionality. They're like keeping empty boxes in your closet "just in case" — they take up space and make everything harder to find, but serve no purpose.

Dead Code

Dead code is code that never executes. It might be:

  • Functions or methods that are never called
  • Variables that are assigned but never read
  • Conditional branches that can never be reached
  • Commented-out code left "for reference"

Dead code is confusing because readers wonder: "Is this important? Should I maintain it? Why is it here?" It creates maintenance burden without providing value.

Example scenario: You remove a feature from your application but leave the supporting functions in place. Now every developer must read and understand code that literally does nothing.

Speculative Generality

Speculative generality happens when you design abstractions for flexibility you might need someday, but don't actually need now. It's building a five-lane highway for a bicycle path.

Common signs:

  • Abstract classes with only one implementation
  • Parameters or configuration options that are never used differently
  • Hooks or extension points with no actual extensions
  • Overly complex inheritance hierarchies "for future use"

This violates the YAGNI principle you learned earlier. The generality adds complexity today while the supposed benefit may never materialize.

Think of it this way: Speculative generality is like packing ski equipment for a beach vacation because you might decide to drive north instead.

Key Takeaway: Dead code and speculative generality clutter your codebase with complexity that provides zero value — remove them ruthlessly to keep your code lean and maintainable.