比较 if-else、switch-case 和 Contains() 的性能、可读性和可重用性

发布于 2024-11-24 16:51:29 字数 1654 浏览 0 评论 0原文

我有下面的代码(这是一个示例,还有许多其他条件 Session["Yapilanislem_Popup"].ToString() 是不同的。)。

       if (Session["Yapilanislem_Popup"].ToString() == "updatemustericeki")
        {
            KayitGuncelleme();
        }

        else if (Session["Yapilanislem_Popup"].ToString() == "updatemusterisenedi")
        {
            KayitGuncelleme();
        }           

            ///

        else if (Session["Yapilanislem_Popup"].ToString() == "yenitahsilat")
        {
            YeniKayit();
            Session["Yapilanislem_Popup"] = "updatetahsilat";
            BaslikLabel.Text = "Tahsilat Güncelle";
        }
        else if (Session["Yapilanislem_Popup"].ToString() == "yeniodeme")
        {
            YeniKayit();
            Session["Yapilanislem_Popup"] = "updateodeme";
            BaslikLabel.Text = "Ödeme Güncelle";
        }

我想使用 switch-caseContains()switch-caseContains() 重构此代码代码>.

对于 Contains() 我想我可以这样做:

if (Session["Yapilanislem_Popup"].ToString().Contains("update"))
            {
                KayitGuncelleme();
            }
else if(Session["Yapilanislem_Popup"].ToString().Contains("yeni")){
YeniKayit();
                Session["Yapilanislem_Popup"] = "updateodeme";
                BaslikLabel.Text = "Ödeme Güncelle";

}

对于 switch-case 我基本上可以为每种情况编写它。

Switch-case 对于 Session["Yapilanislem_Popup"].ToString() 的更多条件来说将是多行代码,但是如果我使用 Contains() > 代码行数将会减少。

我还担心性能问题。

就性能、可读性和可重用性而言,哪一个更好?

I have this below code(this is a sample, there are many more other conditions which Session["Yapilanislem_Popup"].ToString() is different.).

       if (Session["Yapilanislem_Popup"].ToString() == "updatemustericeki")
        {
            KayitGuncelleme();
        }

        else if (Session["Yapilanislem_Popup"].ToString() == "updatemusterisenedi")
        {
            KayitGuncelleme();
        }           

            ///

        else if (Session["Yapilanislem_Popup"].ToString() == "yenitahsilat")
        {
            YeniKayit();
            Session["Yapilanislem_Popup"] = "updatetahsilat";
            BaslikLabel.Text = "Tahsilat Güncelle";
        }
        else if (Session["Yapilanislem_Popup"].ToString() == "yeniodeme")
        {
            YeniKayit();
            Session["Yapilanislem_Popup"] = "updateodeme";
            BaslikLabel.Text = "Ödeme Güncelle";
        }

I want to refactor this code by using a switch-case or Contains() or switch-case and Contains().

For Contains() I think I can do this:

if (Session["Yapilanislem_Popup"].ToString().Contains("update"))
            {
                KayitGuncelleme();
            }
else if(Session["Yapilanislem_Popup"].ToString().Contains("yeni")){
YeniKayit();
                Session["Yapilanislem_Popup"] = "updateodeme";
                BaslikLabel.Text = "Ödeme Güncelle";

}

For switch-case I can basically write it for each case.

Switch-case would be many lines of codes for more conditions of Session["Yapilanislem_Popup"].ToString() however if I use Contains() there will be lesser number of lines of codes.

I am also concerned about performance issue.

Which one would be better to use regarding performance, readibility and reusability?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无远思近则忧 2024-12-01 16:51:29

第一步,停止每次执行 Session["Yapilanislem_Popup"].ToString(),获取一次:

string yapPopup = Session["Yapilanislem_Popup"].ToString();

然后使用 case 语句:

string nextState = yapPopup; // default to no state change

switch (yapPopup)
{
case "yenitahsilat":
    YeniKayit();
    nextState = "updatetahsilat";
    BaslikLabel.Text = "Tahsilat Güncelle";
    break;

case "yeniodeme":
    YeniKayit();
    nextState = "updateodeme";
    BaslikLabel.Text = "Ödeme Güncelle";
    break;

default:
    if (yapPopup.Contains("update"))
    {
        KayitGuncelleme();
    }
    break;
}

Session["Yapilanislem_Popup"] = nextState;

请注意,完全匹配会获得自己的 case,任何其他逻辑/测试都发生在 default 情况下。

编辑:“看起来你正在编写一个状态机”(ala Clippy)。请参阅 nextState 变量的使用以使其更加明显。

Massive first step, stop doing Session["Yapilanislem_Popup"].ToString() each time, get it once:

string yapPopup = Session["Yapilanislem_Popup"].ToString();

Then use a case statement:

string nextState = yapPopup; // default to no state change

switch (yapPopup)
{
case "yenitahsilat":
    YeniKayit();
    nextState = "updatetahsilat";
    BaslikLabel.Text = "Tahsilat Güncelle";
    break;

case "yeniodeme":
    YeniKayit();
    nextState = "updateodeme";
    BaslikLabel.Text = "Ödeme Güncelle";
    break;

default:
    if (yapPopup.Contains("update"))
    {
        KayitGuncelleme();
    }
    break;
}

Session["Yapilanislem_Popup"] = nextState;

Note that exact matches get their own case, any other logic/testing happens in the default case.

Edit: "It looks like you're writing a state machine" (ala clippy). See use of nextState variable to make it more obvious.

温暖的光 2024-12-01 16:51:29

我不禁注意到,使用 Convert.ToString(session["value"]) 而不是 Session["Value"].ToString() 总是更好因为它也会处理您的空对象。

I couldn't help noticing, it is always better to use Convert.ToString(session["value"]) instead of Session["Value"].ToString() as it will handle your null objects as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文