Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should parenthesis always be placed around the ternary operator?

Checkstyle complains about the following:

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.

So: should parens around the ternary be the de facto standard? Is there ever any reason to not put them there?

like image 678
Tom Tresansky Avatar asked Sep 01 '10 17:09

Tom Tresansky


People also ask

Which is the correct option for ternary operator?

?: = Question Mark Colon is also called C Ternary Operator.

What are the three conditions in a ternary operator?

The conditional (ternary) operator is the only JavaScript operator that takes three operands: a condition followed by a question mark ( ? ), then an expression to execute if the condition is truthy followed by a colon ( : ), and finally the expression to execute if the condition is falsy.

How do you use a ternary operator example?

It helps to think of the ternary operator as a shorthand way or writing an if-else statement. Here's a simple decision-making example using if and else: int a = 10, b = 20, c; if (a < b) { c = a; } else { c = b; } printf("%d", c); This example takes more than 10 lines, but that isn't necessary.

Is using ternary operator bad practice?

Ternary operators are not bad. However, many people choose not to use them because they can be difficult to parse on first glance. The expressiveness that you get from using if/else conditionals is the same as a ternary - mostly - but it allows for better readability.


2 Answers

When reading a return statement, I know that everything between 'return' and ';' 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.

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. The odd time I'm unsure, I add parentheses and wait to see whether IDEA tells me they're redundant. 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.

like image 99
Ricky Clarkson Avatar answered Nov 03 '22 21:11

Ricky Clarkson


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:

  • 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.

  • If your ternary is nested within another ternary.

See also on MDN:

  • Operator precedence (order of operations)

  • Conditional (ternary) operator

  • Automatic semicolon insertion

like image 29
2540625 Avatar answered Nov 03 '22 21:11

2540625