如果陈述看起来更好

时间:2012-07-27 18:17:14

标签: c# if-statement

如何让这个看起来更好看:

lblTotalWorldSize.Text = GetDirectorySize(_worldsDirectory + selectedItem) * 1024 + " kb";    // Total world size
if (Directory.Exists(_worldsDirectory + selectedItem + "\\" + selectedItem))                  // World itself
{
    lblWorldSize.Text = GetDirectorySize(_worldsDirectory + selectedItem + "\\" + selectedItem) * 1024 + " kb";
}
else
{
    lblWorldSize.Text = "Couldn't find world.";
}
if (Directory.Exists(_worldsDirectory + selectedItem + "\\" + selectedItem + "_nether"))      // Nether
{
    lblNetherSize.Text = GetDirectorySize(_worldsDirectory + selectedItem + "\\" + selectedItem + "_nether") * 1024 + " kb";
}
else
{
    lblWorldSize.Text = "Couldn't find world.";
}
if (Directory.Exists(_worldsDirectory + selectedItem + "\\" + selectedItem + "_the_end"))     // The End
{
    lblTheEndSize.Text = GetDirectorySize(_worldsDirectory + selectedItem + "\\" + selectedItem + "_the_end") * 1024 + " kb";
}
else
{
    lblWorldSize.Text = "Couldn't find world.";
}

看起来真是一团糟,我似乎无法找到这样的问题。

5 个答案:

答案 0 :(得分:7)

嗯,有几件事可以帮到这里:

  • 辅助方法(每次执行从“路径”到“大小字符串”的相同转换)
  • 条件运算符
  • 提取公共局部变量

这样的事情:

// Note that _worldsDirectory must be an absolute path)
string prefix = Path.Combine(_worldsDirectory, selectedItem, selectedItem);
lblWorldSize.Text = GetDirectorySizeOrDefault(prefix, "Couldn't find world");
lblNetherSize.Text = GetDirectorySizeOrDefault(prefix + "_nether",
                                               "Couldn't find _nether");
lblTheEndSize.Text = GetDirectorySizeOrDefault(prefix + "_the_end",
                                               "Couldn't find _the_end");

...

static string GetDirectorySizeOrDefault(string directory, string defaultText)
{
    return Directory.Exists(directory)
        ? GetDirectorySize(directory) * 1024 + " kb"
        : defaultText;
}

请注意,我已经更正了原始代码总是在出错时分配给lblWorldSize.Text的事实 - 我认为这不是故意的。

答案 1 :(得分:2)

使用Path.Combine创建路径,而不是像您一样使用字符串连接。

答案 2 :(得分:0)

这种方法可能超过1件吗?你可以用其他一些方法重构这个。

答案 3 :(得分:0)

我建议使用意图和变量替换来增强可读性:

string dir = "_worldsDirectory + selectedItem";
string dirItemPath = @"_worldsDirectory + selectedItem \\" + selectedItem;

// Total World Size
lblTotalWorldSize.Text = GetDirectorySize(_worldsDirectory + selectedItem) * 1024 + " kb";

// World itself
if (Directory.Exists(dirItemPath)) {

    lblWorldSize.Text = GetDirectorySize(dirItemPath) * 1024 + " kb";
} else {

     lblWorldSize.Text = "Couldn't find world.";
}

// Nether
if (Directory.Exists(dirItemPath + "_nether")) {

    lblNetherSize.Text = GetDirectorySize(dirItemPath + "_nether") * 1024 + " kb";
} else {

    lblWorldSize.Text = "Couldn't find world.";
}

// The End
if (Directory.Exists(dirItemPath + "_the_end")) {

    lblTheEndSize.Text = GetDirectorySize(dirItemPath + "_the_end") * 1024 + " kb";
} else {

    lblWorldSize.Text = "Couldn't find world.";
}

答案 4 :(得分:0)

lblTotalWorldSize.Text = GetDirectorySize(_worldsDirectory + selectedItem) * 1024 + " kb";    // Total world size

DoOnDirectoryExists(lblWorldSize, "");    
DoOnDirectoryExists(lblNetherSize, "_nether");    
DoOnDirectoryExists(lblWorldSize, "_the_end");    


//Change the name to something more appropriate to your program, same with the LBL type and tempString
public bool DoOnDirectoryExists(LBL lbl, string suffix)
{
    string tempString = _worldsDirectory + selectedItem + "\\" + selectedItem + suffix;

    if (Directory.Exists(tempString))     
    {
         lbl.Text = GetDirectorySize(tempString) * 1024 + " kb";
    }
    else
    {
        lblWorldSize.Text = "Couldn't find world.";
    }
}

摆脱混乱并使代码更具可读性的最佳方法 - 删除代码重复。