12

Edit This isn't a question of whether or not the ternary operator is harmful. It's about whether or not there's a consensus about whether or not it should be used outside of assignment statements. /Edit

Based on a boolean, I want to call one of two different functions (with no return value). I wrote this bit of javascript:

(condition) ? doThis(arg) : doThat(arg)

Which I think looks cleaner than writing out an if-else statement:

if(condition) {
    doThis(arg);
}
else {
    doThat(arg);
}

My colleagues strongly believe ternary statements should only be used in assignment statements. I can't find style guides that discuss this, probably because languages like Java and C# don't allow this at all (their compilers require ternary operators to return a value). Should this be considered a bad use of the ternary statement? Why?

BobbyA
  • 229
  • 2
  • 6
  • Keep in mind that the Jason Voorhees may be assigned maintain that code of yours. – David Hammen Aug 26 '15 at 02:37
  • 2
    @gnat - not a duplicate of that question, which is about general use of ?: in expressions, whereas this is specifically about using it as a replacement for if-else when there is no single value being calculated. – Jules Aug 26 '15 at 06:43
  • While I'm in two minds as to the appropriateness of the proposed duplicate, this question is still primarily opinion-based, so I don't see any point in reopening it. – Ixrec Aug 27 '15 at 21:15
  • Why do you think this looks cleaner? Breaking conventions like this is opposite of clean. – JacquesB Jan 18 '18 at 17:10

3 Answers3

12

Generally speaking, the conditional operator is intended to make an if statement with a value. w = x ? y : z. Thus, if you're using it for side effects, it's counter-intuitive. Valid, but counter-intuitive; and remember that you're writing code for your teammates to read.

asthasr
  • 3,439
  • 3
  • 17
  • 24
  • 1
    This seems right. Just about every usage example I find, [including Wikipedia](https://en.wikipedia.org/wiki/%3F:) uses the operator to perform conditional assignment. – BobbyA Aug 26 '15 at 17:44
4

A couple of cases, that are not assignments, where I find ternary operators useful are:

In function arguments: in my opinion

f( e1, .. cond ? em : dm, .. en);

is less error prone than

if ( cond) f( e1, .. em, .. en); else f( e1, .. dm, .. en);

When you want to call one function or another with identical arguments: again I find

(cond ? f : g)( /* long and complex argument list */);

less error prone than

if ( cond) f( /* long and complex argument list */);
else g( /* long and complex argument list */);

As I see it the key thing in each case is that the ? form has less repetition. Repetition opens up the possibility of changing one instance and forgetting to change the other.

dmuir
  • 41
  • 1
  • 6
    In these cases though, you're still using the ternary operator to return a value, which, according to syrion's answer above, is an expected usage. – BobbyA Aug 26 '15 at 17:35
  • @BobbyA what about void functions ? – pllee Aug 26 '15 at 20:54
1

Here is an example where a non-assignment use comes in handy, specifically to call one of two functions.

In a Javascript Promise, which operates on two function arguments, resolve and reject, it is possible to do the following one-liner without violating the intuition of the ternary operator returning a value:

((this.inputErrors.length === 0) ? resolve : reject)();

To me the above reads cleaner than:

if (this.inputErrors.length === 0) {
    resolve();
} else {
    reject();
}

Note: I know this is similar to this answer, but hopefully easier to understand.

Jan Żankowski
  • 287
  • 1
  • 3
  • 8