简体   繁体   English

在Java中使用/练习括号的最佳方法

[英]Best way to use/practice of using parentheses in java

While working on Sonar static code analyzer I found some confusing (may be only to me) statement by Sonar on using parentheses. 在使用Sonar静态代码分析器时,我发现Sonar关于使用括号的说法令人困惑(可能仅对我而言)。

Below are the few code snippets where Sonar says remove useless parentheses: 以下是Sonar所说的删除无用括号的一些代码片段:

line>1  String auth = "Basic "+ com.somepackge.someMethod(((String) (parent.proxyUsername+ ":" + parent.proxyPassword)));
line>2  return rawtime.length() > 3 ? (rawtime.substring(0, rawtime.length() - 2) + rawtime.substring(rawtime.length() - 2, rawtime.length()).toLowerCase()) : rawtime;

though I have replaced above lines with below one to keep Sonar calm :) : 虽然我用下面的一行替换了上面的行,以使声纳保持冷静:):

Line>3 String auth = "Basic "+ com.somepackge.someMethod((String) (parent.proxyUsername+ ":" + parent.proxyPassword));

Line>4 return  rawtime.length() > 3 ? rawtime.substring(0, rawtime.length() - 2) + rawtime.substring(rawtime.length() - 2, rawtime.length()).toLowerCase() : rawtime;

So the reason for discussing this question is: 因此,讨论此问题的原因是:

  1. Actually using braces/parentheses are way to reduce the confusion so why to remove those parentheses. 实际上,使用大括号/括号是减少混乱的方法,因此为什么要删除这些括号。

  2. What is best way to use parentheses while writing any complex statement in java. 在Java中编写任何复杂的语句时使用括号的最佳方法是什么。

See the line>1 and Line>4 here I think 我认为在这里看到行> 1和行> 4

(String) (parent.proxyUsername+ ":" + parent.proxyPassword)

this part of code should have the braces to avoid confusions but what Sonar expect is something like: 这部分代码应该有括号来避免混淆,但是Sonar期望的是:

   (String) parent.proxyUsername+ ":" + parent.proxyPassword

Any suggestion would be a great help. 任何建议都会有很大帮助。 I got some links regarding this question but those were not much helpful. 我有一些有关此问题的链接,但这些链接没有太大帮助。

Line 1 has redundant parentheses, but Line 2 's parentheses add clarity to the ternary statement. Line 1具有多余的括号,但Line 2的括号为三元语句增加了清晰度。

Whether or not the extra parenthesis in 2 are useful is up for debate - but there's no reason not to remove the redundant ones in 1 . 2中的多余括号是否有用尚待争论-但没有理由不删除1的多余括号。

Generally it's best to use extra parenthesis to convey your intent about what the code should do, or to remove ambiguity in the order that things occur. 通常,最好使用多余的括号来传达您对代码应执行的操作的意图,或消除事物发生的顺序的歧义。

There is a semantic difference between these two versions: 这两个版本之间在语义上有所不同:

(String) (parent.proxyUsername+ ":" + parent.proxyPassword)

(String) parent.proxyUsername+ ":" + parent.proxyPassword

In the first, the second set of () already evaluates to a String , implicitly calling parent.proxyUsername.toString() to convert proxyUsername to a String . 在第一组中,第二组()已经求值为String ,隐式调用parent.proxyUsername.toString()proxyUsername转换为String So the cast is redundant and should be removed IMHO. 因此,演员表是多余的,应删除恕我直言。 The second version casts parent.proxyUsername to String , and will throw an exception is it hasn't got runtime type String (only if it is declared a String is the cast redundant). 第二个版本将parent.proxyUsername强制转换为String ,并将抛出异常,因为它没有运行时类型String (仅当声明为String时才强制转换为冗余)。

I agree that line 2 and 4 are complicated to read no matter if they have the redundant braces or not. 我同意第2行和第4行无论是否具有多余的花括号都很难阅读。 Rewrite if you want clarity. 如果您想清楚,请重写。 That said, redundant braces are sometimes good for clarity IMHO, I do use them occasionally. 话虽如此,多余的花括号有时对清晰度很有帮助,恕我直言,我确实偶尔使用它们。

First snippet 第一个片段

String auth = "Basic "+ someMethod(((String) (parent.proxyUsername+ ":" + parent.proxyPassword)));

You could rewrite it as: 您可以将其重写为:

String auth = "Basic "+ someMethod(parent.proxyUsername+ ":" + parent.proxyPassword);

because the string concatenation operator already does a string conversion. 因为字符串连接运算符已经进行了字符串转换。 Unless you want a ClassCastException thrown when proxyUsername or proxyPassword are not Strings? 除非你想有一个ClassCastException时,抛出proxyUsernameproxyPassword不是字符串?

Second snippet 第二段

return rawtime.length() > 3 ? (rawtime.substring(0, rawtime.length() - 2) + rawtime.substring(rawtime.length() - 2, rawtime.length()).toLowerCase()) : rawtime;

The parenthesis is indeed unnecessary but the statement is quite unreadable. 括号的确是不必要的,但是该语句非常不可读。 If you want to keep using the ternary operator I would suggest splitting the statement across lines: 如果您想继续使用三元运算符,建议将语句分成几行:

return rawtime.length() > 3
       ? rawtime.substring(0, rawtime.length() - 2) + rawtime.substring(rawtime.length() - 2, rawtime.length()).toLowerCase()
       : rawtime;

or you could revert the condition: 或者您可以恢复条件:

return rawtime.length() <= 3 ? rawtime :
       rawtime.substring(0, rawtime.length() - 2) + rawtime.substring(rawtime.length() - 2, rawtime.length()).toLowerCase();

the best way is to put your class that you're casting to in a parentheses then the whole part to be converted in another parentheses, then include this whole code in a container parentheses, your code should look like this eg ((String)(x+y)). 最好的方法是将要转换的类放在括号中,然后将要转换的整个部分放在另一个括号中,然后将整个代码包含在容器括号中,您的代码应如下所示,例如(((String)( x + y))。

I hope that was helpful, thanks. 希望对您有所帮助,谢谢。

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

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