如果在C#代码中其他刺激,重构大大
我有四个非常长的IF-Else代码。如何重构此代码?
if (objectcameraangle > 140)
{
if (Vector3.Distance(joint1, joint2) > 0.5)
{
Rfootikcontroller.transform.Translate((scx / 10000), scy / 10000, 0);
}
else
{
Rfootikcontroller.transform.Translate((scx / 800), scy / 800, 0);
}
}
else if (objectcameraangle < 35)
{
if (Vector3.Distance(joint1, joint2) > 0.5)
{
Rfootikcontroller.transform.Translate((-scx / 10000), scy / 10000, 0);
}
else
{
Rfootikcontroller.transform.Translate((-scx / 800), scy / 800, 0);
}
}
else if (objectcameraangle > 35 && objectcameraangle < 140 && signed > 0)
{
if (Vector3.Distance(joint1, joint2) > 0.5)
{
Rfootikcontroller.transform.Translate(0, scy / 10000, (-scx / 10000));
}
else
{
Rfootikcontroller.transform.Translate(0, scy / 800, (-scx / 800));
}
}
我如何将其改写为更加好的东西。试图在这里学习一些新的东西。感谢您的帮助。
I have four of this really long if-else code. How can I refactor this code?
if (objectcameraangle > 140)
{
if (Vector3.Distance(joint1, joint2) > 0.5)
{
Rfootikcontroller.transform.Translate((scx / 10000), scy / 10000, 0);
}
else
{
Rfootikcontroller.transform.Translate((scx / 800), scy / 800, 0);
}
}
else if (objectcameraangle < 35)
{
if (Vector3.Distance(joint1, joint2) > 0.5)
{
Rfootikcontroller.transform.Translate((-scx / 10000), scy / 10000, 0);
}
else
{
Rfootikcontroller.transform.Translate((-scx / 800), scy / 800, 0);
}
}
else if (objectcameraangle > 35 && objectcameraangle < 140 && signed > 0)
{
if (Vector3.Distance(joint1, joint2) > 0.5)
{
Rfootikcontroller.transform.Translate(0, scy / 10000, (-scx / 10000));
}
else
{
Rfootikcontroller.transform.Translate(0, scy / 800, (-scx / 800));
}
}
How I can rewrite it to something more nice . Trying to learn here something new . Thanks for help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这样的东西?
Something like this?
如果您使用的是C#(我认为8.0+)的最新版本,您可以尝试这样的事情:
希望它编译...我没有所有可用的对象,因此我无法检查我。
If you're using a recent version of C# (8.0+ I think?), you can try something like this:
Hopefully that compiles... I don't have all of your objects available, obviously, so I couldn't check myself.
查看您的代码,我看到了两件事:
几乎所有代码都是出于所有意图和目的,只需分配4个不同的变量,
有秋季/no-op条件:
scx
恰好是35 或
140
,或签名
是非阳性的。我会隔离每个变量的分配,并在最后调用
translate()
方法,因此:然后,您可以将4个变量分配中的每个分配中的每一个提取到单个方法中容易测试。那会给你这样的东西:
Looking at your code I see two things:
Almost all of that code is for all intents and purposes, simply assigning 4 different variables, and
There are fall-through/no-op conditions: When
scx
is exactly35
or140
, orsigned
is non-positive.I would isolate the assignment of each variable and invoke the
Translate()
method at the end, thus:You could then extract the logic for each of the 4 variable assignments out to individual methods, making them simple and easily testable. That would give you something like this: