欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#優(yōu)化if...else代碼的方案總結(jié)

 更新時(shí)間:2024年06月16日 09:42:20   作者:碼農(nóng)浩克  
在編寫(xiě)代碼實(shí)現(xiàn)業(yè)務(wù)需求過(guò)程中,會(huì)使用到大量的if...else 判斷語(yǔ)句,隨業(yè)務(wù)復(fù)雜程度不同,導(dǎo)致判斷語(yǔ)句出現(xiàn)多層嵌套、多分支等情況,導(dǎo)致代碼可讀性變差、增加維護(hù)難度,本文介紹了C# 如何優(yōu)化 if...else 讓代碼優(yōu)雅起來(lái),需要的朋友可以參考下

前言

在編寫(xiě)代碼實(shí)現(xiàn)業(yè)務(wù)需求過(guò)程中,會(huì)使用到大量的if...else 判斷語(yǔ)句。隨業(yè)務(wù)復(fù)雜程度不同,導(dǎo)致判斷語(yǔ)句出現(xiàn)多層嵌套、多分支等情況,導(dǎo)致代碼可讀性變差、增加維護(hù)難度。要減少、消除這種復(fù)雜的、面條式代碼,需要編寫(xiě)代碼時(shí)多注意編程風(fēng)格,盡量規(guī)避if...else 的復(fù)雜化。學(xué)習(xí)一些代碼的優(yōu)化之道,培養(yǎng)自己的易讀、美觀代碼風(fēng)格。

方案

1、提前 return ,去除不必要的 else

缺點(diǎn):對(duì)較多的 if...else 沒(méi)什么大的效果。

示例:

#region 優(yōu)化前
if (string.IsNullOrEmpty(this.txtOrderNo.Text))
{
    MessageBox.Show("訂單號(hào)不能為空,請(qǐng)輸入訂單號(hào)。");
    this.txtOrderNo.Focus();
}
else
{
    // 出處業(yè)務(wù)邏輯
}
#endregion


#region 優(yōu)化后
if (string.IsNullOrEmpty(this.txtOrderNo.Text))
{
    MessageBox.Show("訂單號(hào)不能為空,請(qǐng)輸入訂單號(hào)。");
    this.txtOrderNo.Focus();
    return;
}
// 出處業(yè)務(wù)邏輯
#endregion

2、使用條件三目運(yùn)算符

缺點(diǎn):對(duì)只有 一個(gè) if...esle 兩個(gè)選擇時(shí),才會(huì)有效果。如果有多個(gè)時(shí),閱讀理解會(huì)更費(fèi)力。

示例:

#region 優(yōu)化前
if ("CaiNiao".Equals(templateType, StringComparison.OrdinalIgnoreCase))
{
    // 設(shè)置按鈕可見(jiàn)
    this.btnCaiNiao.Visible = true;
}
else
{
    // 設(shè)置按鈕不可見(jiàn)
    this.btnCaiNiao.Visible = false;
}
#endregion


#region 優(yōu)化后
// 設(shè)置按鈕是否可見(jiàn)
this.btnCaiNiao.Visible = "CaiNiao".Equals(this.templateType, StringComparison.OrdinalIgnoreCase) ? true :  false;
#endregion

3、使用 switch

if...else 的使用場(chǎng)景,都能用 switch 代替,在分支選擇較多時(shí),盡量使用 switch。

示例:

#region 優(yōu)化前
if (keyValue==120)
{
    // 按 F9 快捷鍵光標(biāo)定位到產(chǎn)品輸入框
    this.txtProductNo.Focus();
}
else if (keyValue==121)
{
    // 按 F10 快捷鍵光標(biāo)定位到訂單輸入框
    this.txtOrderNo.Focus();
}
else if (keyValue==122)
{
    // 按 F11 快捷鍵審核
    this.ApproveOrder();
}
#endregion


#region 優(yōu)化后
switch (this.CurrentKeyValue)
{
    case 120:
        // 按 F9 快捷鍵光標(biāo)定位到產(chǎn)品輸入框
        this.txtProductNo.Focus();
        break;
    case 121:
        // 按 F10 快捷鍵光標(biāo)定位到訂單輸入框
        this.txtOrderNo.Focus();
        break;
    case 122:
        // 按 F11 快捷鍵審核
        this.ApproveOrder();
        break;
    default:
        break;
}
#endregion

4、合并條件表達(dá)式

梳理和歸納邏輯判斷,使其變更為更簡(jiǎn)單易懂的邏輯判斷代碼。

