2

我怎样才能使它看起来更好:

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

它看起来真的很乱,我似乎找不到任何这样的问题。

4

5 回答 5

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- 我认为这不是故意的。

于 2012-07-27T18:21:05.983 回答
2

用于Path.Combine路径创建,而不是像您那样进行字符串连接。

于 2012-07-27T18:24:02.290 回答
0

这种方法有可能做不止 1 件事吗?你也许可以用其他一些方法来重构它。

于 2012-07-27T18:22:53.740 回答
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.";
}
于 2012-07-27T18:29:47.790 回答
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.";
    }
}

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

于 2012-07-27T18:32:49.260 回答