Should brackets always be placed around the triple operator?

Checkstyle complains about the following:

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

and says that parens are not needed.

While the statement certainly works fine without them, it seems that they look much more readable - otherwise, when I read it, I tend to see:

 return null 

and then you need to pause to consider the remaining

 == a ? a : new A(); 

since my brain has already gone down one path.

In addition, I try to do the same when I see the triple operator, if it is not grouped in parens.

So: should the guys around the triple be the de facto standard? Is there any reason not to post them there?

+7
java ternary-operator
source share
6 answers

Well, checkstyle is right, brackets are useless for execution. But useless for execution does not mean useless for a good reading of your code. You should leave them if it makes sense to read.

I think this code does not need more parentheses:

 int number = (myBoolean)? 1 : 2; 

but in your case, the return keyword and the fact that your logical expression is an expression can change the way you read the statement.

+6
source share

When reading the return statement, I know that everything is between 'return' and ';' this is what will be returned, so I cannot read your code sample as a null return value, followed by some characters, as you claim to be reading it.

Perhaps reading analysis methods can help you see it the way I do. However, I was not very well versed in parsing methods, although in recent years I have collected several parsers.

I always remove unnecessary parentheses. They do not help in understanding the code, since I know that the priority of Java operators is pretty good. Odd time I'm not sure, I add parentheses and wait to see if IDEA says they are redundant. Then I delete them and try to fix the priority rule that I just discovered in my memory.

In the code bases that I have inherited, I tend to find the largest number of fallback brackets in areas of the code that are poor for other reasons, so I am linking them.

+4
source share

No, this should not be the de facto standard. I prefer it without partners.

I think that the only reason laid in them is to force one to evaluate the order or to clarify the tangled line.

+2
source share

Both options are correct, use what your team uses, or what you like if you work alone.

IIRC By default, checkstyle uses Sun (rip) style rules, so if you want to conform to the standard, listen to it and remove the parens.

+1
source share

Generally not .

Parentheses are not required around ternary (also conditional) operators or their sections, since their priority is so small in the order of operations (just below logical operators and above assignments). See the link below for a complete table.

Thus, it can be argued that such unnecessary paranas visually clutter up the code, and they show a lack of understanding on the part of the programmer.

Exceptions that may require the use of partners in triples or around them:

  • If your ternar is complex enough to deserve a few lines; you could then surround your statement in parens to prevent the semicolon from setting automatically.

  • If your ternary is nested in another ternary.

See also MDN:

+1
source share

Since the basis of your question is related to the act of reading code, I will approach this issue from this point of view.

One of the main principles of the so-called "high-speed" curriculum is that they try to force the reader to develop gestalt lines of the text, and not read it sequentially in one word. You can try to take a page from your book and step back from your code - literally, if necessary - to get an idea of ​​the complete line, rather than consider the act of reading as if it were an act of parsing a token using a token.

Alternatively, you can use an editor that allows you to customize styles: you can make the three-dimensional operator in a different color so that it pops up on you. For example, Notepad ++ has a number of built-in themes that do this, like so many other editors.

0
source share

All Articles