简单[C#]

时间:2010-11-13 19:37:20

标签: 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;
    }
}

4 个答案:

答案 0 :(得分:3)

打破uri-setting部分

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;
}

修改
请注意,if-else-part现在是Refactor-> Extract方法

的良好候选者

答案 1 :(得分:2)

你可以分辨出行

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

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

将它们放在(前者)之前和之后(后者)if / else块。

答案 2 :(得分: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;
}

答案 3 :(得分:0)

1)首先,请尝试以下方法:

var actualFriend = friend.Value;

然后将friend.Value的所有出现替换为actualFriend(或任何您的名字)。


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 == 1sex == 2。至少据我所知,没有“1”或“2”这样的性别,所以当你至少使用enum时,为什么在你的代码中有这种无意义的编码(例如enum Sex { Male, Female, Intersex }它会让你的代码更具可读性,更易于维护,并且可以防止错别字或错误地记住“性别数字”。