如何组织此代码?
|
myThread是每秒执行一次的函数,基本上,它读取一些必须解析和执行的数据。该功能增长了很多,超过了1500行代码,如下面的示例,其中有很多[if else if]阻止了很多重复,例如sleep或SendToChat来将命令发送到控制台,并且它很难维护,对其进行更改等。
我想就如何重写它提供一些建议(如果可能的话,可以通过代码示例来帮助我理解布局),我不是很有经验,所以我不确定将这种代码转换为更好的代码的可能性。可维护性和可读性?
也可以随意评论任何功能或其他任何功能,因为它可以帮助我改善其他错误的地方。
如果您觉得自己需要代码中的任何其他信息,这只是代码的一部分,而不是整个代码,我会尽力而为。
PS:这不是一个不道德的事情。
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);
}
}
没有找到相关结果
已邀请:
5 个回复
倾坞髓
每个命令都需要这些类之一。
拈吉勉犬姆
蓟类
... 我用:
降低代码的复杂性是将代码分开的第一步。我要采取的下一步措施是: 重构为不同的方法 寻找相似的方法,重构并使用辅助方法 将不依赖内部数据的方法移动到其他类 当您似乎有两种处理完全不同的“事物”的不同方法时,请将它们分开设置类(这是一项很大的工作,特别是对于冗长的代码) 当您将各个部分分离但采用过程性方式时,您可能会开始应用一些模式,这些模式会使您的代码重复性降低。杰森·唐(Jason Down)提到了状态,策略,但是工厂,模板方法,其中大多数也可以,这取决于您的需求。
玖料萄
链眷克袒姜