Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the point of using ternary operators instead of IF THEN? [closed]

Tags:

c#

why make things more complex? why do this:

txtNumerator.Text = 
     txtNumerator.Text == "" ? "0" : txtNumerator.Text;

instead of this:

if txtNumerator.Text="" {txtNumerator.Text="0";}
like image 657
Alex Gordon Avatar asked Oct 11 '10 20:10

Alex Gordon


People also ask

Why we use ternary operator instead of if-else?

We can use the ternary operator in place of if-else conditions or even switch conditions using nested ternary operators. Although it follows the same algorithm as of if-else statement, the conditional operator takes less space and helps to write the if-else statements in the shortest way possible.

What is the point of ternary operators?

Programmers use the ternary operator for decision making in place of longer if and else conditional statements. The ternary operator take three arguments: The first is a comparison argument. The second is the result upon a true comparison.


2 Answers

Suppose you wanted to pass either zero or txtNumerator.Text to a method M. How would you do that?

You could say:

string argument;
if (txtNumerator.Text == "")
{
    argument = "0";
}
else
{
    argument = txtNumerator.Text;
}
M(argument);

Or you could say

M(txtNumerator.Text == "" ? "0" : txtNumerator.Text);

The latter is shorter and easier to read.

The larger point here is that statements are useful for their side effects and expressions are useful for their values. If what you want to do is control which of two side effects happens, then use an "if" statement. If what you want to do is control which value gets chosen from two possibilities, then consider using a conditional expression.

UPDATE:

Jenny asks why not just do this?

if (txtNumerator.Text == "")
{
    M("0");
}
else
{
    M(txtNumerator.Text);
}

That's fine if there's just one condition to check. But what if there are, say, four? Now there are sixteen possibilities and writing the "if" statement for it gets messy to say the least:

if (text1.Text == "")
{
    if (text2.Text == "")
    {
        if (text3.Text == "")
        {
            if (text4.Text == "")
            {
                M("1", "2", "3", "4");
            }
            else
            {
                M("1", "2", "3", text4.Text);
            }
        }
        else
        {
            if (text4.Text == "")
            {
                M("1", "2", text3.Text, "4");
            }
            else
            {
                M("1", "2", text3.Text, text4.Text);
            }
        }
        ... about fifty more lines of this.

Instead, you can just say:

M(Text1.Text == "" ? "1" : Text1.Text,
  Text2.Text == "" ? "2" : Text2.Text,
  Text3.Text == "" ? "3" : Text3.Text,
  Text4.Text == "" ? "4" : Text4.Text);
like image 76
Eric Lippert Avatar answered Dec 02 '22 04:12

Eric Lippert


It's one expression, so you can use the result directly in an assignment or a function call without having to duplicate the context in which you're using it. That makes a lot of usage scenarios significantly cleaner to write and read. For example:

int abs = (x >= 0) ? x : -x;

versus

int abs;
if (x >= 0)
    abs = x;
else
    abs = -x;
like image 44
mqp Avatar answered Dec 02 '22 05:12

mqp