The discipline in the book sounds simple but is hard to stick to: write tests before making changes, and take only small steps at a time.

封面: Refactoring: Improving the Design of Existing Code

Refactoring: Improving the Design of Existing Code · Martin Fowler ·

Cover image: Open Library

Key Takeaways

  • Martin Fowler's Refactoring, Second Edition, was written with contributions from Kent Beck, published by Addison-Wesley on November 20, 2018, with examples in JavaScript.
  • The core of the book is a catalog of about 70 refactoring techniques, 15 of which are new, each including motivation, mechanics, and examples.
  • For an FDE, the greatest value lies in this discipline: write tests first, move in small steps, and never leave the client's system in a broken state.

On code without tests, Martin Fowler's stance is blunt: any code that isn't trivial should be considered problematic if it has no tests. For a Forward Deployed Engineer (FDE) who has just been given access to a client's codebase, this should become a working principle.

An FDE's work rarely starts from a blank slate. Usually you're modifying code written by someone else, the system is running in production, and the client's engineering team is watching to see whether you'll break anything. In this situation, Refactoring: Improving the Design of Existing Code, Second Edition, is one of the most worthwhile books to keep at hand.

What's in the book?

The book is by Martin Fowler, with contributions from Kent Beck. It opens with an example that runs through the complete process, followed by foundational chapters on code smells and the role of testing, then a catalog of about 70 refactoring techniques, each with motivation, step-by-step mechanics, and examples.

The second edition merges some older techniques: Add Parameter, Remove Parameter, and Rename Method are now combined into one, Change Function Declaration. It will be mentioned again below.

First idea: changing structure and changing behavior are two different things

By Fowler's definition, refactoring changes the internal structure of software while preserving its externally observable behavior. That sounds obvious. But in real projects, the two kinds of changes often get mixed into the same pull request, and the client's reviewers can't tell which lines are cleanup and which will change results.

So the first habit worth building is separating the two. One commit changes only structure, with tests unchanged and still passing. The next commit adds the feature the client wants. The client's team reviews faster, and if something goes wrong, you know immediately which commit to suspect.

Second idea: make steps so small they're almost impossible to get wrong

Refactoring.com describes the core of this technique as a series of small behavior-preserving transformations. Each step carries low risk, and after each step the system still runs. The benefit is that you can stop anywhere, and the system won't be left in a half-finished state.

Here's a hypothetical example. The client's code has a function

calcInvoice(order, rate)

that is called in 12 places, and you need to add a

currency

parameter to it. The quick-and-dirty approach is to change the signature, update all 12 call sites in one go, and pray the build passes. The Change Function Declaration approach is slower but far safer.

Step one: create a new function with the

currency

parameter, defaulting to the current currency, have the old function call the new one, then run the tests. Step two: migrate call sites to the new function one by one, running the tests after each migration.

Final step: once nothing calls the old function anymore, delete it. If the client pulls you into an urgent meeting after you've updated the fifth call site, the system still works fine.

Third idea: don't touch anything without a safety net

Each of the small steps above is safe only because there are tests to run. Fowler calls this self-testing code, a term also used in the book. In his view, it's precisely this safety net that gives you the headroom to keep code in good shape. The publisher also emphasizes that the book will teach you to build the necessary tests before making changes.

Client code often lacks this net. So when facing a module without tests, the first thing to do is write tests that capture its current behavior, including behavior that looks wrong. You're simply documenting what the system actually does, so every subsequent change has a baseline to compare against.

The most common mistake happens exactly at this stage: while writing tests, you spot a suspected bug and fix it on the spot. As a result, your tests record the behavior you want rather than the behavior actually running, and the client's results change without anyone's approval.

The right approach is: write tests against the current behavior, note the suspected bug, then raise it with the client as a separate behavior change. Another of the client's systems may well depend on that "bug."

In what order should you read it?

If you've ever suffered through legacy code, the problems in the book will feel familiar from the first few pages. Read the opening example carefully first, then the chapters on code smells and testing. The testing chapter was completely rewritten in the second edition, because the example language changed from Java to JavaScript.

Don't read the catalog of 70 refactoring techniques cover to cover like a novel. Treat it as a reference manual, consulting it when you encounter the relevant situation, together with the online catalog Fowler maintains at refactoring.com/catalog/.

Teams using JavaScript or TypeScript can dive right in. If you write Python or Java, the steps apply just as well; you just have to translate the syntax yourself, and that translation process actually deepens your memory.

An interview question worth practicing

Before an FDE interview, it's worth preparing for this question: "A client hands you a billing module with no tests, and a new feature ships this week. What do you do on day one?" A bad answer jumps straight to redesigning. A good answer follows the three ideas above.

First, write tests to pin down the current behavior, and note suspected bugs to confirm with the client later. Then refactor only the parts the new feature will touch, in small steps, with separate commits. The feature comes last, as a change the client can review on its own.

On your resume, describe this sequence with a specific experience rather than a vague line like "experienced in refactoring." If a take-home assignment gives you legacy code, the first commit should be tests covering its current behavior.

Clients rarely remember how elegant the code you left behind was. But the day a "just a quick cleanup" change takes the system down, they'll remember that for a long time.

Was this article helpful?

Thanks for your feedback!

Next on the roadmap · Phase 1: Foundations The chapter of Learning SQL that an FDE should read most is near the end of the book. Read chapters 3–5 first, then jump straight to chapter 15 on metadata; leave the rest of Alan Beaulieu's book for when a project requires it.