2013-10-22 51 views
-4

我有一小塊代碼,每個代碼都在使用。我已經優化了我的代碼,但是我的老闆希望我進一步優化它。我不知道在這裏可以做進一步的優化。優化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'。

+0

這也許應該是在[codereview.se] – MikeTheLiar

+9

你真的告訴我們,你可以正則表達式匹配,而不是兩個邏輯語句合併成一條? – nvoigt

+2

他並沒有要求*優化*在我們通常意義上(性能改進)的意義上,但似乎建議改進代碼的組織方式以使其更易於維護*。 – crashmstr

回答

5

您可以簡化代碼這樣:

foreach (Match match in matches) 
{ 
    if (Override && !match.Groups[2].Value.Equals(OverrideFileName)) 
     continue; 

    df = new DownloadFileStruct(match.Groups[1].Value, match.Groups[2].Value); 
    DownloadFileList.Add(df); 
} 

或LINQ-ISH:

DownloadFileList = 
    matches.Cast<Match>() 
      .Where(x => !Override || x.Groups[2].Value.Equals(OverrideFileName)) 
      .Select(x => new DownloadFileStruct(x.Groups[1].Value, 
               x.Groups[2].Value)) 
      .ToList(); 
5

只需在if中使用OR而不是重複兩次代碼。

它不是關於優化,它關於不必維護2個代碼分支來完成相同的事情。

foreach (Match match in matches) { 
    //When oveeride is false and you have to download all the files 
    if (Override.Equals(false) || match.Groups[2].Value.Equals(OverrideFileName)) { 
     //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); 
    }      
} 
4

那麼它不是真正的優化,但你的代碼那樣簡單:

if (!Override || match.Groups[2].Value == OverrideFileName) 
{ 
    var df = new DownloadFileStruct(match.Groups[1].Value, 
            match.Groups[2].Value); 
    DownloadFileList.Add(df); 
} 

(目前尚不清楚你在哪裏聲明df,但假設你沒有在別處實際使用它,那麼在if聲明中聲明是有意義的。或者完全擺脫它,只使用DownloadFileList.Add(new DownloadFileStruct(...))

+0

實際上這個df被用於不同的功能。因此,它必須在那裏 – user2754845

+4

@ user2754845:它被用於所有地方的同一事物嗎?你真的希望價值是「添加到列表中的最後一項」嗎?這對我來說聽起來很奇怪 - 總的來說,最好儘可能嚴格地限制每個局部變量的範圍。 –

0

嘗試這個

foreach (Match match in matches) { 
    //When oveeride is false and you have to download all the files 
    //OR 
    //When override is true and a paticular file has to be downloaded 
    if ((Override.Equals(false)) || (match.Groups[2].Value.Equals(OverrideFileName))) { 
     //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); 
    } 

}