Quick revision
The short answer
Before changing anything, I would pin down what the method does today with characterisation tests, including its odd cases, introducing seams such as interfaces for its external calls so it can run in a test at all. Then I refactor in small steps using the IDE's automated refactorings, such as extract method and rename, running the tests after each step. Refactoring commits stay separate from the behaviour change, so the final change is small, reviewed on its own and covered by tests.
What to remember
In this answer, we treated the 400-line method as code whose behaviour we must discover before we change it. We introduced seams such as interfaces and a Clock so it could run in a test, wrote characterisation tests that lock in today's behaviour including odd cases, refactored with automated IDE refactorings one small step at a time with tests after each step, and kept those commits separate from the final behaviour change, which became small, tested and easy to review.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Before changing anything, I would pin down what the method does today with characterisation tests, including its odd cases, introducing seams such as interfaces for its external calls so it can run in a test at all. Then I refactor in small steps using the IDE's automated refactorings, such as extract method and rename, running the tests after each step. Refactoring commits stay separate from the behaviour change, so the final change is small, reviewed on its own and covered by tests.
Why this question is asked
Almost every experienced developer has met this method: 400 lines, nested ifs five levels deep, a database call in the middle, and a comment from 2016 saying 'do not touch'. The business now needs it to behave differently. The risky path is to edit it directly and hope. Interviewers ask this to hear a disciplined, safe process rather than heroics. In this answer, we will see why the change is risky, make the method testable by introducing seams, write characterisation tests that capture today's behaviour, refactor in small automated steps, and finally make the behaviour change on a clean, protected base.
1Why Editing It Directly Is Risky
A 400-line method without tests has two problems. First, nobody fully knows what it does. Over the years it has collected special cases, and some of them are relied on by customers or other systems even if they look like bugs. Second, there is no safety net: if we break one of those cases, nothing tells us until production does.
So the goal before the change is not to make the code beautiful. It is to know what it does now and be told immediately if we alter that by accident. Michael Feathers, who wrote the standard book on legacy code, defines legacy code simply as code without tests, and the whole approach below follows from that idea.
- Break dependenciesadd seams so the method runs in a test
- Write characterisation testsrecord what it does today, odd cases too
- Make one small automated refactoringextract method, rename, introduce variable
- Tests still green?YesCommit the refactoringNoUndo the step and take a smaller one
- Enough structure for the change?YesWrite a failing test for the new behaviourNoNext small refactoring
- Make the behaviour changeseparate commit, separate review
6 steps in order. Play them, or step through them one at a time.
2Introduce Seams So the Method Can Be Tested
A seam is a place where we can change what the code does without editing the code itself, usually by substituting a dependency. Legacy methods often create their dependencies inside the method, which makes them impossible to run in a test without a real database or HTTP service.
These edits are small and mechanical, and the IDE can do most of them (extract interface, introduce field, introduce parameter). We make them carefully, without tests yet, because they are the minimum needed to get tests in place. Notice the Clock: a method that reads the current time cannot be tested reliably, so the time becomes a dependency too. In production, Spring injects the real implementations; in tests, we pass simple fakes.
3Write Characterisation Tests
A characterisation test documents what the code actually does, not what we think it should do. We call the method with an input, assert on a value we do not know yet, let the test fail, and copy the actual result into the assertion. The test then locks in today's behaviour.
We cover the main paths and especially the odd cases: empty orders, zero quantities, unknown countries, rounding edges. If we find behaviour that looks wrong, we still capture it and note it, because fixing it is a behaviour change, not a refactoring. For a method with complex output, a golden master (approval) test is efficient: run many generated inputs, save all outputs to a file once, and compare against that file on every run. Coverage tools help us see which branches our tests still miss.
4Refactor in Small, Automated Steps
Now we can restructure. The key is to use the IDE's automated refactorings wherever possible: extract method, rename, introduce variable, inline, move. These are designed to preserve behaviour, so they are far safer than cutting and pasting by hand. A typical first pass is to extract each commented block into a well-named method, such as loadOrder, applyDiscounts, computeTax and buildLines, until the 400-line method reads as ten lines that describe the steps.
We take one step at a time and run the tests after each one. If they stay green, we commit; if they go red, we undo that step rather than debugging a pile of changes. Small steps feel slow, but they are faster overall because we never spend an afternoon finding which of twenty edits broke something.
5Keep Refactoring and Behaviour Changes Apart
The refactoring commits go into their own pull request, or at least their own commits, described as 'no behaviour change'. A reviewer can then check them with one question: did anything change other than structure? The characterisation tests answer most of that for them.
Only then do we make the requested change. With the method now split into clear pieces, the change is often a few lines in one extracted method, such as computeTax. We write a test for the new behaviour first, watch it fail, make the change and watch it pass. Some characterisation tests will need updating where the behaviour is meant to change, and that update is exactly what the reviewer of the behaviour change should look at.
| Commit | Behaviour changed? | Reviewer checks |
|---|---|---|
| Introduce seams (constructor injection, Clock) | No | only wiring changed |
| Add characterisation tests | No | tests pass on old code |
| Extract methods, rename variables | No | tests unchanged and green |
| Change tax rule for B2B orders | Yes | new test, updated expectations, the rule itself |
6Follow-up questions
What if you cannot introduce seams without changing behaviour?
Pick the least risky seam first. Techniques such as subclass-and-override, where a test subclass overrides the one method that calls the external system, need almost no change to the original code. Constructor injection is cleaner but touches more lines. With very risky code, we can also lean on higher-level tests, for example running the service against Testcontainers, until enough unit-level seams exist.
How do you know you have enough characterisation tests?
Run them with branch coverage and mutation testing, for example PIT, on the method. Coverage shows which branches never ran; mutation testing changes the code slightly and checks that some test fails. If mutating a condition does not fail any test, that branch is not really protected yet.
Should you rewrite the method from scratch instead?
Usually not. A rewrite throws away years of behaviour that nobody wrote down, and it is hard to review. Incremental refactoring keeps the system working at every step. A rewrite only makes sense when the characterisation tests are thorough enough to act as a specification for the new code.
7Conclusion
In this answer, we treated the 400-line method as code whose behaviour we must discover before we change it. We introduced seams such as interfaces and a Clock so it could run in a test, wrote characterisation tests that lock in today's behaviour including odd cases, refactored with automated IDE refactorings one small step at a time with tests after each step, and kept those commits separate from the final behaviour change, which became small, tested and easy to review.
Open with
I would not change the behaviour first. Without tests, nobody really knows what the method does, and some of its odd cases are probably relied on. So the first step is to make it testable by introducing seams: if it creates a DAO or an HTTP client inside, or calls LocalDate.now, I move those to constructor parameters behind small interfaces and a Clock, using the IDE's refactorings so the change is mechanical.
Then, if they want more
Then I write characterisation tests. I call the method with realistic inputs, let the assertion fail, and copy the actual result in, so the tests record today's behaviour, including strange cases like zero quantities or unknown countries. If something looks like a bug, I still capture it and note it, because fixing it is a separate behaviour change.
For complex output I might use a golden master test, and I check branch coverage to see what is still unprotected. Then I refactor in small steps, mostly extract method and rename, until the method reads as a list of named steps.
I run the tests after every step and commit when green; if a step breaks something, I undo it instead of debugging. Those refactoring commits are marked as no behaviour change and reviewed separately.
Finally, I write a failing test for the new behaviour, make the change, which is usually small by then, and that pull request only contains the real change.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "You must change behaviour in a 400-line legacy method that has no tests. Describe how...": https://scrivio-interview-library.netlify.app/java/clean-code/7105109dd1/