三元运算符是否倾向于错误/缺陷注入?

发布于 2024-08-08 19:09:40 字数 669 浏览 4 评论 0原文

我的办公室里出现了关于三元运算符使用的讨论。这个讨论有两个方面。

第 1 方面)三元运算符易于编写和读取,因此便利性可以节省净成本。

第 2 面)三元运算符很难维护,因为如果需要将它们修改为稍微复杂一点,则需要进行过多的代码改动。

如果你能引用顶级机构在这个主题上所做的任何实际研究,那就额外加分了……我非常有兴趣看到这方面的硬数据。


我的理论是,最好的代码是可以轻松更改和适应的代码,并且更改所需的复杂性越低,中断的机会就越小。示例:

$id = $user->isRegistered() ? $user->id : null;

好的,这是完全有效的,但是当代码需要更改为稍微复杂一些时会发生什么?

$id = null;
if ($user->isRegistered() || $user->hasEmail()) {
    $id = $user->id;
}

任何理智的程序员都会查看三元并转换为标准 if/else。然而,这需要 4 行更改,而从以下内容开始:

if ($user->isRegistered()) {
    $id = $user->id;
}

只需要 1 行更改。

A discussion has come up in my office about the use of ternary operators. There are two sides to this discussion.

Side 1) That ternary operators are easy to write and read, therefore convenience is a net cost-savings.

Side 2) That ternary operators are difficult to maintain because they require excess code-churn should they ever need to be modified to be even the slightest bit more complex.

Extra nerd points if you can cite any actual studies done by a top-tier institution on this subject... I'm very interested to see hard data on this.


My theory is that the best code is code that can change and adapt easily, and that the less complexity that change requires, the less chance there is for a break. Example:

$id = $user->isRegistered() ? $user->id : null;

Ok, so this is completely valid, but what happens when the code needs to change to become something slightly more complex?

$id = null;
if ($user->isRegistered() || $user->hasEmail()) {
    $id = $user->id;
}

Any sane programmer would look at the ternary and convert to standard if/else. However, this required a 4-line change versus starting with:

if ($user->isRegistered()) {
    $id = $user->id;
}

Which would only require a 1-line change.

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

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

发布评论

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

评论(4

好菇凉咱不稀罕他 2024-08-15 19:09:40

我发现一旦你习惯了语法,这两个选项都同样具有可读性(前提是你没有过度使用任何一个),所以这对我来说不是问题。

至于哪个更省时,我将用一个令人讨厌的问题来回答——你为什么关心?任一选项都需要不到 20 秒的时间才能转换为等效的 if 语句。

就我个人而言,我发现当我想办法减少编程时间时,这是一种拖延。当我专注于完成工作并让可读性小的细节通过经验自行解决时,我的工作效果最好。

I find that once you get used to the syntax both options are equally as readable (provided you aren't going overboard with either) and so that's not an issue for me.

As for which is more time-efficient, I'm going to answer with an obnoxious question - why do you care? Either option takes less than 20 seconds to convert to an equivalent if statement.

Personally I find that when I'm finding ways to chisel seconds off my programming time it's a form of procrastination. I work best when I'm concentrating on getting things done and let the small readability details work themselves out through experience.

亚希 2024-08-15 19:09:40

在你的例子中我会坚持使用 ternery 运算符。

   $id = ($user->isRegistered() ||  $user->hasEmail())? $user->id : null;

只要唯一的“分支”是决定为变量分配什么值,三元运算符就比 if-else 子句更具可读性,因为它不会重复正在执行的实际函数(在这种情况下 -这是代码“$id =”)
如果逻辑条件变得复杂,请将其简化。

   bool $isReg    = $user->isRegistered(),
        $hasEmail = $user->hasEmail();
   $id = ($isReg || $hasEmail)?  $user->id : null;

I'd stick with the ternery operator in your example.

   $id = ($user->isRegistered() ||  $user->hasEmail())? $user->id : null;

As long as the only "branch" is to decide what value is to be assigned to a variable, the ternery operator is more readable than if-else clauses, because it does not potentially duplicate the actual function being performed, (in this case - this is the code "$id =")
If the logical conditions become complex, simplify them.

   bool $isReg    = $user->isRegistered(),
        $hasEmail = $user->hasEmail();
   $id = ($isReg || $hasEmail)?  $user->id : null;
骷髅 2024-08-15 19:09:40

每当我有一个单行表达式(其中值取决于适当的布尔条件)时,我喜欢使用三元运算符。实际上,如果我需要在分配给变量之间进行选择,并且可以在表达式 1 和表达式 2 之间进行选择,那么我经常使用三元。

但是,如果表达式有副作用,那么我会立即将整个内容重写为 if() 语句。使用三元运算符进行流量控制对大多数人来说相当令人困惑。

I like to use the ternary operator whenever I have a one-line expression where the value depends on an appropriate boolean condition. In effect, if I need to choose between assigning to a variable, and I can choose between Expression 1 and Expression 2, then I often use a ternary.

However, if the expressions have a side effect, then I will immediately rewrite the whole thing as an if() statement. Using the ternary operator for flow control is rather confusing to most people.

拒绝两难 2024-08-15 19:09:40

三元运算有其一席之地;它通常是在执行琐碎任务时简化代码。然而,在某些时候,如果条件语句变得过于复杂,您可能需要重新考虑您的策略。确定该点何时成为挑战。只有经验才能指导你。

因此,我认为只要最终结果是干净、可读且可维护的代码,就没有一个“正确”的答案。在做出此决定时,甚至不应该考虑 if 语句的额外代码行(因为代码行数不一定与代码的复杂性一一对应) .)

Ternary operations have their place; it's usually to simplify the code when performing a trivial task. At some point, however, you may need to rethink your strategy if the conditional statement becomes too complex. It's determining when that point is that becomes the challenge. Only experience can guide you then.

So, I don't think there's one "right" answer as long as the end result is clean, readable, and maintainable code. The extra lines of code for the if statement shouldn't even be considered when making this determination (since the number of lines of code doesn't necessarily correlate 1-to-1 to the complexity of the code.)

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