myThread是一個每秒執行的函數,基本上它讀取一些必須解析和執行的數據。這個函數增長了很多,它像下面的示例一樣有1500行以上的代碼,有很多[if else else]會阻止很多重複,比如sleep或者SendToChat向控制檯發送一個命令,而且它很難維護,對其進行更改等。我如何組織這段代碼?
我想一些建議(如果可能的話,代碼示例,這將幫助我瞭解佈局),我可以如何重寫這個,我不是很有經驗,所以我不確定是否可以將此代碼轉換爲一個更好的可維護性和可讀性代碼?
也隨時評論任何功能或其他任何事情,因爲它可以幫助我改進其他錯誤的東西。
這只是代碼的一些示例,而不是整個代碼,如果您覺得您需要代碼中的任何其他信息,請隨時詢問,我會盡快發佈。
PS:這不是一個irc的東西。
private void myThread()
{
while(isRunning)
{
// SOME PARSED DATA HERE
// if (parsedData matchs) do the below stuff
Messages received = new Messages
{
Sent = Convert.ToDateTime(match.Groups[1].Value),
Username = match.Groups[3].Value,
MessageType = (match.Groups[2].Length > 0) ? MsgType.System : MsgType.Chat,
Message = match.Groups[4].Value.Trim(),
CommandArgs = match.Groups[4].Value.ToLower().Trim().Split(' ')
};
// Get the user that issued the command
User user = usersList.Find(x => x.Name == recebe.received.ToLower());
if (user != null)
{
// different greetings based on acess level
if (received.Message == "has entered this room")
{
if (User == null)
{
SendToChat("/w " + received.Username + " " + received.Username + " you have no registration.");
Thread.Sleep(1000);
SendToChat("/kick " + received.Username + " not registered.");
Thread.Sleep(1000);
}
else
{
string cmd = (user.Access < Access.Vouch) ?
"/ann " + user.Access.ToString() + " <" + received.Username + "> has entered the room." :
"/w " + received.Username + " " + received.Username + " welcome to our room !";
SendToChat(cmd);
Thread.Sleep(1000);
}
}
// Here we filter all messages that start with a . which means it is a command
else if (received.Message.StartsWith(".") && user != null)
{
// here we verify if the user has Access to use the typed command and/or if the command exists
if (accessList.Exists(x => x.Access == user.Access && x.HasCommand(received.CommandArgs[0])))
{
if (received.CommandArgs[0] == ".say")
{
SendToChat("/ann " + received.Username + " says: " + received.Message.Substring(received.CommandArgs[0].Length + 1));
Thread.Sleep(1000);
}
else if (received.CommandArgs[0] == ".command")
{
string allowedList = string.Empty;
int count = 0;
foreach (string cmd in listaAccesss.Find(x => x.Access == user.Access).Command)
{
if (count == 0)
allowedList += cmd;
else
allowedList += ", " + cmd;
}
SendToChat("/w " + received.Username + " " + received.Username + " you are allowed to use the followed commands: " + permite);
Thread.Sleep(1000);
}
else if (received.CommandArgs[0] == ".vip")
{
if (received.Command.Count() < 2)
{
SendToChat("/w " + received.Username + " " + received.Username + ", see an example of how to use this command: .vip jonh");
Thread.Sleep(1000);
}
else if (received.Command.Count() == 2)
{
var target = usersList.Find(x => x.Name == received.CommandArgs[1]);
if (target == null)
{
User newUser = new User
{
Name = received.CommandArgs[1].ToLower(),
Access = Access.VIP,
Registered = DateTime.Now,
RegisteredBy = received.Username.ToLower()
};
usersList.Add(newUser);
SendToChat("/ann " + user.Access.ToString() + " " + user.Name + " has promoted " + received.CommandArgs[1] + " to VIP.");
Thread.Sleep(1000);
}
else if (target != null && target.Access == Access.VIP)
{
SendToChat("/w " + received.Username + " " + received.Username + " the user " + target.Name + " already have VIP access.");
Thread.Sleep(1000);
}
else if (target != null && user.Access == Access.HeadAdmin && user.Access < target.Access)
{
Access last = target.Access;
target.Access = Access.Vouch;
SendToChat("/ann " + user.Access.ToString() + " " + received.Username + " promoted/demoted the " + last.ToString() + " " + target.Name + " to VIP.");
Thread.Sleep(1000);
}
else if (target != null && target.Access == Access.Vouch)
{
target.Access = Access.VIP;
target.RegisteredBy = user.Name;
SendToChat("/ann " + user.Access.ToString() + " " + received.Username + " promoted the vouch of " + target.Name + " to VIP.");
Thread.Sleep(1000);
}
else
{
SendToChat("/w " + received.Username + " " + received.Username + " you can't register or modify the user " + received.CommandArgs[1] + ".");
Thread.Sleep(1000);
}
}
}
}
else
{
SendToChat("/w " + received.Username + " " + received.Username + " command not found.");
Thread.Sleep(1000);
}
}
}
Thread.Sleep(1000);
}
}
您可能想要在http://codereview.stackexchange.com上發佈此內容 – carlosfigueira 2011-05-30 21:40:28
@carlosfigueira我認爲我的問題適合在這裏以及我不僅尋找建議,建議,但我也尋找潛在的編碼一個社區成員可能會感興趣的例子,以幫助我學習,我相信很多用戶已經通過這種體驗一次,但我會保持codereview記在腦海中我不知道它.. – Guapo 2011-05-30 21:47:29
這個問題基本上是關閉的根據常見問題解答。這不是「特定的編程問題」,「每個答案都是同樣有效的」。正如carlosfigueira提到的,還有一個更多的話題stackexchange網站。 – 2011-05-30 21:52:20