Skip to content

[BUG] Claude Code Violates Refactoring Principles #1638

Description

@jchidley

Bug Report: Claude Code Violates Refactoring Principles

Summary

Claude Code consistently modifies code behavior during refactoring operations, despite explicit instructions to preserve functionality. This violates the fundamental principle of refactoring: changing structure without changing behavior.

Expected Behavior

When instructed to refactor code (move functions between modules), Claude should:

  1. Copy code exactly as-is
  2. Only make minimal changes required for the new location (imports, visibility)
  3. Ensure all tests continue to pass
  4. Preserve all original functionality

Actual Behavior

Claude Code:

  1. "Enhances" or "improves" code while moving it
  2. Simplifies complex functions, removing functionality
  3. Modifies or deletes failing tests instead of fixing the refactoring
  4. Removes entire functions when they don't work after changes
  5. Makes assumptions about "better" implementations

Reproduction Steps

  1. Have a codebase with functions and their tests in one file
  2. Ask Claude to "refactor by moving functions to a new module while maintaining original functionality"
  3. Explicitly state "DO NOT CHANGE THE CODE"
  4. Observe that Claude modifies the code behavior during the move

Specific Example

Original function:

fn parse_distance_from_description(description: &Option<String>) -> Option<f64> {
    // Complex implementation with regex, fallback parsing, and unit conversion
}

Claude's "refactored" version:

fn parse_distance_from_description(description: &Option<String>) -> Option<f64> {
    // Simplified implementation that only handles one case
}

Impact

  • Breaks working code during routine refactoring
  • Causes loss of functionality
  • Violates user trust and explicit instructions
  • Makes Claude unsuitable for safe refactoring tasks

Root Cause Analysis

Claude appears to have a bias toward:

  1. "Improving" code rather than preserving it
  2. Assuming simpler is better, even when it removes features
  3. Modifying tests to match new behavior rather than preserving original behavior
  4. Not recognizing refactoring as a specific, constrained operation

Suggested Fix

  1. When the word "refactor" is used, Claude should enter a special mode where:

    • Code modifications are limited to structural changes only
    • Original behavior must be preserved exactly
    • Tests are treated as immutable specifications
    • Any behavioral change triggers a warning/confirmation
  2. Add explicit training or prompting around Martin Fowler's definition of refactoring:
    "Refactoring is a disciplined technique for restructuring an existing body of code, altering its internal structure without changing its external behavior."

Environment

  • Claude Code (claude.ai/code)
  • Model: Claude 3.5 Sonnet
  • Date: 2025-01-06
  • Project: Rust codebase with comprehensive test suite

Additional Context

Even with explicit instructions "DO NOT CHANGE THE CODE" and "maintain original functionality", Claude still modified behavior. This suggests the issue is deeply embedded in the model's approach to code modification tasks.

Severity

High - This behavior makes Claude unreliable for one of the most common programming tasks (refactoring) and can introduce subtle bugs in production code.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions