Skip to content
C.W.K.
Stream
Lesson 02 of 04 · published

Extract at Second Use

~11 min · reuse, duplication, evidence, refactoring

Level 0Cold Vessel
0 XP0/36 lessons0/12 achievements
0/100 XP to next level100 XP to go0% complete

Extract at Second Use

The first implementation contains facts and guesses in the same file. You know the behavior one product needs, but you do not yet know which parts another product will share. Extracting a framework now freezes guesses into an API and makes the only consumer pay abstraction costs with no proof of reuse.

The second real consumer changes the evidence. You can place the two implementations side by side and ask where behavior, failure modes, and change pressure match. Identical text is only a clue. Two functions can look different and own the same invariant; two copied functions can look identical while one has already acquired a domain exception.

Promotion at second use is therefore a measurement rule, not a slogan against duplication. The first copy gives you a concrete baseline. The second gives you a comparison. The extraction is the third move, performed with both consumers under tests so the common contract is observed rather than imagined.

After extraction, the original products should become thin adapters, not abandoned fossils. Their tests continue to assert product behavior, while kernel tests assert shared behavior. A future change that touches only one adapter is evidence that the seam remains healthy, not proof the abstraction failed.

Compare Change Pressure

Before extracting, inspect the last several changes in both consumers. If the same requirement caused parallel edits, you found stronger evidence than matching lines. If one file changed for product reasons and the other stayed still, keep that policy out of the kernel even when the code happens to match today.

The second example reveals the stable seam. One occurrence shows that work happened; two comparable occurrences expose which instructions repeat and which details belong to the instance. Extract only the common behavior, and keep both source records as evidence for the choice.

Code

Detect shared behavior with consumer tests·python
def normalize_stages(items: list[str]) -> tuple[str, ...]:
    return tuple(dict.fromkeys(x.strip() for x in items if x.strip()))

# Consumer A and B discovered the same invariant independently.
assert normalize_stages(["plan-gate", "review", "review"]) == ("plan-gate", "review")
assert normalize_stages(["scope", "", "scope"]) == ("scope",)
print("the observed seam has executable examples")

External links

Exercise

Find one apparent duplicate in two consumers. Compare their last three changes and write the smallest behavior contract that actually moved together.
Hint
Do not start from line similarity; start from tests and reasons for change.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.