How does code refactoring work?

Source: Internet
Author: User

These days, take over a colleague's code, about the development of micro-letter interface, that a heap of if, see Brother egg pain, this problem is also a lot of beginners easy to make, so specially to write this refactoring.

Let's take a look at the problem with this code, if the code block logic in the if else inside is hard to change, making it reusable 0 and difficult to read. Of course, if there are only one or two, or 3, this is not a very good question to write.

But if more, this kind of code will make maintenance become difficult.

if (Strmsgtype = = "Text")

{

Textcontentclient = Rootelement.selectsinglenode ("Content"). InnerText;

strresult = Setmsgtype_text (Strclientname, textcontentclient, DB, strServerName, Identity);

System.Diagnostics.Trace.WriteLine (strresult);

Return Content (strresult);

}

else if (Strmsgtype = "event")

{

String eventtype = Rootelement.selectsinglenode ("Event"). Innertext.tolower ();

if (EventType = = "Subscribe")

{

String keycode = "";

if (Rootelement.selectsinglenode ("Eventkey")!= null)

KeyCode = Rootelement.selectsinglenode ("Eventkey"). Innertext.tolower ();

strresult = Formateventsubscribe (keycode);

Recordreplymessage ();

Return Content (strresult);

}

else if (EventType = "Scan")

{

String keycode = Rootelement.selectsinglenode ("Eventkey"). Innertext.tolower ();

var outletname = "Welcome attention";

var outletdb = shoppingcontext.createinstance (Identity);

var Outletmodel = OutletDB.Outlets.FirstOrDefault (o => O.sceneid = = Int32.Parse (keycode));

if (Outletmodel!= null)

Outletname + = Outletmodel.name;

Return Content (Gettexttemp (Strclientname, strServerName, Outletname));

}

else if (EventType = = "click")

{

String keycode = Rootelement.selectsinglenode ("Eventkey"). Innertext.tolower ();

strresult = Fomatmenumessage (keycode);

Return Content (strresult);

}

else if (EventType = "unsubscribe")

{

var subids = db. Replyrecords.where (r => R.fromopenid = = this. Clientid.tostring () && R.emesstype = = EEventType.Subscribe.ToString () && r.keyword!= null). Select (o => o.keyword). ToArray ();

var unsubids = db. Replyrecords.where (r => R.fromopenid = = this. Clientid.tostring () && R.emesstype = = EEventType.Unsubscribe.ToString () && r.keyword!= null). Select (o => o.keyword). ToArray ();

var sencesid = "";

foreach (var k in subids)

{

if (!unsubids.contains (k))

{

This. Replymodel.keyword = k;

Break

}

}

This. Replymodel.emesstype = EEventType.Unsubscribe.ToString ();

Recordreplymessage ();

}

}

else if (strmsgtype.tolower () = = "Location")

{

String strlocation_x = Rootelement.selectsinglenode ("location_x"). InnerText;

String strlocation_y = Rootelement.selectsinglenode ("location_y"). InnerText;

strresult = formatoutletlbs (double. Parse (strlocation_x), double. Parse (strlocation_y), 10);

strresult = formattextmessage (strlocation_x + "|" + strlocation_y);

Return Content (strresult);

}

else if (strmsgtype.tolower () = = "image")

{

String strimgurl = Rootelement.selectsinglenode ("Picurl"). InnerText;

}

A better way to do this is to extract the code from the block of statements and write it as a function, as shown here:

public class Messageprocesser

{

Public replymessage Process (string xml)

{

var msg = postmessage.fromxml (XML);

Switch (MSG. Msgtype)

{

Case Models.PostMessageType.Event:

var EventType = ((eventmessage) msg). Event;

Switch (EventType)

{

Case Eventtype.click:

Return Processclickevent ((clickevent) msg);

Case Eventtype.location:

Return Processlocationevent ((locationevent) msg);

Case Eventtype.scan:

Return Processscanevent ((scanevent) msg);

Case Eventtype.subscribe:

Return Processsubscribeevent ((subscribeevent) msg);

Case Eventtype.unsubscribe:

Return Processunsubscribeevent ((unsubscribeevent) msg);

}

Break

Case Models.PostMessageType.Image:

Return Processimagemessage ((imagemessage) msg);

Case Models.PostMessageType.Link:

Return Processlinkmessage ((linkmessage) msg);

Case Models.PostMessageType.Location:

Return Processlocationmessage ((locationmessage) msg);

Case Models.PostMessageType.Text:

Return Processtextmessage ((textmessage) msg);

Case Models.PostMessageType.Video:

Return Processvideomessage ((videomessage) msg);

Case Models.PostMessageType.Voice:

Return Processvoicemessage ((voicemessage) msg);

}

return null;

}

Protected virtual Replymessage processclickevent (clickevent msg)

{

return defaultprocess (msg);

}

Protected virtual Replymessage processlocationevent (locationevent msg)

{

return defaultprocess (msg);

}

Protected virtual Replymessage processscanevent (scanevent msg)

{

return defaultprocess (msg);

}

Protected virtual Replymessage processsubscribeevent (subscribeevent msg)

{

return defaultprocess (msg);

}

Protected virtual Replymessage processunsubscribeevent (unsubscribeevent msg)

{

return defaultprocess (msg);

}

Protected virtual Replymessage processimagemessage (imagemessage msg)

{

return defaultprocess (msg);

}

Protected virtual Replymessage processlinkmessage (linkmessage msg)

{

return defaultprocess (msg);

}

Protected virtual Replymessage processlocationmessage (locationmessage msg)

{

return defaultprocess (msg);

}

Protected virtual Replymessage processtextmessage (textmessage msg)

{

return defaultprocess (msg);

}

Protected virtual Replymessage processvideomessage (videomessage msg)

{

return defaultprocess (msg);

}

Protected virtual Replymessage processvoicemessage (voicemessage msg)

{

return defaultprocess (msg);

}

Protected virtual Replymessage defaultprocess (PostMessage msg)

{

var reply = new textreply (msg);

if (Msg. Msgtype = = postmessagetype.event)

{

Reply. Content = string. Format (' {0} ' not processed. ', ((eventmessage) msg). Event);

}

Else

{

Reply. Content = string. Format (' {0} ' is ' not processed. ', MSG. Msgtype);

}

return reply;

}

}

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.