如果在C#代码中其他刺激,重构大大

发布于 2025-01-26 03:08:20 字数 933 浏览 2 评论 0原文

我有四个非常长的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

寻找一个思念的角度 2025-02-02 03:08:20

这样的东西?

var distance = Vector3.Distance(joint1, joint2);
var divisor = distance > 0.5 ? 10000 : 800;

var scx2 = scx / divisor;
var scy2 = scy / divisor;

if (objectcameraangle < 35)
{
    Rfootikcontroller.transform.Translate(-scx2, scy2, 0);
}
else if (objectcameraangle < 140)
{
    if (signed > 0)
        Rfootikcontroller.transform.Translate(0, scy2, -scx2);
}
else
{
    Rfootikcontroller.transform.Translate(scx2, scy2, 0);
}

Something like this?

var distance = Vector3.Distance(joint1, joint2);
var divisor = distance > 0.5 ? 10000 : 800;

var scx2 = scx / divisor;
var scy2 = scy / divisor;

if (objectcameraangle < 35)
{
    Rfootikcontroller.transform.Translate(-scx2, scy2, 0);
}
else if (objectcameraangle < 140)
{
    if (signed > 0)
        Rfootikcontroller.transform.Translate(0, scy2, -scx2);
}
else
{
    Rfootikcontroller.transform.Translate(scx2, scy2, 0);
}
遗弃M 2025-02-02 03:08:20

如果您使用的是C#(我认为8.0+)的最新版本,您可以尝试这样的事情:

var denominator = (Vector3.Distance(joint1, joint2) > 0.5 ? 10000 : 800);

switch (true) {
    case var _ when objectcameraangle > 140:
        Rfootikcontroller.transform.Translate((scx / denominator), scy / denominator, 0);
        break;
    case var _ when objectcameraangle < 35:
        Rfootikcontroller.transform.Translate((-scx / denominator), scy / denominator, 0);
        break;
    case var _ when objectcameraangle > 35 && objectcameraangle < 140 && signed > 0:
        Rfootikcontroller.transform.Translate(0, scy / denominator, (-scx / denominator));
        break;
}

希望它编译...我没有所有可用的对象,因此我无法检查我。

If you're using a recent version of C# (8.0+ I think?), you can try something like this:

var denominator = (Vector3.Distance(joint1, joint2) > 0.5 ? 10000 : 800);

switch (true) {
    case var _ when objectcameraangle > 140:
        Rfootikcontroller.transform.Translate((scx / denominator), scy / denominator, 0);
        break;
    case var _ when objectcameraangle < 35:
        Rfootikcontroller.transform.Translate((-scx / denominator), scy / denominator, 0);
        break;
    case var _ when objectcameraangle > 35 && objectcameraangle < 140 && signed > 0:
        Rfootikcontroller.transform.Translate(0, scy / denominator, (-scx / denominator));
        break;
}

Hopefully that compiles... I don't have all of your objects available, obviously, so I couldn't check myself.

错爱 2025-02-02 03:08:20

查看您的代码,我看到了两件事:

  • 几乎所有代码都是出于所有意图和目的,只需分配4个不同的变量,

  • 有秋季/no-op条件:scx恰好是35 或140,或签名是非阳性的。


我会隔离每个变量的分配,并在最后调用translate()方法,因此:

int angle = objectcameraangle // because, too many letters (and runtogetherwords)

int d = Vector3Distance > 0.5
      ? 10000
      :   800
      ;
int? x = angle > 140                              ?   scx / d
       : angle <  35                              ? - scx / d
       : angle >  35 && angle < 140 && signed > 0 ? 0
       : null // TODO what happens when objectcameraangle is exactly 35 or 140, or is signed <= 0?
       ;
int? y = angle > 140                              ?   scy / d
       : angle <  35                              ?   scy / d
       : angle >  35 && angle < 140 && signed > 0 ?   scy / d
       : null // TODO what happens when objectcameraangle is exactly 35 or 140, or is signed <= 0?
       ;
