admin管理员组

文章数量:1327849

Is it a good practice to use the ternary operator for this:

answersCounter = answer.length != 0 ? ++answersCounter : answersCounter;

Is it a good practice to use the ternary operator for this:

answersCounter = answer.length != 0 ? ++answersCounter : answersCounter;

This is a question that I always asked myself as it happens quite often. Or, is it better to use a normal if statement? For me, this looks much cleaner in one line.

Share Improve this question edited Aug 16, 2020 at 7:13 craigcaulfield 3,53810 gold badges35 silver badges43 bronze badges asked Jun 13, 2016 at 23:04 FraKFraK 1,0891 gold badge15 silver badges24 bronze badges 14
  • 3 answersCounter += answer.length != 0; is all you need – Pointy Commented Jun 13, 2016 at 23:05
  • that looks nice ! but how does it work ?, as the result of the evaluation is a boolean and the variable is a number. This cannot work in Java right ? – FraK Commented Jun 13, 2016 at 23:08
  • 2 Not really. You should really just use the straightforward, obvious if statement rather than trying to get fancy. – Louis Wasserman Commented Jun 13, 2016 at 23:08
  • The right-hand side evaluates to a boolean result (true or false). The += operator will coerce that to type Number (1 or 0). Try it in your console! – Pointy Commented Jun 13, 2016 at 23:13
  • And no @Frak it won't work in Java, but then lots of stuff won't work in one language or the other; Java and JavaScript have as much in mon as "ham" and "Hamlet". – Pointy Commented Jun 13, 2016 at 23:13
 |  Show 9 more ments

2 Answers 2

Reset to default 7

This is just opinion, but I think that writing the increment like you have it is somewhat poor style.

Assigning a variable to a pre-incremented version of itself is a little bit confusing. To me, the best code is the clearest (excepting nods to optimization where necessary), and sometimes brevity leads to clarity and sometimes it does not (see anything written in Perl... I kid, sorta).

Have you ever had the programming trick question of:

int i = 5;
i += i++ + i;

Or something similar? And you think to yourself who would ever need to know how that works out since when would you ever assign a variable to the pre/post increment version of itself? I mean, you would never ever see that in real code, right?

Well, you just provided an example. And while it is parseable, it is not idiomatic and not clearer than a straight forward if.

E.g.

if (answer.length != 0) answersCounter++;

Of course, some people don't like if statements with out braces, and don't like braces without newlines, which is probably how you ended up with the ternary. Something with the coding style needs to be re-evaluated though if it is resulting in (subjectively) worse code to avoid a few carriage returns.

Again, this is opinion only, and certainly not a rule.

For Javascript
As it's unclear whether OP is asking about Java, JavaScript or genuinely both. Also know this is an old question but I've been playing with this and ended up here so thought it worth sharing.

The following does nothing, as incrementers within ternary operators don't work as expected.

let i = 0;
const test = true;
i = test ? i++ : i--; 
console.log(i) // 0

Switching ++ to +1 and -- to -1 does work.
However it conceptually is a little strange. We are creating an increment of the variable, then assigning that incremented variable back to the original. Rather than incrementing the variable directly.

let i = 0;
const test = true;
i = test ? i+1 : i-1; 
console.log(i) // 1

You can also use the logical operators && and ||.
However I personally find this harder to read and know for sure what will be output without testing it.

let i = 0;
const test = true;
i = test && i+1 || i-1; 
console.log(i) // 1

But at the end of the day as mented above, an if else statement seems to be the clearest representation.
This increments the variable directly, and if brevity is the aim then it can still all go on one line.

let i = 0;
const test = true;
if (test) { i++ } else { i-- }
console.log(i) // 1

本文标签: javascriptTernary Operator use to increase variableStack Overflow