Decompose Conditional(条件記述の分解)


HOME > プログラミング > リファクタリング > Decompose Conditional リファクタリング プログラムの体質改善テクニック

Decompose Conditionalとは、複雑な条件文をメソッドに切り出して可読性を向上させるリファクタリングです。
複雑な条件判断文をbool型を返すメソッドとして抽出するとコードが見やすくなります。

リファクタリング前:

public double GetMemberDiscountRate(Customer customer)
{
	if(customer.MemberFlag = true && customer.TotalYearPurchase >= 1000 && customer.TotalYearPurchase < 2000)
	{
		return 0.1;
	}
	else if(customer.MemberFlag = true && customer.TotalYearPurchase >= 2000)
	{
		return 0.2;
	}
	
	return 0;
}
	  

リファクタリング後:

public double GetMemberDiscountRate(Customer customer)
{
	if(IsGoldMemeber(customer))
	{
		return 0.1;
	}
	else if(IsPlatinumMemeber(customer))
	{
		return 0.2;
	}
}


public bool IsGoldMember(Customer customer)
{
	return (customer.MemberFlag = true && customer.TotalYearPurchase >= 1000 && customer.TotalYearPurchase < 2000);
}

public bool IsPlatinumMember(Customer customer)
{
	return (customer.MemberFlag = true && customer.TotalYearPurchase >= 2000);
}
	  

Copyright(c) 2012 WoodenSoldier Software