2

Possible Duplicate:
Is the ternary operator evil?

Does using shorthand if else statments hurt readability?

Compare:

string name = street != null ? street.Owner : "Not known";

With:

string name;

if(street != null)
{
    name =  street.Owner;
}
else
{
    name = "Not known";
}

I have become familiar enough with the shorthand that its just as readable for short statements as the long version. Is that true for most developers or should I favor the more verbose version

Tom Squires
  • 17,695
  • 11
  • 67
  • 88
  • 5
    I got confused by the horrible '{' '}' each getting their own line. – Paul Mar 02 '12 at 16:20
  • 5
    It's not an if/else shorthand, it's a ternary operator, which isn't exactly the same thing (you're comparing multiple statements using control structures with an expression). And it hurts readability if you abuse it, not if you use it for what it is. – haylem Mar 02 '12 at 16:33
  • As @haylem said they are not the same thing. They may not always compile to the same thing. Just something to be aware of. I now the standard if is more efficient in c++, not sure for c#. – Ash Burlaczenko Mar 02 '12 at 16:40
  • A slightly better one-liner: `string name = street?.Owner ?? "Not known";` Not exactly the same though, as it will overwrite non-null `street` if `street.Owner == null`. – Gábor Imre Feb 14 '20 at 15:22

6 Answers6

15

What you are referring to is a ternary operator. In my opinion it can increase readability for simplistic conditional checks like the one in your question. It's short and provides a clear and obvious pair of return values. Nesting them hurts readability. While not quite a duplicate question the "hurts" is clearly demonstrated in THIS question.

Code from that question:

int median(int a, int b, int c) {
    return (a<b) ? (b<c) ? b : (a<c) ? c : a : (a<c) ? a : (b<c) ? c : b;
}
Rig
  • 1,497
  • 17
  • 21
  • In that particular case, just use Min() and Max(). `return Max(a,b) < c ? Max(a,b) : Max(c, Min(a,b));`. Of course, breaking this apart into variable names might be easier to read... – Mateen Ulhaq Feb 25 '16 at 20:42
  • 1
    With recursion! (Don't actually do this.) `return Max(a, b) < c ? Max(a, b) : median(Min(a, b), c, Max(a, b));` – Mateen Ulhaq Feb 25 '16 at 20:46
3

It took me about five minutes to get used to the short form (a "ternary operator").

The ternary operator is much better for this particular example, because it's just a validation transformation, if you will. As part of a validation process, there can be many of these, and using ordinary if/thens can quickly become unwieldy.

Robert Harvey
  • 198,589
  • 55
  • 464
  • 673
2

It's readable as long as you don't abuse it by using complicated boolean logic or using too much logic.

Example:

string name = street != null & street.Length > 0 ? GetNumber(street) + " " + street + " " + GetOwner(street) : Properties.Resources.UnknownStreet;
Bernard
  • 8,859
  • 31
  • 40
2
string name = street != null ? street.Owner : "Not known";

This one was horrible to read at the first sight! I was busy figuring out name = street but realized later that your syntax was actually perfect.

string name;

if(street != null)
{
name =  street.Owner;
}
else
{
name = "Not known";
}

This one i didn't too effort to understand at all (partly because the first one fixed my brain already). However, i agree that this is too inefficient.

However, keeping brevity while the readability both, i would have done this way,

string name = (street != null) ? (street.Owner) : "Not known";

This is fine, but i won't use ternary operator ? in lieu of if ever, in case either of the clauses are more than a single variable. or any complex expression with more than 2 parenthesis or 2 lines.

Dipan Mehta
  • 10,542
  • 2
  • 33
  • 67
  • 2
    +1 for the final example, definitely the best way to do it. – Izkata Mar 02 '12 at 17:04
  • I find the pattern `var x = a != b ? a : b` to be common enough as to not really need the parens--basically, a ternary operation as an assignment should be recognizable enough. That's just me, not claiming to speak for others. – Patrick Szalapski Apr 17 '17 at 13:38
0

I often hear form programmers coming from other languages than it hurts readability. But once they got used to it, it becomes natural. For programmers of other languages (e.g. Delphi) it may not translate to anything they already know, so they think it's difficult.

When you know the language, I think it actually improves readability (unless you chain this shorthand multiple times or use really complex conditions).

Doug
  • 113
  • 6
0

Like most of the shorthand, it can be good for readability if use correctly.

If you start to merge long condition with other ternary operator, you will start having trouble.

If you hesitate, I suggest to do a code review about it. If everyone in your team prefer the shorthand version, go with it.

Readability is subjective

Nettogrof
  • 229
  • 2
  • 7