Resources

Refactoring: Improving the Design of Existing Code

BookEvergreen

Martin Fowler's catalog of named, small, behavior-preserving changes — the second edition, rewritten in JavaScript so the refactorings apply to functions as well as classes. The book behind the third step of red–green–refactor.

The book that gave refactoring its vocabulary: Extract Function, Inline Variable, Replace Conditional with Polymorphism — each a named move with a motivation, a mechanics section and an example, so that a code review can say "extract that" and both people know the steps. The first edition (1999) was in Java and about classes; the second is in JavaScript, chosen because it has top-level and first-class functions, which lets the refactorings speak to code that is not object-oriented. Sixty-eight refactorings, ten of the old ones dropped and seventeen new, nearly every chapter rewritten.

Read it beside TDD: the refactor step is only safe because the tests are green when it starts, and this is the catalog of what that step can be. The first chapter — one worked example, refactored end to end — is the part to read if nothing else.

The canonical catalog is online at refactoring.com; Fowler's page on the second edition records what changed and why.

  • a: Appearance
  • ?: Keyboard shortcuts