2010-11-13 21 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 },它会让你的代码更可读,更易于维护,并可能防止错别字或记错“性号”