我有一小塊代碼,每個代碼都在使用。我已經優化了我的代碼,但是我的老闆希望我進一步優化它。我不知道在這裏可以做進一步的優化。優化foreach的使用
foreach (Match match in matches) {
//When oveeride is false and you have to download all the files
if (Override.Equals(false)) {
//putting the matches from regular expression into DownloadFileStruct oject
df = new DownloadFileStruct(match.Groups[1].Value, match.Groups[2].Value);
//Adding DownloadFileStruct object to a array list
DownloadFileList.Add(df);
}
//When override is true and a paticular file has to be downloaded
else if (match.Groups[2].Value.Equals(OverrideFileName)) {
//putting the matche from regular expression into a DownloadFileStruct oject
df = new DownloadFileStruct(match.Groups[1].Value, match.Groups[2].Value);
//Adding DownloadFileStruct object to a array list
DownloadFileList.Add(df);
}
}
}
我的老闆說「你不需要在兩個分支中都執行相同代碼的'if'和'else if'。
這也許應該是在[codereview.se] – MikeTheLiar
你真的告訴我們,你可以正則表達式匹配,而不是兩個邏輯語句合併成一條? – nvoigt
他並沒有要求*優化*在我們通常意義上(性能改進)的意義上,但似乎建議改進代碼的組織方式以使其更易於維護*。 – crashmstr