liveBook · Manning

Chapter 9: Refactoring Legacy Code

What you will learn in chapter 9 of Data-Oriented Programming in Java

The introduction frames data-oriented refactoring as a way to improve ordinary legacy code incrementally by bringing its hidden data to the surface.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The introduction frames data-oriented refactoring as a way to improve ordinary legacy code incrementally by bringing its hidden data to the surface.

Key terms: data-oriented, refactoring, data, objects, data type, incremental

9.1 A messy starting point

The section introduces a legacy shopping-cart service whose growing feature complexity, shared entity models, and scattered behavior make it a difficult starting point for refactoring.

Key terms: ShoppingCart, CartItem, Entity types, boundary issues, inventory checks, state management, upselling, Cart object, main controller

9.2 Analyzing common problems in code

The section argues that refactoring should begin by identifying problems in the existing code, rather than by rearranging architectural boundaries.

Key terms: responsibility boundaries, indirection, refactors

9.2.1 Messy data modeling

The section identifies poorly focused Cart and item models and explains why their unclear invariants make refactoring risky.

Key terms: Cart, Inventory Tracker, lack of cohesion, invariants, Entities

9.2.2 Uncontrolled mutation

This section identifies uncontrolled mutation in the legacy Cart, explains why it undermines encapsulation and invariants, and cautions against treating all mutation as the problem.

Key terms: Cart, uncontrolled mutation, behavioral methods, mutable Entities, encapsulation boundaries, invariants, mutable reference, mutable state, refactoring

9.2.3 Sequential coupling

This section explains how methods that must be called in a particular order undermine encapsulation and can leave an object in an inconsistent state.

Key terms: sequential coupling, temporal coupling, encapsulation, single responsibility, invariants, atomic, updateMetadata, updateInventory

9.2.4 Hidden knowledge, partial construction, and shotgun validation

This section shows how partially constructed cart items, hidden assumptions, and validation that happens too early can combine to create a duplicate-item bug.

Key terms: partial construction, Entity, CartItem, sellerId, validation, invalid state, hidden knowledge, invariants, Shotgun Parsing

9.3 Fixing the problems

This section frames refactoring as a chance to prevent recurring problems by starting with data and setting better modeling standards.

Key terms: refactor, data, illegal states

9.3.1 Work backwards from invariants

The section works backward from cart requirements to invariants, then uses those invariants to guide a practical refactor of the cart’s data model.

Key terms: invariants, for all states, Desired ≤ Available, Active cart, immutable data, defensive copies, List, Set, Maps, uniqueness

9.3.2 Draw boundaries with data

This section shows how a data type can create a proof-requiring boundary inside legacy code and provide a starting point for incremental refactoring.

Key terms: data types, boundaries, gate keeper, proof, Curry-Howard Correspondence, ValidCart, boundary, incrementally, invariants

9.3.3 Make validation about evidence rather than errors

The section reframes validation as a boundary that produces data proving a feature’s preconditions are met, including when the data represents an intermediate state.

Key terms: error-centric view, preconditions, shotgun parsing, validation boundary, evidence, preflight checks, ValidAddItemsRequest, Item, Cart, ValidCart

9.3.4 Write utilities that make data-oriented programs more expressive

This section shows how small Map utilities make immutable cart logic easier to express, reason about, and refactor.

Key terms: Map, utility function, streams and collectors, intersection, difference, set-like operations, Active and Saved cart items, collision, BinaryOperator, right biased

9.3.5 Keep mutation where useful, but use it to build data

This section shows how controlled local mutation can simplify a data-oriented refactor and how a new data model can coexist with legacy code during incremental migration.

Key terms: mutation, mutable collections, pure, deterministic function, immutable data, cart, data model, Entities, Data transfer Object (DTO), serialize, incremental improvements

9.3.6 Now consider object boundaries

The section explains why object ownership and service boundaries are best reconsidered after the refactoring has established sound data types and data-flow boundaries.

Key terms: ownership, responsibility, invariants, boundaries, data flow, Cart, Store Façade, data-oriented code, seam

9.4 Some encouragement on refactoring legacy code

The section encourages incremental data-oriented refactoring of legacy code rather than accepting decline or waiting for a rewrite.

Key terms: refactoring, downward spiral, existing patterns, data type, data-orientation, representation

9.5 Wrapping up

The section closes by presenting data types as a way to refactor code incrementally by reshaping its boundaries.

Key terms: data type, boundaries, Curry-Howard Correspondence

9.6 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 8: Business Rules as Data Chapter 10: Data Oriented Architecture →

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.