目的
「リファクタリング」を理解するためにサンプルコードを PHP で書き換えてみました。
今回は「パラメータへの代入の除去」について書きます。
「パラメータへの代入の除去」 について
引数への代入が行われている。
一時変数を使う
例
変更前
function discountVersion1($inputVal, $quantity, $yearToDate)
{
if ($inputVal > 50) $inputVal -= 2;
return $inputVal;
}
echo discountVersion1(100, 2, 2018) . "\n";
変更後
function discountVersion2($inputVal, $quantity, $yearToDate)
{
$result = $inputVal;
if ($inputVal > 50) $result -= 2;
return $result;
}
echo discountVersion2(100, 2, 2018) . "\n";