当同一个 Perl 语句中有一个条件运算符和一个后缀条件时会发生什么?
有人能解释一下这条线是如何工作的吗?
return $y < 0 ? - pip2 : pip2 if $x == 0;
如果 $y <0
它返回 -pip2
,但是当 $y >= 0
和 $x 时它返回什么! = 0 ?
这一行来自这个函数:
sub _atan {
my( $y, $x ) = @_;
return $y < 0 ? - pip2 : pip2 if $x == 0;
return atan( $y / $x );
}
Can anybody explain how this line works?
return $y < 0 ? - pip2 : pip2 if $x == 0;
if $y <0
it returns -pip2
, but what it returns when $y >= 0
and $x != 0
?
This line is from this function:
sub _atan {
my( $y, $x ) = @_;
return $y < 0 ? - pip2 : pip2 if $x == 0;
return atan( $y / $x );
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
后缀“if”表示仅在条件为真时才执行 return 语句,因此
与“如果您对 ?: 三元运算符感到困惑”相同
,也可以将其重写为常规 if 语句,以产生此结果
The postfix "if" means the return statement is only executed if the condition is true, so
is the same as
If you're puzzled by the ?: ternary operator, that could be rewritten as a regular if statement too, to yield this
相同然后变为:
它与整个函数
its the same as
The entire function then becomes:
这是难以阅读的代码的一个很好的例子。
让我们比较一下重写代码示例的几种不同方法,看看我们在保持简洁性和提高可读性方面做得如何。
这个仅三元的版本在简洁性方面胜出,但仍然难以阅读:
我发现链式条件运算符 (?:) 仅当后续运算符落在 else 位置时才可读:
仍然简短,但可读性有所提高。
但是使用
if
和unless
又如何呢?我们是否也可以使用它们来编写简洁、可读的代码?从本质上来说,直接的 if/else 方法会更加冗长:
很容易跟踪上面的内容并查看结果是什么。因此可读性获胜,但简洁性受到损害。
我发现使用
unless
和if
的语句修饰符形式提供了一种向代码块添加短路逻辑的干净方法:这是简洁且可读的,但是在我看来,我们获得的回报似乎超出了我们的需要。
因此,如果我们在混合中引入条件运算符,我们会得到
这种形式与上述任何一种形式一样简洁,但更容易理解:
嵌套的 if/else 子句可能很难理解。在构造决策代码时稍微小心一点可以大大提高可读性,从而提高可维护性,同时保留底层逻辑的简洁表达。
这里要修复的代码味道是条件运算符 (
?:
) 与语句修饰符形式if
的巴洛克组合。通过重新安排测试的顺序并仔细选择如何表示条件逻辑,我们能够保持简洁并澄清代码。This is a good example of hard to read code.
Let's compare a few different ways to rewrite the code sample and see how we do in retaining brevity and improving readability.
This ternary only version wins for brevity, but it is still hard to read:
I find that chained conditional operators (?:) are only readable when subsequent operators fall in the else position:
Still brief, but readability is improved.
But what about using
if
andunless
? Can we have concise, readable code using them, too?By its nature a straight if/else approach will be more verbose:
It is easy to trace through the above and see what the result will be. So readability wins, but brevity suffers.
I find that using the statement modifier forms of
unless
andif
provide a clean way to add short-circuit logic to a chunk of code:This is consise and readable, but to me it seems like we've got more returns than we need.
So if we introduce a conditional operator to the mix, we get
This form is as concise as any of the above forms, but much easier to understand:
Nested if/else clauses can be difficult to understand. Taking a little care when structuring your decision code can greatly improve readability and therefore maintainability, while retaining concise expression of the underlying logic.
The code smell to be fixed here was the baroque combination of the conditional operator (
?:
) with the statement modifier form ofif
. By rearranging the order of the tests and carefully choosing how we represent conditional logic, we were able to preserve brevity and clarify the code.用于解决问题的行太多会使代码难以维护(总是必须滚动)。嵌套 if 的解决方案要长 4 倍。想象一下使用小 4 倍的屏幕进行工作。我最喜欢的语法是:
如果将后缀运算符放在下一行,那么使用后缀运算符的好处就会减少。
此行顺序(由 @daotoad 建议)允许将后缀条件放在更简单的行上。
初始语法也很好,但我不想处理包含以前帖子中建议的嵌套 if 的代码。
Too many lines used to solve a problem makes the code difficult to maintain (always obliged to scroll). The solution with nested if is 4 times longer. Imagine working with a screen 4 times smaller. My favorite syntax is:
The benefit of using a postfix operator is reduced if you put them on the next line.
This line order (suggested by @daotoad) allows to put the postfix condition on a simpler line.
The initial syntax is also nice, but I would not like to work on code containing the suggested nested if of previous posts.