Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

? operator without else-part

I use C# ? operator when I have if-statements that affects one row and it's all good. But lets say I have this code (using classic if-statements):

if(someStatement) {     someBool = true;  //someBools value is unknown } else {     //Do nothing } 

This can be achieved on a one-liner by doing:

someBool = (someStatement) ? true : someBool; 

But why can't I do something like this:

someBool = (someStatement) ? true : ; //or possibly someBool = (someStatement) ? true; 

Is this possible in some way? If it is, is there any benefits of using one method over the other? And if not, why shouldn't you be able to do it?

like image 812
Johan Hjalmarsson Avatar asked Jul 24 '14 13:07

Johan Hjalmarsson


People also ask

Can we write ternary operator without else?

You cannot use ternary without else, but you can use Java 8 Optional class: Optional.

Which operator is used instead of if else?

The conditional operator – also known as the ternary operator – is an alternative form of the if/else statement that helps you to write conditional code blocks in a more concise way. First, you need to write a conditional expression that evaluates into either true or false .

What are the 3 conditional operators?

There are three conditional operators: && the logical AND operator. || the logical OR operator. ?: the ternary operator.

How do you do nothing in ternary operator?

A ternary operator doesn't have a do nothing option for either the true or false path since the entire ternary command returns a value and so both paths must set a value. The “do nothing” value will depend on what value you want the ternary operator to return to indicate “do nothing”.


2 Answers

You can do:

someBool = (someStatement) ? true : someBool; 

I don't think that gains you a lot of clarity over:

if (someStatement) {     someBool = true; } 

But it really seems to be a matter of taste. I wouldn't say either is clearly bad, but the former is uncommon, so I'd probably avoid it.


You ask why you can't use the operator like this:

someBool = (someStatement) ? true : ; 

This would be a very big language change! Bear in mind that an assignment looks like this:

<location> = <expression>; 

The expression is evaluated to give some value, and that value is stored in location. (Depending on whether location is a variable, property, field or indexing expression the "storing" operation could be quite different.)

Here you're proposing that the value of the expression on the right, in addition to its normal values, can be a "no-change" value, which has the special behaviour that when you use it in an assignment statement it causes no store operation to occur. That's different from any other normal value, and potentially surprising. But what would it mean if you used it in other places?

// Does this call DoSomething when cond is false? // If so, what value is passed to it? someObject.DoSomething(cond?x:);  // What happens here if cond is false? Does it cancel // the entire assignment? int x = 77 + (cond?2:) * 3 - 4;  // If cond is false, are methods F1 and F2 called or not called? int x = F1() + (cond?2:) + F2();  // What does this do? Does it skip the return if cond is false? return (cond?2:); 

I think you'd find it extremely hard to come up with sensible, intuitive and consistent behaviour for the operator in all these circumstances, and I don't think it would be useful anywhere other than in a simple assignment. It just doesn't fit with the rest of the language - including it would make the language harder to learn, read, understand, implement and explain. It's just not worth it for a tiny bit of conciseness.

like image 61
Weeble Avatar answered Sep 20 '22 15:09

Weeble


Basically, you're trying to use the conditional operator for something that it's not designed for.

It's not meant to optionally take some action... it's meant to evaluate one expression or another, and that be the result of the expression.

If you only want to perform an action when some condition is met, use an if statement - that's precisely what it's there for.

In your example, you could use:

// Renamed someStatement to someCondition for clarity someBool |= someCondition; 

or

someBool = someCondition ? true : someBool; 

... in other words "use the existing value unless someCondition is true... but personally, I think the original if statement is clearer.

like image 25
Jon Skeet Avatar answered Sep 22 '22 15:09

Jon Skeet