liveBook · Manning

Reuse

As explained in Grokking Simplicity

The practice of using existing code in multiple places or contexts instead of duplicating its implementation.

Code reuse means using existing code in multiple places or contexts instead of duplicating its implementation. Smaller functions are easier to reuse because they do less and make fewer assumptions. section 5.7 section 9.18

Use these design rules to make reuse more likely:

  • Pull concerns apart into small functions that can be composed again. section 5.7
  • Extract functions into lower layers of a stratified design; code with fewer functions underneath it is more reusable. section 9.18 section 9.19
  • Prefer named callbacks over inline callbacks when a smaller function can serve more than one context. section 13.3 section 13.2

Extracting reusable callbacks

A customer-processing chain becomes more reusable when inline callbacks are named: filter(customers, isGoodCustomer) and map(bestCustomers, getBiggestPurchase). isGoodCustomer works on one customer, while the surrounding selection function only works on an array. The smaller function can therefore be applied in other contexts. section 13.3 section 13.2

Image

Use the figure to visualize how lower layers create opportunities for reuse and how call-graph depth affects it. section 9.18

How Grokking Simplicity covers it

How deep it goes
Explains reuse with definitions, call-graph rules, and JavaScript refactorings rather than language-runtime internals.
Examples
Demonstrates reuse through extracted customer-selection callbacks, lower-layer functions, and call-graph comparisons.
What it assumes
Assumes familiarity with functions, arguments, callbacks, and call graphs while teaching their design consequences through examples.
What you can do after
Apply reuse through function extraction, lower-layer placement, and named callbacks that reduce assumptions and duplication.
How it connects
Connects reuse with stratified design, abstraction barriers, higher-order functions, maintenance, testing, and onion architecture.

Also explained in

Compare how 6 books explain reuse →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking Simplicity. The section links point to the parts of the book it draws on.