int? z = angle > 140 ? 0
       : angle <  35 ? 0
       : angle >  35 && angle < 140 && signed > 0 ? - scx / d
       : null // TODO what happens when objectcameraangle is exactly 35 or 140, or is signed <= 0?
       ;

// Wrapped in a guard clause to protect against the fall-through condition(s) noted above
if ( x != null && y != null && z != null ) {
  Rfootikcontroller.transform.Translate( x, y, z );
}

然后,您可以将4个变量分配中的每个分配中的每一个提取到单个方法中容易测试。那会给你这样的东西:

static int SelectDistance( double v3Distance )
{
  return Vector3Distance > 0.5
       ? 10000
       :   800
       ;
}

static int SelectValue( angle int , signed int, v1 int, v2 int , v3 int , v4 int)
{
  return angle > 140                             ? v1
       : angle <  35                             ? v2
       : angle >  35 && angle < 140 && signe > 0 ? v3
       :                                           v4
       ;
}
.
.
.
int  angle = objectcameraangle // because, too many letters (and runtogetherwords)

int  d = SelectDistance( Vector3Distance );
int? x = SelectValue( angle, signed, scx/d, -scx/d,      0, null );
int? y = SelectValue( angle, signed, scy/d,  scy/d,  scy/d, null );
int? z = SelectValue( angle, signed,     0,      0, -scx/d, null );

// Wrapped in a guard clause to protect against the fall-through condition(s) noted above
if ( x != null && y != null && z != null ) {
  Rfootikcontroller.transform.Translate( x, y, z );
}

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 exactly 35 or 140, or signed is non-positive.

I would isolate the assignment of each variable and invoke the Translate() method at the end, thus:

int angle = objectcameraangle // because, too many letters (and runtogetherwords)

int d = Vector3Distance > 0.5
      ? 10000
      :   800
      ;
int? x = angle > 140                              ?   scx / d
       : angle <  35                              ? - scx / d
       : angle >  35 && angle < 140 && signed > 0 ? 0
       : null // TODO what happens when objectcameraangle is exactly 35 or 140, or is signed <= 0?
       ;
int? y = angle > 140                              ?   scy / d
       : angle <  35                              ?   scy / d
       : angle >  35 && angle < 140 && signed > 0 ?   scy / d
       : null // TODO what happens when objectcameraangle is exactly 35 or 140, or is signed <= 0?
       ;
int? z = angle > 140 ? 0
       : angle <  35 ? 0
       : angle >  35 && angle < 140 && signed > 0 ? - scx / d
       : null // TODO what happens when objectcameraangle is exactly 35 or 140, or is signed <= 0?
       ;

// Wrapped in a guard clause to protect against the fall-through condition(s) noted above
if ( x != null && y != null && z != null ) {
  Rfootikcontroller.transform.Translate( x, y, z );
}

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:

static int SelectDistance( double v3Distance )
{
  return Vector3Distance > 0.5
       ? 10000
       :   800
       ;
}

static int SelectValue( angle int , signed int, v1 int, v2 int , v3 int , v4 int)
{
  return angle > 140                             ? v1
       : angle <  35                             ? v2
       : angle >  35 && angle < 140 && signe > 0 ? v3
       :                                           v4
       ;
}
.
.
.
int  angle = objectcameraangle // because, too many letters (and runtogetherwords)

int  d = SelectDistance( Vector3Distance );
int? x = SelectValue( angle, signed, scx/d, -scx/d,      0, null );
int? y = SelectValue( angle, signed, scy/d,  scy/d,  scy/d, null );
int? z = SelectValue( angle, signed,     0,      0, -scx/d, null );

// Wrapped in a guard clause to protect against the fall-through condition(s) noted above
if ( x != null && y != null && z != null ) {
  Rfootikcontroller.transform.Translate( x, y, z );
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文