简体   繁体   English

是否应该在三元运算符周围放置括号?

[英]Should parenthesis always be placed around the ternary operator?

Checkstyle complains about the following: Checkstyle抱怨以下内容:

return (null == a ? a : new A());

and says the parens are unnecessary. 并且说这些是不必要的。

While the statement certainly works fine without them, it seems far more readable with them present---otherwise as I'm reading it I tend to see: 虽然声明在没有它们的情况下确实可以正常工作,但它们在现在看起来更具可读性 - 否则当我读它时我倾向于看到:

return null

first and then have to pause to consider the remaining 首先,然后必须暂停考虑剩下的

== a ? a : new A(); 

part, since my brain has already gone down one path. 部分,因为我的大脑已经走了一条路。

Furthermore, I tend to do the same thing whenever I see a ternary operator, unless it is grouped in parens. 此外, 每当我看到三元运算符 ,我倾向于做同样的事情,除非它被分组为parens。

So: should parens around the ternary be the de facto standard? 所以:围绕三元的parens应该是事实上的标准吗? Is there ever any reason to not put them there? 有没有理由不把它们放在那里?

Well, checkstyle is right, the parentheses are useless for the execution. 好吧,checkstyle是对的,括号对执行没用。 But useless for the execution doesn't mean useless for the good reading of your code. 但是执行无用对于良好的代码读取并不意味着无用。 You should leave them if it makes more sense to read. 如果阅读更有意义,你应该留下它们。

I think this code doesn't need more parentheses: 我认为这段代码不需要更多括号:

int number = (myBoolean)? 1 : 2;

but in your case the return keyword and the fact your boolean is an expression can change the way you read the statement. 但在你的情况下, return关键字和你的布尔值是表达式的事实可以改变你阅读语句的方式。

When reading a return statement, I know that everything between 'return' and ';' 在阅读return语句时,我知道'return'和';'之间的所有内容 is what is going to be returned, so there is no way I can read your code sample as return null followed by some symbols as you claim you read it. 是什么将被返回,所以我无法读取您的代码示例作为返回null后跟一些符号,因为您声称你读它。

Perhaps reading up on parsing techniques might help you to see it as I do. 也许阅读解析技术可能会帮助你像我一样看到它。 That said, I haven't really read up on parsing techniques, though I've cobbled a few parsers together over the years. 也就是说,我还没有真正阅读过解析技术,尽管多年来我一起拼凑了几个解析器。

I always remove unnecessary parentheses. 我总是删除不必要的括号。 They don't aid in code comprehension, as I know Java's operator precedence pretty well. 它们无助于代码理解,因为我非常了解Java的运算符优先级。 The odd time I'm unsure, I add parentheses and wait to see whether IDEA tells me they're redundant. 奇怪的是,我不确定,我加上括号,等待IDEA告诉我他们是多余的。 Then I remove them, and try to commit to memory the precedence rule I've just discovered. 然后我删除它们,并尝试提交内存我刚刚发现的优先规则。

In the codebases I inherited, I tend to find the greatest number of redundant parentheses in areas of code that are poor for other reasons, so I associate the two. 在我继承的代码库中,我倾向于在代码区域中找到最多的冗余括号,这些代码区域由于其他原因而很差,所以我将两者联系起来。

No, it should not be the de facto standard. 不,它不应该是事实上的标准。 I prefer it without parens. 没有parens,我更喜欢它。

I think the only reason to put them there is to force evaluation order or to clarify a confusing line. 我认为将它们放在那里的唯一理由是强制执行评估订单或澄清令人困惑的界限。

Both options are correct, use what your team uses or what you like if you are working alone. 这两个选项都是正确的,使用您的团队使用的或您喜欢的单独工作。

IIRC By default checkstyle uses Sun's (rip) style guidelines, so if you want to conform to standard style, listen to it and remove the parens. IIRC默认情况下,checkstyle使用Sun(rip)样式指南,因此如果您想要符合标准样式,请听取它并删除parens。

In general, no . 一般来说, 没有

Parentheses are not required around ternary (also known as conditional) operators or its sections, because their precedence is so very low in the order of operations (just below logical operators and above assignments). 三元(也称为条件)运算符或其部分不需要括号,因为它们的优先级在运算顺序上非常低(仅在逻辑运算符和上面的赋值之下)。 See the link below for the full table. 请参阅下面的链接以获取完整表格。

It could be argued, therefore, that such unnecessary parens visually clutter the code, and they reveal a lack of comprehension on the part of the programmer. 因此,可以说,这些不必要的问题在视觉上使代码混乱 ,并且它们表明程序员缺乏理解

Exceptions that might require use of parens in or around ternaries would be: 可能需要在三元组中或周围使用parens的例外情况是:

  • If your ternary is complex enough to merit multiple lines; 如果你的三元素足够复杂,值得多行; you might then surround your statement in parens in order to prevent automatic semicolon insertion. 然后你可以用parens包围你的陈述,以防止自动分号插入。

  • If your ternary is nested within another ternary. 如果你的三元嵌套在另一个三元组中。

See also on MDN: 另见MDN:

Since the basis for your question has to do with the act of reading code, I'll approach the question from that perspective. 由于您的问题的基础与阅读代码的行为有关,我将从这个角度处理问题。

One of the basic principles of so-called "speed-reading" training programs is that they try to get the reader to develop a gestalt of the line of text rather than reading it sequentially word-by-word . 所谓的“速读”训练计划的基本原则之一是,他们试图让读者发展文本行的格式 ,而不是逐字逐句地阅读。 You might try to take a page from their book and step back from your code -- literally if necessary-- to get a sense of the full line rather than treating the act of reading as if it were the act of parsing token by token. 您可能会尝试从他们的书中取出一个页面并从代码中退一步 - 从字面上看,如果必要的话 - 了解整行,而不是将阅读行为视为通过令牌解析令牌的行为。

Alternatively, you could use an editor that lets you configure styles: you could make the ternary operator a different color so it jumps out at you. 或者,您可以使用一个允许您配置样式的编辑器:您可以使三元运算符具有不同的颜色,以便它跳出来。 Notepad++, for example, has a number of built-in themes that do this, as do many other editors. 例如,Notepad ++有许多内置主题可以执行此操作,许多其他编辑器也是如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM