-4

我有一小段代码,每个都在使用。我已经优化了我的代码,但我的老板希望我进一步优化它。我不知道可以在这里进行哪些进一步的优化。

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'”。

4

4 回答 4

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);
    }                     
}
于 2013-10-22T15:16:52.867 回答
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();
于 2013-10-22T15:17:30.260 回答
4

好吧,这并不是真正的优化,但是您的代码更简单:

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

(不清楚你在哪里声明,但df声明中声明它是有意义的,假设你实际上并没有在其他地方使用它。或者完全摆脱它,只使用.ifDownloadFileList.Add(new DownloadFileStruct(...))

于 2013-10-22T15:17:48.133 回答
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);
    }

}
于 2013-10-22T15:24:57.413 回答