โšกLearnIT
Architecture ยท Topic 19

โ™ป๏ธRefactoring Strategy

19.1The core principle

Refactor in small, independently-shippable steps where the app works after every commit. If you ever reach a state you couldn't ship, the step was too big. No long-lived refactor branch drifting from main for weeks.

19.2Refactoring a monolithic component

Step 0 โ€” safety net first, not the refactor. Big components usually lack tests (that's why they grew). Write characterization tests that capture current behavior (what it does, not what it should do). These are your seatbelt โ€” as long as they stay green, observable behavior is intact. Also record a Profiler baseline so you can prove you didn't regress performance.

Step 1 โ€” map the seams. Identify natural boundaries: state used by only one section, JSX chunks that map to a visual region, logic that could be a pure function or hook. This is a map, not a change.

Step 2 โ€” extract in this order (it minimizes risk):

  1. Pure functions (data transforms, derived values) โ€” zero React risk, trivially testable.
  2. Custom hooks (stateful logic) โ€” same JSX, state now sourced from a hook.
  3. Leaf/presentational components last, innermost first (Row before List before Shell). Small leaves are easy to verify.

After each extraction: run characterization tests โ†’ green โ†’ commit. Every commit is a safe checkpoint.

Step 3 โ€” keep the public API stable. If <UserDashboard> is used in ten places, its props don't change during the refactor. All restructuring is internal โ€” that's what makes the change invisible to the rest of the app. Changing the API is a separate follow-up after the internal refactor ships.

On performance specifically: splitting one component into many does not automatically speed things up โ€” and can slow things if you introduce new context providers or unstable inline props that defeat React.memo. So: profile before, watch for the inline-callback regression (useCallback on callbacks to memoized children), and re-profile after to confirm you're even or better. The real win is that smaller components let you memoize precisely so a filter keystroke stops re-rendering the whole dashboard โ€” but only if you measure and target it. Don't sprinkle memo/useMemo/useCallback preemptively.

Step 4 โ€” ship incrementally. A series of small PRs (pure fns โ†’ hooks โ†’ leaves โ†’ shell) or a couple of medium PRs behind the stable API. Deployable at every point.

19.3Refactoring a larger system (e.g., Context โ†’ Zustand migration)

The same philosophy, scaled up:

  1. Audit โ€” map which components read/write which state; identify hot spots (frequently-changing state causing cascade re-renders) vs cold spots.
  2. Build alongside, don't replace โ€” stand up the new stores next to the old Context; both coexist; nothing breaks yet.
  3. Adapter layer โ€” a bridging hook so components can migrate one at a time; keep old and new in sync during transition (useEffect mirroring).
  4. Migrate high-impact components first โ€” measure the win with the Profiler after each.
  5. Cleanup โ€” once everything's migrated, delete the old Context and the sync layer.

Outcome framing for interviews: zero breaking changes, no feature freeze (teams keep shipping), measurable wins (load/interaction/re-render counts), no production incidents.

19.4Common refactoring mistakes

  • Big-bang rewrite ("rewrite the whole thing in 2 weeks") โ†’ months of churn, blocked features, bugs. Go incremental.
  • Changing too many things at once (state + React upgrade + TS migration together) โ†’ can't tell which change caused which bug. One concern per refactor.
  • Refactoring without measuring โ†’ "cleaner but slower." Performance metrics are the north star when perf is a goal.
  • Deploying risky changes carelessly (Friday 5pm, no staging) โ†’ weekend incidents. Test thoroughly; use staging; roll out gradually.

๐Ÿง  Quiz: Refactoring Strategy

1 question

1The core principle when refactoring a large system is:

Answered 0/1