gpt4 book ai didi

c# - 我该如何组织这段代码?

转载 作者:太空宇宙 更新时间:2023-11-03 17:27:34 24 4
gpt4 key购买 nike

myThread 是一个每秒执行一次的函数,基本上它读取一些必须解析和执行的数据。该函数增长了很多,它超过了 1500 行代码,就像下面的示例一样,有很多 [if else if 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);
}
}

最佳答案

通常当您看到复杂的条件逻辑(很多 if/else block 和嵌套条件)时,您应该考虑将代码重构到 State 中。或 Strategy设计模式(视情况而定)。查看这些内容以获得一些想法。

更新:
如果您在如何重构代码方面遇到困难,我最近读了一本很棒的书,它有助于分解流程(涵盖从添加版本控制到单元测试到持续集成的所有内容......还谈到了破坏事物迭代向下直到你能够应用依赖注入(inject)等)。它没有全面深入地涵盖任何主题,因为每个主题都有专门的整本书,但这是一个很好的起点:
Brownfield Application Development in .Net

关于c# - 我该如何组织这段代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6180954/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com