2012-07-27 80 views
2

我怎樣才能讓這個更好看:讓if語句看起來更好

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

這真的看起來像一個爛攤子,我似乎無法找到任何這樣的問題。

+1

在每個* else中賦值給'lblWorldSize'是否有錯誤? – 2012-07-27 18:22:32

回答

7

好,有幾件事情,可以幫助在這裏:

  • 的helper方法
  • 條件運算符(你是從「路徑」爲「大小的字符串」每次都執行相同的轉換)
  • 提取一個共同的局部變量

事情是這樣的:

// 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 - 我認爲這不是故意的。

+0

喜歡製作一個可以檢查每個文件夾的功能嗎? – 2012-07-27 18:22:20

+0

@FoxyShadoww:看我的編輯。 – 2012-07-27 18:23:34

+0

我明白了!這確實使它看起來更好。順便說一句,如果我以另一種方式來做(我現在怎麼做),它會比你的方式慢嗎?好奇的 – 2012-07-27 18:26:32

0

這種方法有可能做超過1件事嗎?也許你可以用其他幾種方法來重構它。

2

使用Path.Combine進行路徑創建,而不是像你一樣進行字符串連接。

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

最佳途徑,使你的代碼更易讀 - 刪除代碼重複。