重构这个块

发布于 2024-10-09 20:19:34 字数 344 浏览 3 评论 0原文

我正在重构一些不是我写的代码。该块设置 $val 的值,但我想稍微清理一下它。显然我不能在这里使用三级运算符。还有什么其他方法可以使代码更简洁?

  if (isset($vars[$input])) {
       $val = $vars[$input];
  } elseif (isset($this->getI['io'])) {
       $val = $this->getI['io'];
  } elseif (isset($vars[5])) {
       $val = $vars[5];
  } else {
       $val = 10;
  }

I'm refactoring some code that wasn't written by me. This block sets the value of $val but I want to clean it up a bit. Obviously I can't use the tertiary operator here. What other ways I can make this code cleaner?

  if (isset($vars[$input])) {
       $val = $vars[$input];
  } elseif (isset($this->getI['io'])) {
       $val = $this->getI['io'];
  } elseif (isset($vars[5])) {
       $val = $vars[5];
  } else {
       $val = 10;
  }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

怎会甘心 2024-10-16 20:19:34
$val = 10;
if (isset($vars[$input])) {
    $val = $vars[$input];
} elseif (isset($this->getI['io'])) {
    $val = $this->getI['io'];
} elseif (isset($vars[5])) {
    $val = $vars[5];
}

这在不混淆代码的情况下非常简单。我宁愿尝试简化逻辑,有点难以理解为什么在这么多不同的地方寻找价值。

$val = 10;
if (isset($vars[$input])) {
    $val = $vars[$input];
} elseif (isset($this->getI['io'])) {
    $val = $this->getI['io'];
} elseif (isset($vars[5])) {
    $val = $vars[5];
}

This is about as simple as it gets without obfuscating the code. I'd rather try to simplify the logic, it's kinda hard to comprehend why the value is being looked for in so many different places.

怎樣才叫好 2024-10-16 20:19:34

恐怕我不懂php。我假设如果您要将 $vars[$input] 传递给函数,那么当它成为函数的参数时,参数的设置性将为 true(如果不是这种情况,我尝试编写一个函数来测试 isset() 的参数并设置 $val(如果是)。我发现 elseif 会增加复杂性;我尽量避开他们。在这种情况下,我会编写一个返回值的函数;那么我所有的 elseif 都可以变成简单的 if 。

f() {
    if (isset($vars[$input])) {
        return $vars[$input];
    }
    if (isset($this->getI['io'])) {
        return $this->getI['io'];
    }
    if (isset($vars[5])) {
        return $vars[5];
    }
    return 10;
}

当然,在您的调用函数中,将 $val 分配给该函数的结果。

I'm afraid I don't know php. I'm assuming that if you were to pass (say) $vars[$input] to a function, by the time it was a parameter to the function, the parameter's set-ness would be true (if that's not the case, I'd try writing a function that tested isset() on its parameter and set $val if so). I find elseif's to add complexity; I try to avoid them. In this case, I would write a function that returned the value; then all my elseif's can become plain if's.

f() {
    if (isset($vars[$input])) {
        return $vars[$input];
    }
    if (isset($this->getI['io'])) {
        return $this->getI['io'];
    }
    if (isset($vars[5])) {
        return $vars[5];
    }
    return 10;
}

And, of course, in your calling function, assign $val to the result of this function.

淑女气质 2024-10-16 20:19:34

在我看来,你的例子是很干净的。当然,您可以使用三元运算符将其写为一个巨大的单行:

$val = isset($vars[$input]) ? $vars[$input] : isset($this->getI['io'] ? $this->getI['io'] : isset($vars[5]) ? $vars[5] : 10;

但这显然更难以阅读和维护,因此原始示例绝对更清晰(尽管可能缺少一些注释)。

In my opinion, your example is as clean as it gets. Sure, you could write it as a huge one-liner using the ternary operator:

$val = isset($vars[$input]) ? $vars[$input] : isset($this->getI['io'] ? $this->getI['io'] : isset($vars[5]) ? $vars[5] : 10;

But this is obviously much harder to read and to maintain, so the original example is definitely cleaner (although it might be missing some comments).

小傻瓜 2024-10-16 20:19:34

我不知道……看起来很简洁。

如果你知道它的作用,它做得很好,而且足够干净,你将来可以再次弄清楚它,我说不要碰它。

I don't know...it seems to be pretty concise, as is.

If you know what it does, it does it well and it is clean enough that you can figure it out again in the future, I say don't touch it.

掀纱窥君容 2024-10-16 20:19:34

当你在做的时候,弄清楚它在做什么并添加一些评论。

例如,为什么将它分配给神奇的数字 10?也许其余部分的背景可能会带来一些启发。

就代码而言,您不会得到比这更简单的代码。

While you're at it figure out what it's doing and add some comments.

e.g. why assign it to the magic number 10? maybe the context of the rest of it may shed some light.

As far as code goes, you're not going to get it any simpler than this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文