Extracting Shared Code After Comparing Behaviors

Two similar-looking routines can be combined when their compatible behavior and meaningful variations are understood.

Source
Anbloq
Language
English
Created
Updated
Chapters
2
Reading time
1 min
In this article2 chapters
  1. Trace two importers
  2. Keep prior callers covered

Trace two importers

Both parse numeric quantities, but one permits zero and the other requires a positive value. A shared parser can convert text while callers supply domain rules. See parsing and validation.

Keep prior callers covered

Use separate tests for boundary cases that each caller relied on before the refactor. Contracts for replacements explain which observations need preservation.[1]

References

  1. David L. Parnas, On the Criteria to Be Used in Decomposing Systems into Modules, CMU-CS-71-101 ↩