6

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.

craigcaulfield
  • 3,381
  • 10
  • 32
  • 40
FraK
  • 919
  • 1
  • 13
  • 21
  • 3
    `answersCounter += answer.length != 0;` is all you need – Pointy Jun 13 '16 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 Jun 13 '16 at 23:08
  • 2
    Not really. You should really just use the straightforward, obvious `if` statement rather than trying to get fancy. – Louis Wasserman Jun 13 '16 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 Jun 13 '16 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 common as "ham" and "Hamlet". – Pointy Jun 13 '16 at 23:13
  • `answersCounter += answer.length != 0 ? 1 : 0;` – tkausl Jun 13 '16 at 23:14
  • @tkausl that's fine but it's precisely identical to what my first comment's code will do. – Pointy Jun 13 '16 at 23:14
  • In any case, in my opinion it's ugly and redundant to use *both* the pre-increment operator with its side-effect *and* an assignment. – Pointy Jun 13 '16 at 23:15
  • @Pointy the difference is, that mine works in java – tkausl Jun 13 '16 at 23:15
  • @tkausl so? **The two languages are completely different.** If you want to restrict your coding practices to things that work only in both languages, you're going to run into a lot of very serious problems. Follow your bliss, however :) – Pointy Jun 13 '16 at 23:17
  • He asked for Java in his comment so I restricted my coding practice to something which works in Java. – tkausl Jun 13 '16 at 23:20
  • @tkausl fair enough. I made the possibly invalid assumption that that was just a mistake; that happens a lot, but you may be right that the OP really did want a solution workable in Java. – Pointy Jun 13 '16 at 23:21
  • I was looking for a general solution, so it was really nice to see both options, the Java one looks good to me as well, thanks for the option, but still the question is, is it a good practice ? or should I use the if statement ? – FraK Jun 13 '16 at 23:23
  • `answer.length && ++answersCounter;` is all you *really* need. I limit the use of the ternary operator for few specific cases (return a or b, var a = a or b) whenever a decision has to be made between two expressions WITHOUT side effects. – le_m Jun 13 '16 at 23:48

2 Answers2

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.

Trevor Freeman
  • 7,112
  • 2
  • 21
  • 40
  • That is a really nice tricky question, thanks for it !!! And I think being more specific with an if statement is the way to go here :) – FraK Jun 14 '16 at 01:25
0

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 commented 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
Kris Gesling
  • 176
  • 1
  • 4