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:
- Copy code exactly as-is
- Only make minimal changes required for the new location (imports, visibility)
- Ensure all tests continue to pass
- Preserve all original functionality
Actual Behavior
Claude Code:
- "Enhances" or "improves" code while moving it
- Simplifies complex functions, removing functionality
- Modifies or deletes failing tests instead of fixing the refactoring
- Removes entire functions when they don't work after changes
- Makes assumptions about "better" implementations
Reproduction Steps
- Have a codebase with functions and their tests in one file
- Ask Claude to "refactor by moving functions to a new module while maintaining original functionality"
- Explicitly state "DO NOT CHANGE THE CODE"
- 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:
- "Improving" code rather than preserving it
- Assuming simpler is better, even when it removes features
- Modifying tests to match new behavior rather than preserving original behavior
- Not recognizing refactoring as a specific, constrained operation
Suggested Fix
-
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
-
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.
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:
Actual Behavior
Claude Code:
Reproduction Steps
Specific Example
Original function:
Claude's "refactored" version:
Impact
Root Cause Analysis
Claude appears to have a bias toward:
Suggested Fix
When the word "refactor" is used, Claude should enter a special mode where:
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
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.