liveBook · Manning

Chapter 3: Chapter 3. Refactoring in small steps

What you will learn in chapter 3 of Test Driven: Practical TDD and Acceptance TDD for Java Developers

The introduction frames a template-engine design problem and defines a spike as throwaway prototyping used to explore a potential solution.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The introduction frames a template-engine design problem and defines a spike as throwaway prototyping used to explore a potential solution.

Key terms: spike solution, spike, throwaway prototyping

3.1 Exploring a potential solution

This section introduces a spike as exploratory work to evaluate or find an approach to a problem.

Key terms: spike, Extreme Programming, exploratory process

3.1.1 Prototyping with spikes

This section explains how spikes reduce uncertainty about potential solutions and how to use their findings without compromising production code.

Key terms: spike, experiment, test-first, production code

3.1.2 Learning by writing tests

The section introduces learning tests for exploring an API and proposes segmenting templates to prevent variable values from being interpreted as variables.

Key terms: learning tests, automated tests, spikes, plain-text segments, variable segments, parse, evaluate

3.1.3 Example spike for learning an API

A learning-test spike explores Java’s Matcher API and discovers how to locate template placeholders without relying on match groups.

Key terms: spike, learning tests, groupCount, match groups, regular expression, find, start, end, variable placeholders, plain text segments

3.2 Changing design in a controlled manner

The section explains a case-by-case choice of development approach and checks the test baseline before refactoring.

Key terms: breadth-first, depth-first, faking, Template, tests, double-rendering problem

3.2.1 Creating an alternative implementation

The section test-drives a separate template parser, refactors its tests, and reflects on the size and complexity of the implementation step.

Key terms: TemplateParse, plain text segment, failing test, helper method, varargs, custom assertion methods, collectSegments, variable placeholder, plain-text segments, variable segments

3.2.2 Switching over safely

The section switches template parsing to TemplateParse under test, then refactors the rendering code and identifies remaining design smells.

Key terms: TemplateParse, refactor, green bar, evaluate, segments, StringBuilder, variables map, level of abstraction, rendering logic, extracting methods

3.3 Taking the new design further

The section designs a uniform Segment interface and plans a small-step refactoring toward it.

Key terms: Segment, TemplateParse, StringBuilder, Map, programming by intention, evaluate, test first, small steps

3.3.1 Keeping things compatible

The section introduces Segment-based parsing and evaluation alongside the existing implementation, preparing for a compatible switchover.

Key terms: parseSegments, parse, backward compatibility, isVariable, Template, TemplateParse, Segment, evaluate, polymorphism, Variable

3.3.2 Making the switchover

The section switches Template to the new segment-based design, uses tests to catch a behavioral regression, and cleans up obsolete code.

Key terms: Template, evaluate, TemplateParse, Segment, test suite, missing variable values, Variable, exception, dead code, isVariable

3.4 Summary

Concepts explained in this chapter

← Chapter 2: Chapter 2. Beginning TDD Chapter 4: Chapter 4. Concepts and patterns for TDD →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.