我目前有兩種方法:結合類似的方法
CalculateDaily()
{
List<string> tempList;
// Effective query. not what is really passed
tempList = "SELECT timestamp FROM table1 WHERE date = today";
var total = tempList.Sum();
}
和:
CalculateTotal()
{
List<string> tempList;
// Effective query. not what is really passed
tempList = "SELECT timestamp FROM table1"
var total = tempList.Sum();
}
我的問題是我應該讓他們分開,或者將它們組合成一個單一的方法可行,運行if
檢查?喜歡的東西:
Calculate(bool daily)
{
List<string> tempList;
if(daily)
tempList = "SELECT timestamp FROM table1 WHERE date = today";
else
tempList = "SELECT timestamp FROM table1";
var total = tempList.Sum();
}
首選的做法是分解和減少耦合,而不是增加複雜性。一種功能,一種責任。但更好的是下面的建議,以結合常見的然後是子類。 – dkretz