Skip to content

Latest commit

 

History

History
50 lines (34 loc) · 1.04 KB

extract-function.md

File metadata and controls

50 lines (34 loc) · 1.04 KB

Extract Function

Relevant To

  • Extract Method
  • Single Responsibility Principal

Relates To

Languages

All

Motivation

As a function's expression gains complexity, it is behaviour becomes harder to comprehend. Extracting a new, well named, function for different parts of that function can significantly increase clarity.

Creating smaller functions, with a single responsibility, promotes more opportunities for code reuse via composition.

Description

Identify groups in the expression which perform a specific task, then extract it to a new, well-named function.

Examples

Clojure

Before

(defn order-total
  [tax-rate items]
  (* (sum (map :amount items)) (+ 1 (/ tax-rate 100))))

After

(defn tax-multiplier [rate] (+ 1 (/ rate 100)))
(defn with-tax [rate amount] (* amount (tax-multiplier rate)))
(defn total-amount [items] (sum (map :amount items)))

(defn order-total
  [tax-rate items]
  (with-tax tax-rate (total-amount items)))