Technical Mid Level

You inherit a 2,000-line function with no tests. How do you approach refactoring it safely?

Quick Tip

Write characterisation tests first - tests that document what the code actually does, not what you think it should do.

What good answers include

Best answers follow a disciplined approach: first understand what it does (read, add logging, trace execution), then write characterisation tests to capture current behaviour before changing anything. Refactor in small incremental steps, extracting methods and introducing seams. Mention the Strangler Fig pattern for large rewrites. Never rewrite from scratch without tests.

What interviewers are looking for

Reveals maturity and risk awareness. Reckless developers want to rewrite everything. Experienced ones add tests first and refactor incrementally. Ask follow-up: "What if there is pressure to ship fast?"

← All Software Developer questions