Computer Science I / Modular Decomposition and Reuse
Practice question · Put in order

Order the steps of factoring duplicated logic into one reusable unit.

Hints
  1. You cannot design the parameters until you know what varies between the copies.
  2. The payoff step is last, and it is the reason the earlier steps were worth doing.
Show the answer
  1. Notice that the same logic appears in several places
  2. Compare the copies and identify what actually differs between them
  3. Write one function whose parameters cover those differences
  4. Replace each copy with a call to that function
  5. Change the rule once, in the single remaining definition
Why

The differences between copies determine the parameters, so they must be found before the shared function can be written; the call sites are then replaced, and only after that does a change become a one-place edit. Jumping straight to writing the function is how people produce a shared routine that does not actually fit every call site.

Read the lesson: Modular Decomposition and Reuse →

Practise Modular Decomposition and Reuse

The app has 6 more questions on this lesson, and keeps your place in the course. Computer Science I is free to start.

More questions on Modular Decomposition and Reuse