Skip to content

Latest commit

 

History

History
47 lines (31 loc) · 817 Bytes

File metadata and controls

47 lines (31 loc) · 817 Bytes

Name Value

Relevant To

  • Extract Constant
  • Extract Variable

Relates To

Languages

All

Motivation

As a function's expression gains complexity, it is behaviour becomes harder to comprehend. Giving names to intermediate steps of the expression can significantly increase clarity.

Description

Identify groups in the expression which perform a specific task, then store them as values with meaningful names.

Examples

Clojure

Before

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

After

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