比较 if-else、switch-case 和 Contains() 的性能、可读性和可重用性
我有下面的代码(这是一个示例,还有许多其他条件 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-case
或 Contains()
或 switch-case
和 Contains()
重构此代码代码>.
对于 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
第一步,停止每次执行
Session["Yapilanislem_Popup"].ToString()
,获取一次:然后使用 case 语句:
请注意,完全匹配会获得自己的
case
,任何其他逻辑/测试都发生在default
情况下。编辑:“看起来你正在编写一个状态机”(ala Clippy)。请参阅
nextState
变量的使用以使其更加明显。Massive first step, stop doing
Session["Yapilanislem_Popup"].ToString()
each time, get it once:Then use a case statement:
Note that exact matches get their own
case
, any other logic/testing happens in thedefault
case.Edit: "It looks like you're writing a state machine" (ala clippy). See use of
nextState
variable to make it more obvious.我不禁注意到,使用
Convert.ToString(session["value"])
而不是Session["Value"].ToString()
总是更好因为它也会处理您的空对象。I couldn't help noticing, it is always better to use
Convert.ToString(session["value"])
instead ofSession["Value"].ToString()
as it will handle your null objects as well.