示例:

#region 優(yōu)化前
string expressCode ="DOUYIN-YTO"
string templateurl=string.Empty;
string[] splitCode = expressCode..Split('-');
if (splitCode.Length >= 2)
{
    if (expressCode.Contains(splitCode[1]))
    {
        templateurl="http://url.com/test.html"
    }
}
#endregion


#region 優(yōu)化后
string expressCode = "DOUYIN-YTO";
string templateurl = string.Empty;
string[] splitCode = expressCode.Split('-');
if (splitCode.Length >= 2 &&  expressCode.Contains(splitCode[1]))
{
    templateurl = "http://url.com/test.html";
}
#endregion

5、使用枚舉

將條件作為枚舉的值,這種可以避免使用大量的 if...else 語(yǔ)句。

示例:

#region 優(yōu)化前
int  platformID = 0;
string platformCode ="DOUYIN";
if ("PINDUODUO".Equals(platformCode, StringComparison.OrdinalIgnoreCase))
{
    platformID = 1;
}
else if ("DOUYIN".Equals(platformCode, StringComparison.OrdinalIgnoreCase))
{
    platformID = 2;
}
else if ("KUAISHOU".Equals(platformCode, StringComparison.OrdinalIgnoreCase))
{
    platformID = 3;
}
else if ("SHIPINHAO".Equals(platformCode, StringComparison.OrdinalIgnoreCase))
{
    platformID = 4;
}
else if ("AIKUCUN".Equals(platformCode, StringComparison.OrdinalIgnoreCase))
{
    platformID = 5;
}
else if ("XIAOHONGSHU".Equals(platformCode, StringComparison.OrdinalIgnoreCase))
{
    platformID = 6;
}
#endregion


#region 優(yōu)化后
// 定義一個(gè)數(shù)據(jù)枚舉
public enum Platform
{
    PINDUODUO=1,
    DOUYIN,
    KUAISHOU,
    SHIPINHAO,
    AIKUCUN,
    XIAOHONGSHU
}
Platform platform;
bool result=Enum.TryParse<Platform>("SHIPINHAO", out platform);
int platformID = Convert.ToInt32(platform);
// 需判斷SHIPINHAO是否枚舉
if (platformID==0)
{
    // 異常處理
}
#endregion

6、使用字典

將條件作為字典的鍵,將處理方法作為字典的鍵值。這樣可以避免使用大量的 if...else 語(yǔ)句。

示例:

#region 優(yōu)化前
int  platformID = 0;
string platformCode ="DOUYIN";
if ("PINDUODUO".Equals(platformCode, StringComparison.OrdinalIgnoreCase))
{
    platformID = 1;
}
else if ("DOUYIN".Equals(platformCode, StringComparison.OrdinalIgnoreCase))
{
    platformID = 2;
}
else if ("KUAISHOU".Equals(platformCode, StringComparison.OrdinalIgnoreCase))
{
    platformID = 3;
}
else if ("SHIPINHAO".Equals(platformCode, StringComparison.OrdinalIgnoreCase))
{
    platformID = 4;
}
else if ("AIKUCUN".Equals(platformCode, StringComparison.OrdinalIgnoreCase))
{
    platformID = 5;
}
else if ("XIAOHONGSHU".Equals(platformCode, StringComparison.OrdinalIgnoreCase))
{
    platformID = 6;
}
#endregion


#region 優(yōu)化后
// 定義一個(gè)數(shù)據(jù)字典
Dictionary<string,int> platformDictionary = new Dictionary<string,int>();
platformDictionary.Add("PINDUODUO",1);
platformDictionary.Add("DOUYIN", 2);
platformDictionary.Add("KUAISHOU", 3);
platformDictionary.Add("SHIPINHAO", 4);
platformDictionary.Add("AIKUCUN", 5);
platformDictionary.Add("XIAOHONGSHU", 6);


int platformID = 0;
string platformCode = "DOUYIN";
if (platformDictionary.ContainsKey(platformCode))
{
    platformID = platformDictionary[platformCode];
}
#endregion

小結(jié)

以上介紹了一些優(yōu)化 if...else 語(yǔ)句的方法,使代碼更加清晰、易于維護(hù)和擴(kuò)展。當(dāng)然還有其它優(yōu)化方法(可留言提供你的方法一起進(jìn)步)。希望本文的一些方法能為你帶來(lái)幫助。如有不到之處,請(qǐng)多多包涵。

相關(guān)文章

最新評(píng)論