Replace Parameter with Methodとは、あるメソッドの結果をすぐに他のメソッドのパラメーターに代入しているときに、コードを簡単にすることができます。
このような場合には、2つめのメソッドの内部で、1つめのメソッドを呼ぶようにすれば、よけいなパラメーター渡しを行わなくて良くなる場合が多いです。
パラメーターはできるだけ無くすというのがオブジェクト指向風なプログラムになります。
リファクタリング前:
private void DisplayStars()
{
int index = Convert.ToInt32(this.rating * 2);
pictureBoxStar.Image = GetImageByIndex(index);
}
private Image GetImageByIndex(int index)
{
if(style = Style.Mac)
{
return imageListMacStar.Images[index];
}
return imageListDefaultStar.Images[index];
}
リファクタリング後:
private void DisplayStars()
{
pictureBoxStar.Image = GetImageByIndex();
}
private Image GetImageByIndex()
{
if(style = Style.Mac)
{
return imageListMacStar.Images[Convert.ToInt32(this.rating * 2)];
}
return imageListDefaultStar.Images[Convert.ToInt32(this.rating * 2)];
}
|
Copyright(c) 2012 WoodenSoldier Software |