Overview

Chapter 3. Refactoring in small steps

The chapter shows how small, test-driven steps can be used to reshape a growing design without losing control. After an earlier implementation of a template engine runs into trouble with double-rendering, the author uses a spike and learning tests to explore Java’s regular expression API, discovering how to locate repeated variable placeholders safely. The key lesson is that spikes are for learning, not for producing reusable code; the real value is the knowledge that lets the developer continue with confidence.

Armed with that understanding, the implementation is refactored in a controlled way by introducing a separate parser class. The parser is built up from the simplest cases first, then extended to handle templates with multiple variables and plain text. Along the way, the text emphasizes constant cleanup: removing duplication in tests, keeping methods at a consistent level of abstraction, and making each change small enough that the test suite can verify it immediately.

The design is then pushed further by replacing raw strings with first-class segment objects. A new segment abstraction allows plain text and variables to evaluate themselves, eliminating type-checking logic from the template engine and reducing code smells like primitive obsession and tell-don’t-ask violations. The chapter closes by removing dead code, restoring proper encapsulation, and showing how a strong test suite makes even substantial design changes feel safe and manageable.

Listing 3.5. Refactoring the assertions to communicate intent better
Listing 3.6. Parsing variables from plain text
Listing 3.8. Extracting the rendering logic into a separate method
Listing 3.9. The append() method after a quick diet
Listing 3.10. Treating all segments the same
Listing 3.13. Converting the List of Strings into a List of Segment objects
Listing 3.16. Implementation of the evaluate() method for plain-text segments
Listing 3.18. Implementation of evaluate() for variable segments
Listing 3.19. Making the switchover in Template
Listing 3.20. The proper test for missing variable values
Listing 3.21. Checking for missing values in the Variable class at rendering time

FAQ

What is the main goal of refactoring in small steps in this chapter?

The goal is to improve the design of the template engine safely and incrementally, without breaking existing behavior. The chapter emphasizes making a large design change through a sequence of small, test-backed refactorings.

What is a spike solution, and why is it used?

A spike is a short, throwaway experiment used to explore a possible solution and learn whether it is viable and how much effort it might take. In this chapter, spikes are used to learn about Java’s regular expression API before implementing the real parser.

What are learning tests?

Learning tests are automated tests written to verify assumptions about unfamiliar code or APIs. Their purpose is not to test your application logic, but to teach you how an API behaves so you can safely use it in production code.

Why does the chapter recommend parsing the template before rendering it?

Parsing first splits the template into plain-text and variable segments, which prevents a rendered variable value from being interpreted as another variable. This avoids the double-rendering problem and makes evaluation simpler and safer.

Why was the TemplateParse class introduced?

TemplateParse was introduced to separate parsing responsibilities from rendering responsibilities. This makes the design easier to understand, easier to test, and easier to change without affecting the rest of the template engine.

Why start with tests for empty or plain-text-only templates?

These are the simplest cases and provide a low-risk starting point for building the parser. Starting small helps establish the basic behavior first and gives a safe foundation for adding more complex cases later.

How did the design evolve from Strings to Segment objects?

Initially, the parser returned a list of strings representing template parts. Later, those strings were replaced with first-class Segment objects so that each segment could know how to evaluate itself, reducing conditional logic in Template and improving encapsulation.

What problem was solved by introducing polymorphic Segment objects?

Polymorphic Segment objects eliminated the need for the Template class to inspect segment types with if-else logic. Instead, each segment type handles its own rendering, which follows the “Tell, Don’t Ask” principle and improves maintainability.

Why was the missing-value check moved into the Variable class?

The Variable class has both the variable name and access to the map of variable values during evaluation, so it is the best place to detect missing values and throw an exception. This keeps responsibility localized where the needed information is already available.

What is the key lesson about refactoring from this chapter?

The key lesson is that big design changes can be made safely through a series of very small, test-driven steps. With a good test suite, you can refactor aggressively, keep the system green, and gradually move toward a cleaner design.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Test Driven Test Driven ebook for free
Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Test Driven Test Driven ebook for free