Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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

share|improve this question

marked as duplicate by Robert Harvey, Oded, Thomas Owens Mar 2 '12 at 16:30

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

5  
I got confused by the horrible '{' '}' each getting their own line. –  Paul Mar 2 '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 2 '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 2 '12 at 16:40
add comment

6 Answers

up vote 9 down vote accepted

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;
}
share|improve this answer
2  
+1 for nested usage. –  Bernard Mar 2 '12 at 16:22
add comment

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).

share|improve this answer
add comment

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;
share|improve this answer
add comment

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.

share|improve this answer
add comment

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

share|improve this answer
add comment
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.

share|improve this answer
    
+1 for the final example, definitely the best way to do it. –  Izkata Mar 2 '12 at 17:04
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.