2010-11-13 79 views
0

可能這個寫簡單,代碼是在這裏:進行簡單[C#]

foreach (var friend in friends) 
{ 
    friend.Value.blockQuote = GetBlockQuote(friend.Value.nick); 

    if (friend.Value.photo == "0") 
    { 
     if (friend.Value.sex == 1) 
     { 
      var img = new BitmapImage(); 
      img.BeginInit(); 
      img.UriSource = new Uri(@"avatars\man.jpg", 
            UriKind.Relative); 
      img.EndInit(); 
      friend.Value.profilePhoto = img; 
     } 
     if (friend.Value.sex == 2) 
     { 
      //da default 
      var img = new BitmapImage(); 
      img.BeginInit(); 
      img.UriSource = new Uri(@"avatars\woman.jpg", 
            UriKind.Relative); 
      img.EndInit(); 
      friend.Value.profilePhoto = img; 
     } 
    } 
    else 
    { 
     var img = new BitmapImage(); 
     img.BeginInit(); 
     img.UriSource = new Uri(friend.Value.photo.Replace(@"\", "").Replace(@"s_", ""), UriKind.Absolute); 
     img.EndInit(); 
     friend.Value.profilePhoto = img; 
    } 
} 

回答

3

擺脫URI設定部

foreach (var friend in friends) 
{ 
    friend.Value.blockQuote = GetBlockQuote(friend.Value.nick); 

    Uri uri; 
    if (friend.Value.photo == "0") 
    { 
     if (friend.Value.sex == 1) 
     { 
      uri = new Uri(@"avatars\man.jpg", UriKind.Relative); 
     } 
     else if (friend.Value.sex == 2) 
     { 
      //da default 
      uri = new Uri(@"avatars\woman.jpg", UriKind.Relative); 
     } 
     else 
     { 
      uri = null; // insert error handling here 
     } 
    } 
    else 
    { 
     uri = new Uri(friend.Value.photo.Replace(@"\", "").Replace(@"s_", ""), UriKind.Absolute); 
    } 
    var img = new BitmapImage(); 
    img.BeginInit(); 
    img.UriSource = uri; 
    img.EndInit(); 
    friend.Value.profilePhoto = img; 
} 

編輯
注意的是,如果其他部分現在是Refactor-一個很好的候選人>提取方法

+0

很好的回答,但一個小挑剔:這可能會給你一個未初始化的變量錯誤,因爲'uri'沒有明確分配會發生什麼。如果'friend.Value.sex'不是1或2? – 2010-11-13 19:45:52

+0

@Jim米契爾,已經發現和更新的例子。 – 2010-11-13 19:46:41

+0

@Jim米契爾,這實際上宣告爲什麼當我不初始化'uri'變量,捕捉這些錯誤,但我的頭沒有工作那還有一個編譯器... – 2010-11-13 19:48:37

2

你可以通過把他們之前

var img = new BitmapImage(); 
img.BeginInit(); 

img.EndInit(); 
friend.Value.profilePhoto = img; 

分解出的線(對於前者)和之後(對於後者)if/else塊。

1
foreach (var friend in friends) 
{ 
    friend.Value.blockQuote = GetBlockQuote(friend.Value.nick); 
    var img = new BitmapImage(); 
    img.BeginInit(); 

    if (friend.Value.photo == "0") 
    { 
     if (friend.Value.sex == 1) 
     { 
      img.UriSource = new Uri(@"avatars\man.jpg", 
     } 
     if (friend.Value.sex == 2) 
     { 
      img.UriSource = new Uri(@"avatars\woman.jpg", 
               UriKind.Relative); 
     } 
    } 
    else 
    { 
      img.UriSource = new Uri(friend.Value.photo.Replace(@"\", "").Replace(@"s_", ""), UriKind.Absolute); 

    } 

    img.EndInit(); 
    friend.Value.profilePhoto = img; 
} 
0

1)一開始,嘗試這樣的事情:

var actualFriend = friend.Value; 

然後用actualFriend(或任何你的名字)取代friend.Value所有出現。


2)內幾乎每一個代碼塊,你正在創建一個BitmapImage,而你總是做在完全相同的方式。唯一不同的是img.UriSource = ...系列。因此,請執行if語句之外的通用代碼,例如像這樣:

Uri uri; // this will need to be initialized in every code branch below, 
      // or you'll get a compiler error or warning (which is a good thing). 

if (actualFriend.... == ...) 
{ 
    if (...) 
    { 
     uri = new Uri("..."); 
    } 
    else 
    { 
     uri = new Uri("..."); 
    } 
} 
else 
{ 
    uri = new Uri("..."); 
} 

var img = new BitmapImage(); 
img.BeginInit(); 
img.UriSource = uri; 
img.EndInit(); 
friend.Value.profilePhoto = img; 

3)此無關立即用簡化代碼,但(恕我直言)爲什麼不擺脫if sex == 1或的。至少據我所知,沒有性,如「1」或「2」,那麼,爲什麼在你的代碼有這種毫無意義的編碼時,你至少可以使用enum(如enum Sex { Male, Female, Intersex },它會讓你的代碼更可讀,更易於維護,並可能防止錯別字或記錯「性號」