Book cover of Refactoring by Martin Fowler, featuring abstract geometric shapes suggesting structured transformation on a clean background

Pages

461

Published

2012

Programming ✨ New

Refactoring

Improving the Design of Existing Code

Learn the precise mechanics of restructuring working code so it becomes easier to read, cheaper to change, and safer to extend β€” without breaking it.

Refactoring is the disciplined process of changing code's internal structure without altering its external behavior. This book catalogs over 70 named refactoring techniques, each with a motivation, step-by-step mechanics, and before-and-after code examples. Whether you're untangling a legacy codebase or keeping a greenfield project clean, the techniques here give you a shared vocabulary and a repeatable method for improving code that actually works in production.

About this book

Every programmer eventually faces code that has grown hard to change. Functions that do too much. Classes that know too little about their own data. Duplicated logic hiding in plain sight. The temptation is to rewrite from scratch. The pragmatic answer is to refactor β€” to reshape the code incrementally, one small behavior-preserving transformation at a time.

This book defines what refactoring actually means and then delivers the concrete techniques that make it possible. Martin Fowler and his co-authors cataloged the moves that experienced developers already make intuitively and gave them names, motivations, and step-by-step mechanics. The result is a reference you can open mid-session and follow precisely, rather than a vague prescription to "write cleaner code."

The core of the book is a catalog of over 70 refactorings. Each entry tells you when to apply the technique, why it improves the design, exactly how to execute it without introducing bugs, and what the result looks like. Techniques cover the full range of everyday restructuring work: extracting and inlining methods, moving responsibilities between classes, replacing conditionals with polymorphism, introducing parameters and removing flags, decomposing complex expressions, and much more.

Beyond the catalog, the book builds the surrounding practice. You will learn how to identify the "bad smells" that signal a refactoring opportunity before the code becomes a crisis. You will see how a suite of automated tests is not optional infrastructure but the safety net that makes refactoring fast and fearless. You will understand when to refactor, when to stop, and how to fold refactoring into your normal development rhythm rather than scheduling it as a separate cleanup sprint.

  • A vocabulary of named transformations your whole team can use in code reviews and design discussions
  • Step-by-step mechanics for over 70 refactorings, verified against real code
  • A catalog of code smells β€” with the specific refactorings that cure each one
  • Guidance on writing and running tests so you can refactor with confidence
  • Practical advice on introducing refactoring practice into a team that has never done it systematically

The techniques in this book are language-agnostic in principle. The examples are written in Java, so readers comfortable with any C-family language will follow without friction. If you write code professionally and have never worked through this material, it belongs on your desk, not your wish list.

🎯 What you'll learn

  • Recognize the code smells that signal a design problem before it becomes a maintenance crisis
  • Apply over 70 named refactoring techniques with precise, repeatable step-by-step mechanics
  • Execute behavior-preserving transformations safely by relying on a fast, comprehensive test suite
  • Move methods and data to the right class by applying the core principles of responsibility assignment
  • Replace tangled conditional logic with polymorphism and clear object hierarchies
  • Introduce a shared refactoring vocabulary that makes code reviews and design discussions more precise
  • Fit refactoring into your daily development rhythm instead of treating it as a separate cleanup task

πŸ‘€ Who is this book for?

  • Working software developers who maintain or extend an existing codebase and want a systematic method for improving it
  • Junior engineers who have learned syntax and frameworks but have not yet been taught how experienced developers reshape messy code
  • Senior engineers and tech leads who want a shared vocabulary for code-quality conversations with their team
  • Engineering teams adopting agile or continuous-delivery practices where keeping code changeable is a survival skill
  • Computer science students and bootcamp graduates ready to move beyond writing code that merely works

Table of contents

  1. 01

    Refactoring: A First Example

    Walk through a single worked example from messy starting code to a cleaner result, applying a sequence of refactorings step by step so the process is concrete before the theory is introduced.

  2. 02

    Principles in Refactoring

    Establish a precise definition of refactoring, distinguish it from other kinds of code change, and explain why it is essential to sustainable development practice.

  3. 03

    Bad Smells in Code

    Survey the catalog of code smells β€” the recurring patterns in code that signal an underlying design problem β€” and map each smell to the refactorings that address it.

  4. 04

    Building Tests

    Learn how to construct a fast, reliable unit-test suite and understand why a solid test harness is the prerequisite for safe, confident refactoring.

  5. 05

    Toward a Catalog of Refactorings

    Understand how the refactoring catalog entries are structured β€” motivation, mechanics, and examples β€” so you can apply and adapt them efficiently during real work.

  6. 06

    Composing Methods

    Apply the fundamental method-level refactorings β€” Extract Method, Inline Method, Replace Temp with Query, and related moves β€” that clarify intent and reduce duplication at the function level.

  7. 07

    Moving Features Between Objects

    Redistribute responsibilities across classes using Move Method, Move Field, Extract Class, and related techniques to bring data and behavior together where they belong.

  8. 08

    Organizing Data

    Restructure how data is represented and accessed, replacing raw values with objects, removing flags, and encapsulating fields to improve both clarity and encapsulation.

  9. 09

    Simplifying Conditional Expressions

    Break apart complex conditional logic using Decompose Conditional, Consolidate Conditional Expression, Replace Conditional with Polymorphism, and related refactorings.

  10. 10

    Making Method Calls Simpler

    Clarify the interfaces between objects by renaming methods, adding and removing parameters, separating query from command, and hiding the details that callers should not need to know.

Frequently asked questions

Do I need to know Java to benefit from this book?

The code examples are written in Java, but the techniques apply to any object-oriented language. Developers comfortable with C#, C++, Python, or similar languages follow the examples without difficulty.

Is this the first edition or the second edition?

The publication details indicate a 2012 release, which corresponds to the first edition. A second edition with JavaScript examples was published later, so check which edition fits your preferred language.

What level of programming experience do I need?

You should already be writing production code professionally or have completed a serious programming curriculum. The book assumes you know what a class, method, and unit test are β€” it teaches you how to improve code, not how to write it for the first time.

Is this a book you read cover to cover, or is it a reference?

Both. Fowler recommends reading the first few chapters straight through to understand the principles and practice, then using the catalog section as a reference you return to during actual refactoring sessions.

Does the book cover automated refactoring tools or IDEs?

The book focuses on the mechanics and reasoning behind each transformation. While modern IDEs automate many of these steps, understanding the underlying moves makes you a more effective user of those tools and helps when automation is unavailable.

You might also like

πŸ“¬ Weekly Newsletter

Stay ahead of the curve

Get the best programming tutorials, data analytics tips, and tool reviews delivered to your inbox every week.

No spam. Unsubscribe anytime.