Skip to content

TDD guard blocks removal of dead code and its tests during refactoring phase #83

@tfriedel

Description

@tfriedel

Disclaimer: the following is written by Claude Code when it got stuck.

Context

During the refactoring phase of TDD (all tests green, improving code structure), I encountered a situation where I needed to remove dead code along with its associated tests.

The Scenario

  1. I had a method _find_english_subtitles() that was originally used in production code
  2. During feature development, I created a new more general method _find_subtitles() that replaced it
  3. The old method is now dead code - defined but never called in production
  4. The old method still has 4 tests in the test file

The Problem

When trying to clean up this dead code during refactoring:

Attempt 1: Remove the dead method first

  • Tests fail (as expected, since method is gone)
  • This is fine, but now...

Attempt 2: Remove the tests for the dead method

  • TDD guard blocks with: Removing existing tests violation - deleting 4 working test methods reduces test coverage

Attempt 3: Restore the method to fix failing tests

  • TDD guard blocks with: Premature implementation violation - adding new method '_find_english_subtitles' without a failing test

This creates a catch-22 situation where:

  • Can't remove tests for dead code (blocked as "removing coverage")
  • Can't restore dead code to make tests pass (blocked as "premature implementation")

Expected Behavior

During the refactoring phase, it should be valid to remove dead code along with its tests in a single atomic operation, since:

  1. The code is not called anywhere in production
  2. The tests only test dead code
  3. Removing both together maintains the invariant that all tested code is actually used

Suggested Solutions

  1. Allow removal of tests when the corresponding implementation is also being removed in the same edit
  2. Add a refactoring-specific mode that allows dead code cleanup
  3. Detect when code being tested is not called from production code (dead code detection)

Environment

  • Using tdd-guard-pytest plugin
  • In explicit refactoring phase (triggered by /refactor command)
  • All tests were green before attempting cleanup

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions