views:

874

answers:

12
repeater.StartIndex = layout == ProductLayout.Grid4Columns ? startIndex - 1 : 0;

I don't get it. What is layout == [expression] doing here in this case as it sets StartIndex

Can somebody help explain what this code is doing?

+17  A: 

This code is equivalent to:

if(layout == ProductLayout.Grid4Columns)
    repeater.StartIndex = startIndex - 1;
else
    repeater.StartIndex = 0;

Hope that helps

EDIT:

The conditional operator ?: is the only ternary operator in C# (I believe). The first operand is a boolean value, if this is true, the value of the whole expression is the second term, otherwise, the value of the entire expression is the third term.

value = (condition) ? (valueIfConditionIsTrue) : (valueIfConditionIsFalse)

LorenVS
You mean "ternary", not "tertiary"
Marc Gravell
it's not the conditional that's the problem. It's chaining a bunch of stuff together on one line that is lazy code smell
CoffeeAddict
My bad, thanks for the clarification on the operator name... Not one I have to spit out very often
LorenVS
how is that lazy code exactly? that's what the ternary operator was made for..
Stan R.
There is no chain. There is one conditional, being used in an assignment. The conditional, like all conditionals, has three expressions.
Adam Bellaire
Sounds more like lazy coder smell to me...heh
Justin Niessner
"it's not the conditional that's the problem. It's chaining a bunch of stuff together on one line that is lazy code smell " - Not reading the language specs to know about ternary operators smells a little lazy too.
EBGreen
In all seriousness, as much as there isn't a problem with the code as it is written, the one thing about the ternary operator that makes it hard to approach is that its hard to define exactly where the different terms end. A coder who isn't used to the operator, looking at this line sees 4 terms seperated by 3 operators.It's not a problem with his coding style, but unless you're used to it, it can become more difficult to read
LorenVS
lazy as in chaining. The ternary is not lazy, it's chaining to set other variables and chaining is harder to read.
CoffeeAddict
I've used ternaries before but only ONE variable on left side. Why? because it's much easier to read.
CoffeeAddict
EBGreen. How do you assume I do not know what a ternary is. I did before I posted. The part I did not get was the chaining as it was just reading really weird to me.
CoffeeAddict
Is chaining so bad? If you use Linq you chain allot.
David Basarab
@coffeeaddict-"I've used ternaries before but only ONE variable on left side" ...? ... I only see 1 variable on the left side. Is it the lack of parentheses confusing you?
John MacIntyre
no, I'm saying break that ternary out then into its own variable if you're got more than just one variable on the left side of the ternary. I'm talking about ONE on the left side of the ternary. That's not the case here.
CoffeeAddict
I also cannot see more than one variable on the left side. I see only one variable on the left side: repeater.StartIndex. Then there is a single boolean expression: layout == ProductLayout.Grid4Columns and then finally the two return values. You say you would break it out into its own variable, could you show an example please?
Chris Dunaway
A: 

The ? is a conditional operator that states if condition then x else y on a single line

int x = ( condition ) ? 0 : 1;

Would result in x = 0 if condition == true or x = 1 if condition == false

Adam Fox
+1  A: 

It is called ternary operator. More information at the following link.

http://msdn.microsoft.com/en-us/library/ty67wk28%28VS.80%29.aspx

idursun
Pure myth. It is called the "conditional" operator. It happens to be ternary (as opposed to unary or binary)
Marc Gravell
It's a semantic issue. Its proper name is the conditional operator. However, you can say the ternary operator when referring to it, because it is (typically) the only ternary operator in the language. If your language has multiple ternary operators, that usage would be confusing and incorrect, of course.
Adam Bellaire
It was not the ternary that threw me off. It was the entire line.
CoffeeAddict
+1  A: 

Just break it into pieces:

repeater.StartIndex = layout == ProductLayout.Grid4Columns ? startIndex - 1 : 0;

repeater.StartIndex = (layout == ProductLayout.Grid4Columns ? startIndex - 1 : 0);

bool criteria = (layout == ProductLayout.Grid4Columns);
repeater.StartIndex = criteria ? startIndex - 1 : 0;

In other words, if layout is equal to Grid4Columns, it will set start index to "startIndex - 1", otherwise, it will set StartIndex to 0.

Reed Copsey
A: 

Equivalent to

   if(layout == ProductLayout.Grid4Columns)
    {
      repeater.StartIndex = startIndex - 1;
    }
    else
    {
      repeater.StartIndex = 0;
    }
Steve Gilham
A: 

Unless I'm mistaken and this does something else in C# than in PHP, that's a ternary expression, which written in verbose form would be:

if ( layout == ProductLayout.Grid4Columns ) {
    repeater.StartIndex = startIndex - 1;
} else {
    repeater.StartIndex = 0;
}
Jakob
+1  A: 

It is not chained just simple use of ?: Much more readable than

if(layout == ProductLayout.Grid4Columns)
{
     repeater.StartIndex = startIndex - 1 ;
}
else
{
    repeater.StartIndex = 0;
}
Mark
A: 

Its an Inline If Else Statement.

Havenard
+3  A: 

I don't like it... I feel it's fairly ambiguous about ordering. It's not clear what was intended, and if that matches how it's actually executed.

Is it

repeater.StartIndex = (layout == ProductLayout.Grid4Columns) ? startIndex - 1 : 0;

or

repeater.StartIndex = layout == (ProductLayout.Grid4Columns ? startIndex - 1 : 0);

Things could get screwy if operator precedence is not what you expect, and you're unlucky enough that the types are compatible.

Add the appropriate bracketing, though, and I have no problem with it.

patros
dude, I'm not like this at work, chill. It's called frustration. Relax.
CoffeeAddict
I'm not the one who freaked out. In fact my post seems pretty calm.
patros
"I'd rather work with whoever wrote this than with you." Nice. That's a lame assumption.
CoffeeAddict
so why did 10 people yell at me and call me stupid when 2 people also think this is bad code. I find that interesting. So does that mean the rest are bad coders ? hehe, ok I'll stop.
CoffeeAddict
I find that many others here are obnoxious just as I was perceived.
CoffeeAddict
by the way patros, thanks for your answer even though I did not like your comment about me personally.
CoffeeAddict
+1  A: 

Although the syntax seems hackish at first glance, after you get used to it it becomes a great shortcut for simple things like null substitution.

public myNormalizedString = someString ? null : "";
Robert Harvey
yes, that I'm used to.
CoffeeAddict
A: 

When I write a statement like this, I add parentheses to make the intent of the statement clearer:

repeater.StartIndex = 
  (layout == ProductLayout.Grid4Columns ? startIndex - 1 : 0);
Robert Harvey
+1 I see no good reason for a -1. Especially without comments.
EBGreen
I don't think it is all that more clear for me personally but that is a purely personal decision so I don't see any good reason for a DV.
EBGreen
A: 
repeater.StartIndex = layout == ProductLayout.Grid4Columns ? startIndex - 1 : 0;

layer == [expression] => [expression]
What this means that the above statement can be treated like a black back reducing to the following expression:

repeater.StartIndex = (some logic)?0:n ,where n is some value greater than zero. The purpose of n begin a relative position in the grid.


Note, layout holds a reference to a instance of ProductLayout.member. All the code means is that in the special case (i.e. when setting the repeater's startIndex when the layout type is Grid4Columns) then the start index is not 0.