編輯:我對特定編程實踐或編碼方式更感興趣,以避免這種情況。但任何建議都是值得歡迎的。在C#代碼中簡化case-if-else梯形圖
我寫了這個難看的代碼來執行某些任務:
switch(arg[0])
{
case "a"
{
foreach(var c in obja)
{
if(c.entry == 'a')
{
if (Independent_condition_1)
{
// do logging
continue;
}
if (Independent_condition_2)
{
// do logging
continue;
}
// Do something if above two conditions are false
}
else
{
if (Independent_condition_3)
{
// do logging
continue;
}
if (Independent_condition_4)
{
// do logging
continue;
}
// do something if above 2 conditions are false.
}
}
break;
}
case "b"
{
// similar if else ladder
}
}
問題是代碼看起來很醜,是非常不可讀。爲了簡化代碼,我將if-else中的大部分內容移到了函數中。這種長嵌套if-else的替代方案是什麼?
問題是更適合HTTP: //codereview.stackexchange.com/ – venerik