Asked  6 Months ago    Answers:  5   Viewed   48 times

What are the benefits and drawbacks of the ?: operator as opposed to the standard if-else statement. The obvious ones being:

Conditional ?: Operator

  • Shorter and more concise when dealing with direct value comparisons and assignments
  • Doesn't seem to be as flexible as the if/else construct

Standard If/Else

  • Can be applied to more situations (such as function calls)
  • Often are unnecessarily long

Readability seems to vary for each depending on the statement. For a little while after first being exposed to the ?: operator, it took me some time to digest exactly how it worked. Would you recommend using it wherever possible, or sticking to if/else given that I work with many non-programmers?

 Answers

80

I would basically recommend using it only when the resulting statement is extremely short and represents a significant increase in conciseness over the if/else equivalent without sacrificing readability.

Good example:

int result = Check() ? 1 : 0;

Bad example:

int result = FirstCheck() ? 1 : SecondCheck() ? 1 : ThirdCheck() ? 1 : 0;
Tuesday, June 1, 2021
 
ajaybc
answered 6 Months ago
52

You can convert a lambda expression to a particular target delegate type, but in order to determine the type of the conditional expression, the compiler needs to know the type of each of the second and third operands. While they're both just "lambda expression" there's no conversion from one to the other, so the compiler can't do anything useful.

I wouldn't suggest using an assignment, however - a cast is more obvious:

Func<Order, bool> predicate = id == null 
    ? (Func<Order, bool>) (p => p.EmployeeID == null)
    : p => p.EmployeeID == id;

Note that you only need to provide it for one operand, so the compiler can perform the conversion from the other lambda expression.

Wednesday, June 2, 2021
 
medhybrid
answered 6 Months ago
65

The null propagation operator returns a value. And since you must have a variable on the left hand side of an assignment, and not a value, you cannot use it in this way.

Sure, you could make things shorter by using the tenary operator, but that, on the other hand, doesn't really help the readability aspect.

Joachim Isaksson's comment on your question shows a different approach that should work.

Thursday, June 10, 2021
 
edorian
answered 6 Months ago
24

A GNU extension to C allows the ternary conditional operator to evaluate to its condition if it's true and there's nothing in the first branch:

char *email = (char *)sqlite3_column_text(statement, 10);
email = email ? : "";

or, more exactly what you say you're looking for

char * email = (char *)sqlite3_column_text(statement, 10) ? : "";

This works when compiling with Clang, too.

Another possibility would be:

char *email = (char *)sqlite3_column_text(statement, 10);
email = (NULL == email) ? email : "";

But I think I'd recommend just going with your first option. Make a code snippet if you're doing it a lot.

Thursday, August 26, 2021
 
djdd87
answered 3 Months ago
93

Conditional operators are intentionally succinct and especially useful for assignments:

var a = x ? 1 : 2;

Using them to conditionally run functions, while possible, should, for the sake of readability be done using IF/ELSE statements:

// This is possible but IMO not best practice:
X ? doSomething() : doSomethingElse();

While long-winded, most of the time, this is the better solution:

if (X) {
    doSomething();
} else {
    doSomethingElse();
}

One notable benefit to the IF/ELSE structure is that you can add additional tasks under each condition with minimal hassle.

Your last snippet is also possible but it looks somewhat long-winded and, again, might be better suited to a more conventional logical structure; like an IF/ELSE block.

That said, a conditional operator can still be readable, e.g.

(something && somethingElse > 2) ?
   doSomeLongFunctionName()
   : doSomeOtherLongFunctionName();

In the end, like many things, it's down to personal preference. Always remember that the code you're writing is not just for you; other developers might have to wade through it in the future; try and make it as readable as possible.

Saturday, September 18, 2021
 
PeterTheLobster
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share