-4

ok so i've seen [? and :] used in as replacement for some if statements in a lot of code

for example

driftDirec = Input.GetAxis("Horizontal")>0 ? 1: -1;

why not something like

if (driftDirec > 0)
{ do stuff}
else
{do stuff}

or something, can you tell me when is the appropriate time to use those and i don't know what that's called?

Rand Random
  • 7,300
  • 10
  • 40
  • 88
Furvus
  • 19
  • 2
  • First sample is an `assignment` while the second example is calling different code without assigning something. – Biesi Nov 29 '19 at 10:32
  • [Ternary ? operator vs the conventional If-else operator in c#](https://stackoverflow.com/questions/11643137/ternary-operator-vs-the-conventional-if-else-operator-in-c-sharp), [Benefits of using the conditional ?: (ternary) operator](https://stackoverflow.com/questions/3312786/benefits-of-using-the-conditional-ternary-operator), [Is the conditional operator slow? Performance review](https://stackoverflow.com/questions/2259741/), [Full if/else statement vs. Conditional Operator](https://stackoverflow.com/questions/3622244/). – xdtTransform Nov 29 '19 at 10:34

2 Answers2

0

The "[? and :]", as you call it, is called a "ternary operator". It is used as a shorthand for

if (condition)
  variable = value1;
else
  variable = value2;

// as ternary operator:
variable = (condition)? value1 : value2;

There is no necessity to ever use, but it can be shorter and therefore more readable. So use it whenever you want (maybe speak with your team, as some people don't like reading it), but I highly recommend not nesting them or use them with long expressions like this:

// please do not do this
very_long_variable_name = (this_condition_is_also_very_long)? a_complicated_expression-another_complicated_expression : yet_another_even_more_complicated_expression;

// or this
variable = (condition1)? (condition2)? value1 : value2 : (condition2)? value3 : value2:

I think it should be clear why this is a bad idea.

TLDR: Use it, when it makes your assignment more readable.

Jerome Reinländer
  • 1,227
  • 1
  • 10
  • 26
0

Its called tenrary conditional operator. Have a look here MSDN.

I think there is no general rule or something. So you have to choose when to use which synthax.

I for myself just look which is easier to read for someone else looking at my source.

For simple assignments I often use the tenrary conditional operator:

var moneyToPay = (person.Age > 12) ? 15 : 8;

If the result is only a bool value, you don't need the operator, because the condition itself is a bool.

bool isOldPerson = (person.Age > 45);

If there is only like one function which i want to call depending on the condition, sometimes i do this:

var moneyToPay = (person.Age > 45)
                 ? GetSomeValue()
                 : GetSomeOtherValue();

But this can get pretty messi really fast. If you want to do some extra work depending on the condition, you should use a normal if-statement like this:

if (person.Age > 45) 
{
    //Do stuff...
}
else 
{
    //Do stuff...
}

Just try to look at your code in the perspective of another developer. Is it really more easy to read with the tenrary conditional operator?

Olli
  • 658
  • 5
  • 26