Lesson Completion
Back to course

Legacy Refactor Workflow

Advanced
8 minutesโ˜…4.7Git

The Hook (The "Byte-Sized" Intro)

Refactoring legacy code is like renovating a house while people live in it. You can't tear everything down at once. The strangler fig pattern โ€” build new alongside old, redirect traffic gradually, remove old when empty โ€” is the safest approach. Git supports this with parallel branches, feature flags, and incremental PRs.

๐Ÿ“– What is the Legacy Refactor Workflow?

A Git workflow designed for safely replacing or overhauling legacy code systems through incremental changes rather than big-bang rewrites.

Conceptual Clarity

Refactoring approaches:

ApproachRiskSpeedBest For
Big-bang rewrite๐Ÿ”ด Very highFast (appears)Never recommended
Strangler fig๐ŸŸข LowGradualLarge systems
Branch by abstraction๐ŸŸข LowMediumInternal refactors
Parallel implementation๐ŸŸก MediumMediumAPI replacements

Strangler fig steps:

PhaseGit Action
1. Create abstraction layerSmall PR, merge to main
2. Build new implementationPRs behind feature flag
3. Route traffic to newGradual flag rollout
4. Verify new worksMonitor and compare
5. Remove old codeClean-up PR

Real-Life Analogy

The strangler fig tree grows around an existing tree, gradually replacing it. The old tree supports the new one until the new tree can stand alone. Then the old tree is safely removed. No clear-cutting. No risk of collapse.

Visual Architecture

flowchart LR OLD["๐Ÿš๏ธ Legacy Code"] --> ABS["๐Ÿ“ Abstraction Layer"] ABS --> NEW["๐Ÿ—๏ธ New Code<br/>Behind flag"] NEW -->|"Gradual rollout"| LIVE["๐Ÿ  New Code Live"] LIVE --> REMOVE["๐Ÿงน Remove Legacy"] style OLD fill:#2d1b1b,stroke:#e94560,color:#e94560 style LIVE fill:#1b2d1b,stroke:#53d8fb,color:#53d8fb

Why It Matters

  • Safety: Production is never broken by half-finished refactors.
  • Reversibility: Feature flags allow instant rollback to old code.
  • Incremental progress: Each PR is small, reviewable, and deployable.
  • Proof: New code is proven in production before old code is removed.

Code

bash
# โ”€โ”€โ”€ Phase 1: Create abstraction (interface) โ”€โ”€โ”€ git checkout -b refactor/payment-interface # Create PaymentProcessor interface that old code implements # Small PR, merge to main # โ”€โ”€โ”€ Phase 2: Build new implementation โ”€โ”€โ”€ git checkout -b refactor/new-payment-processor # Implement new PaymentProcessor behind feature flag # if (flags.newPaymentProcessor) { useNew() } else { useOld() } # PR 1: New processor class # PR 2: Integration with existing system # PR 3: Migration utilities # โ”€โ”€โ”€ Phase 3: Gradual rollout โ”€โ”€โ”€ # Enable flag for 5% โ†’ 25% โ†’ 50% โ†’ 100% # Monitor error rates, latency, correctness # โ”€โ”€โ”€ Phase 4: Remove old code โ”€โ”€โ”€ git checkout -b chore/remove-legacy-payment # Remove old implementation, feature flag, and abstraction # One clean PR # โ”€โ”€โ”€ Each PR should be small โ”€โ”€โ”€ # Bad: 1 PR with 5,000 lines # Good: 10 PRs with 200-500 lines each

Key Takeaways

  • Never do a big-bang rewrite โ€” use the strangler fig pattern.
  • Build new code behind feature flags while old code runs in production.
  • Gradual rollout proves the new code works before removing the old.
  • Each step is a small, reviewable PR โ€” never one massive change.

Interview Prep

  • Q: How do you safely refactor a critical legacy system? A: Use the strangler fig pattern: create an abstraction layer, build the new implementation behind a feature flag, gradually route traffic to the new code, verify it works, then remove the old code. Each step is a separate, small PR.

  • Q: Why are big-bang rewrites risky? A: They're all-or-nothing โ€” if the new system has issues, there's no fallback. They also take months, during which the old system still needs maintenance. The team effectively maintains two systems with no production validation of the new one.

  • Q: What is "branch by abstraction"? A: A technique where you introduce an abstraction (interface) that both the old and new implementations satisfy. You swap implementations behind the abstraction without changing consumers. It enables incremental replacement.

Topics Covered

WorkflowsRefactoring

Tags

#git#refactoring#legacy#workflow

Last Updated

2026-02-13