我在c#中有這個方法,我希望重構它。有太多的布爾和線條。什麼是最好的重構。製作一個新班級看起來有點矯枉過正,簡單地減少兩個班似乎很難。任何洞察力或指針將不勝感激。重構一個有太多bool的方法
方法重構
private DialogResult CheckForSireRestrictionInSubGroup(bool deletingGroup,string currentId)
{
DialogResult result = DialogResult.No;
if (!searchAllSireList)
{
DataAccessDialog dlg = BeginWaitMessage();
bool isClose = false;
try
{
ArrayList deletedSire = new ArrayList();
ISireGroupBE sireGroupBE = sireController.FindSireGroupSearch();
if (sireGroupBE != null)
{
//if the current group is in fact the seach group before saving
bool currentGroupIsSeachGroup = sireGroupBE.TheSireGroup.id == currentId;
//if we have setting this group as search group
bool selectedAsSearchGroup = this.chkBoxSelectedSireGroup.Checked;
//if the group we currently are in is not longer the seach group(chk box was unchecked)
bool wasSearchGroup = currentGroupIsSeachGroup && !selectedAsSearchGroup;
//if the group is becoming the search group
bool becomesSearchGroup = !currentGroupIsSeachGroup && selectedAsSearchGroup;
//if the group being deleted is in fact the search group
bool deletingSearchGroup = deletingGroup && currentGroupIsSeachGroup;
//if the user checked the checkbox but he's deleting it, not a so common case, but
//we shouldn't even consider to delete sire in this case
bool deletingTemporarySearchGroup = deletingGroup && !currentGroupIsSeachGroup;
//if we are not deleting a temporary search group and it's either
//becoming one (without deleting it) or we already are the search group
bool canDeleteSires = !deletingTemporarySearchGroup &&
(becomesSearchGroup || currentGroupIsSeachGroup);
//we only delete sires if we are in search group
if (canDeleteSires)
{
if (deletingSearchGroup || wasSearchGroup)
{
// If we deleted all sires
deletedSire = new ArrayList();
deletedSire.AddRange(sireGroupBE.SireList);
}
else
{
//if we delete a few sire from the change of search group
deletedSire = GetDeleteSire(sireGroupBE.SireList);
}
}
EndWaitMessage(dlg);
isClose = true;
result = ShowSubGroupAffected(deletedSire);
}
}
finally
{
if (!isClose)
{
EndWaitMessage(dlg);
}
}
}
return result;
}
這看起來像表達邏輯清晰的方式 - 它很容易閱讀,也很好評論。我根本不會碰它。 – dasblinkenlight 2012-01-13 14:46:20
同意....它可能很長,但它是可讀的。 – 2012-01-13 14:49:17
我認爲當前的代碼是可讀的,但會減少方法的目標,刪除條目。布爾邏輯可以保持原樣並遷移到另一種方法,這樣主方法就可以減少「支持」代碼,但不能解決刪除東西的主要問題。 – 2012-01-13 15:17:17