只要不损害可读性,简单的三元大小写就可以在程序流程中使用吗?
阅读要三元还是不三元?和这是三元运算符的合理使用吗?,我收集了这么简单三元运算符的使用被普遍接受,因为它们不会损害可读性。我还认为,当您不希望三元块执行某些操作时,让三元块的一侧返回 null 完全是一种浪费。但是,我在重构我的网站时遇到了这种情况,这让我皱起鼻子:
if ($success) {
$database->commit();
} else {
$database->rollback();
}
我重构了这个 我对此
$success ? $database->commit() : $database->rollback();
非常满意..但我内心的某些东西让我来这里寻求意见。抛开异常捕获不谈,您认为这是一个好的用例吗?我是否想知道这是否可以使用,因为我以前从未这样做过,或者因为这确实是不好的做法?这对我来说似乎并不困难,但是对其他人来说这似乎很难理解吗?它是否取决于语言……例如,这在 C、C++ 或 Java 中或多或少是错误的吗?
After reading To ternary or not to ternary? and Is this a reasonable use of the ternary operator?, I gathered that simple uses of the ternary operator are generally accepted, because they do not hurt readability. I also gathered that having one side of the ternary block return null when you don't want it to do something is a complete waste.. However, I ran across this case while refactoring my site that made me wrinkle my nose:
if ($success) {
$database->commit();
} else {
$database->rollback();
}
I refactored this down to
$success ? $database->commit() : $database->rollback();
And I was pretty satisfied with it.. but something inside me made me come here for input. Exception catching aside, would you consider this an okay use case? Am I wondering if this is an okay use because I have never done this before, or because it really is bad practice? This doesn't seem difficult to me, but would this seem difficult to understand for anyone else? Does it depend on the language.. as in, would this be more/less wrong in C, C++, or Java?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不,这不行。您正在将看起来像语句的东西变成看起来像表达式的东西。事实上,如果
commit()
和rollback()
返回void
,这至少不会在 Java 中编译(不确定提到的其他内容) )。如果您想要单行代码,则应该在
$database
对象上创建另一种方法,例如$database->endTransaction($success)
来执行$database->endTransaction($success)
>if 内部语句。No, it is not OK. You are turning something that should look like a statement into something that looks like an expression. In fact, if
commit()
androllback()
returnvoid
, this will not compile in Java at least (not sure about the others mentioned).If you want a one-liner, you should rather create another method on the
$database
object such as$database->endTransaction($success)
that does theif
statement internally.如果两个动作相互排斥和/或相反(但彼此相关),我会更倾向于使用它,例如:
对于两个不相关的动作,我不太愿意使用它,原因是其中一个分支将来需要扩展的可能性较高。如果是这种情况,您将再次需要将其重写为
if-else
语句。想象一下:如果在某个时刻您决定第一个分支也需要
do_def()
,您需要将整个事情重写为if-else
> 再次声明。然而,三元运算符更常见的用法是:
通过这种方式,您可以将其作为表达式而不是语句进行计算。
I would be more inclined to use it in case the two actions are mutually-exclusive and/or opposite (yet related to each other), for example:
For two unrelated actions I would be less inclined to use it, the reason being that there is a higher probability for one of the branches to need expanding in the future. If that's the case, you will again need to rewrite it as an
if-else
statement. Imagine that you have:If at some point you decide that the first branch needs to
do_def()
as well, you'll need to rewrite the whole thing to anif-else
statement again.The more frequent usage of the ternary operator, however, is:
This way you are evaluating it as an expression, not as a statement.
真正的问题是,“三元形式比 if 形式可读性更强还是更差?”。我想说不是。但这是风格问题,而不是功能问题。
The real question is, "Is the ternary form more or less readable than the if form?". I'd say it isn't. But this is a question of style, not of function.