2013-04-23 51 views
0

這僅僅是一個「最佳實踐」的問題...評估一系列條件和停止條件時遇到

我有一個功能,其採用輸入字符串,然後具有基於內容,但一旦改變它滿足特定的條件,則所有進一步處理停止。

目前,我使用的是「而(真)」循環,然後「破發」時,我已經得到了我想要的東西,下面是僞代碼..

string Input = "xyz"; 
string Output = string.Empty; 
while (true) 
{ 
    if (Input.StartsWith("x")) 
    { 
     Output = "string starts with an X"; 
     break; 
    } 

    if (Input.Contains("y")) 
    { 
     Output = "string has a 'y' in it"; 
     break; 
    } 

    if (Input.IndexOf("z") == 2) 
    { 
     Output = "string has a 'z' as the 3rd character"; 
     break; 
    } 

    Output = "string does not match any conditions"; 
    break; 
} 

有沒有更「純粹主義」的方式達到上述目的?

感謝

+0

[CodeReview.SE]會更好嗎? – 2013-04-23 08:12:52

+0

在我以前用CA-Clipper編碼的美好時代,有BEGIN SEQUENCE .... END SEQUENCE的代碼構造,你可以在...之間「打破」... http://www.itlnet .net/programming/program/reference/c53g01c/ngfc7b7.html – 2013-04-23 09:21:18

回答

4

你應該在這裏使用標準if-ifelse-else。這對你的情況更爲常見和可讀。

string Input = "xyz"; 
string Output = string.Empty; 

if (Input.StartsWith("x")) 
{ 
    Output = "string starts with an X"; 
} 
else if (Input.Contains("y")) 
{ 
    Output = "string has a 'y' in it"; 
} 
else if (Input.IndexOf("z") == 2) 
{ 
    Output = "string has a 'z' as the 3rd character"; 
} 
else 
{ 
    Output = "string does not match any conditions"; 
} 

更新

第二個版本,使用LINQ。您可以創建條件函數和所需輸出的List,然後使用FirstOrDefault獲得第一個匹配條件。

string Input = "xyz"; 
string Output = string.Empty; 

var conditionList = new List<Tuple<Func<string, bool>, string>>(); 
conditionList.Add(Tuple.Create<Func<string, bool>, string>((string x) => x.StartsWith("x"), "string starts with x")); 
conditionList.Add(Tuple.Create<Func<string, bool>, string>((string x) => x.Contains("y"), "string has a 'y' in it")); 
conditionList.Add(Tuple.Create<Func<string, bool>, string>((string x) => x.IndexOf("z") == 2, "string has a 'z' as the 3rd character")); 

var firstMatch = conditionList.FirstOrDefault(x => x.Item1(Input)); 
Output = firstMatch != null ? firstMatch.Item2 : "string does not match any conditions"; 
+0

我接受你在說什麼,但上面的例子只是一個簡單的解釋。我正在做的實際過程有相當數量的 – 2013-04-23 08:24:52

0

我認爲if else if..應該就足夠了。你做到這一點,如果你重構你的代碼,忘記最後的break;你可能會面臨更大的問題。

+0

我接受你在說什麼,但上面的例子只是一個簡單的解釋。 我正在做的實際過程有相當數量。 – 2013-04-23 08:16:20

+0

您可以爲每個鍵(一個函數即:((input)=> input.Contains(「y」))創建一個'Disctionary ,string>'影響相應的輸出字符串'「字符串中有'y')'')使用LINQ從字典中讀取並返回適當的輸出字符串。 – 2013-04-23 08:27:03

1

正如你所說,這是一個更大的問題的只是一個簡單的例子,我可能會做這樣的事情(當然這是矯枉過正的一個小例子,但它做的非常好):

public interface ICondition 
{ 
    bool IsMatch(string input); 
    string GetMessage(); 
} 

public class StartsWithTest : ICondition 
{ 
    public bool IsMatch(string input) 
    { 
     return input.StartsWith("x"); 
    } 

    public string GetMessage() 
    { 
     return "string starts with an X"; 
    } 
} 


public class TestInput 
{ 

    private readonly IList<ICondition> _conditions; 

    public TestInput() 
    { 
     _conditions = new List<ICondition>(); 

     _conditions.Add(new StartsWithTest()); 
     //etc etc 
    } 

    public string Test(string input) 
    { 
     var match = _conditions.FirstOrDefault(c => c.IsMatch(input)); 

     if (match != null) 
      return match.GetMessage(); 
     else 
      return string.Empty; 
    } 
} 
+0

優雅的解決方案! – 2013-04-23 08:40:51