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?
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 thedefaultcase.Edit: “It looks like you’re writing a state machine” (ala clippy). See use of
nextStatevariable to make it more obvious.