Split Variable

Ben Butler
Jul 15, 2021

--

The Split Variable refactoring comes to play when a variable gets overwritten without having been used.

let price = amount + surcharge;
price = price — discount;
price = price * tax;

This leaves the code difficult to maintain, especially if the variable is overwritten far downstream. It is easy for readers to identify the one place a final variable is defined. Creating a separate variable to represent a state and naming them self-comments the code and lets us be reassured that the variable is final in a given transaction.

const initialPrice = amount + surchase;
const dscPrice = initialPrice — discount;
const finalPrice = dscPrice * tax;

--

--

No responses yet