14 Replies - 1277 Views - Last Post: 06 November 2012 - 08:17 AM Rate Topic: -----

#1 Julyuary  Icon User is offline

  • D.I.C Head

Reputation: 38
  • View blog
  • Posts: 109
  • Joined: 20-December 11

Conditional Operator?

Posted 03 November 2012 - 04:29 PM

So earlier I was watching YouTube videos on rectangle collision in XNA and saw some syntax I'd never seen before, so I Google'd conditional statements in c# and found this (?: operator). I honestly prefer this

            if (playerRectangle.Intersects(tileRectangle))
                background.color = Color.Blue;

to
            background.color = playerRectangle.Intersects(tileRectangle) ? Color.Blue : Color.White;

The latter just isn't as readable and neat to me. Is there any real reason to use this over if-else, and should I feel stupid for just finding out about this?

Is This A Good Question/Topic? 0
  • +

Replies To: Conditional Operator?

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5676
  • View blog
  • Posts: 12,194
  • Joined: 02-June 10

Re: Conditional Operator?

Posted 03 November 2012 - 05:05 PM

Readability is in the eye of the coder. It probably seems less readable to you because its new. LINQ is like that to me because I don't use it much.

StatusPictureBox.Image = boolReady ? GreenLED : RedLED;

This is very readable to me, and conveys the intent of the developer.


Also your first example is not equivalent to your second example.
The second example sets to either Blue or White as needed.
The first example does nothing if false. It only set to blue if True.
Was This Post Helpful? 3
  • +
  • -

#3 MrShoes  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 314
  • View blog
  • Posts: 490
  • Joined: 13-June 12

Re: Conditional Operator?

Posted 03 November 2012 - 05:06 PM

Sometimes it can shorten this
if(value > maxValue)
     variable = maxValue;
else
     variable = value;


to this
variable = value > maxValue ? maxValue : value;


It might look unwieldy at first, but you get used to it.
Was This Post Helpful? 3
  • +
  • -

#4 Julyuary  Icon User is offline

  • D.I.C Head

Reputation: 38
  • View blog
  • Posts: 109
  • Joined: 20-December 11

Re: Conditional Operator?

Posted 03 November 2012 - 05:44 PM

View PosttlhIn`toq, on 04 November 2012 - 12:05 AM, said:

Readability is in the eye of the coder. It probably seems less readable to you because its new. LINQ is like that to me because I don't use it much.

StatusPictureBox.Image = boolReady ? GreenLED : RedLED;

This is very readable to me, and conveys the intent of the developer.


Also your first example is not equivalent to your second example.
The second example sets to either Blue or White as needed.
The first example does nothing if false. It only set to blue if True.


I see now. I'm not sure what I was thinking when I hastily wrote that up.. Thanks for the help in understanding, the both of you.
Was This Post Helpful? 0
  • +
  • -

#5 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1012
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Conditional Operator?

Posted 03 November 2012 - 06:15 PM

Something I wrote recently looks like this:
 private int MinNode(int a, int B)/> {
    return b < position ? nodes[a] < nodes[b] ? a : b : a < position ? a : position;
}


Which if written out looks like
private int MinNode(int a, int B)/> {
    if (b < position) {
        if (nodes[a] < nodes[b]) {
            return a;
        } else {
            return b;
        }
    } else {
        if (a < position) {
            return a;
        } else {
            return position;
        }
    }
}

Was This Post Helpful? 1
  • +
  • -

#6 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,499
  • Joined: 29-May 08

Re: Conditional Operator?

Posted 03 November 2012 - 06:31 PM

@Momerath: I'd be extra cautious doing it like that and put in the parenthesis.
As

Quote

The conditional operator is right-associative.


 private int MinNode(int a, int B)/> {
    return (b < position ? (nodes[a] < nodes[b] ? a : B)/> : (a < position ? a : position));
}

This post has been edited by AdamSpeight2008: 03 November 2012 - 06:33 PM

Was This Post Helpful? 4
  • +
  • -

#7 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4577
  • View blog
  • Posts: 8,019
  • Joined: 08-June 10

Re: Conditional Operator?

Posted 05 November 2012 - 09:35 AM

I do love this operator, but I try not to nest it, because it's a little difficult to read at that point. But again, readability is in the eye of the coder.
Was This Post Helpful? 2
  • +
  • -

#8 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,472
  • Joined: 05-May 12

Re: Conditional Operator?

Posted 05 November 2012 - 09:41 AM

View PostCurtis Rutland, on 05 November 2012 - 08:35 AM, said:

I do love this operator, but I try not to nest it, because it's a little difficult to read at that point. But again, readability is in the eye of the coder.

Yes, have somebody really familiar with LISP look at code with a ton of nested parentheses and they go "So what's the problem?" while the rest of us are playing with our mice in our favorite text editors trying match them up. :-)
Was This Post Helpful? 2
  • +
  • -

#9 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4424
  • View blog
  • Posts: 12,293
  • Joined: 18-April 07

Re: Conditional Operator?

Posted 05 November 2012 - 04:10 PM

Yeah the conditional operator rocks for setting defaults values and such on variables. I think it is why I get so many chicks too. I bust that conditional operator out and they are like "Oh martyr, we love youuuu".

Of course I am like "Yeah I know baby, but someday you really will love me for me and not just because I use leet coding lines like conditional operators"

:)
Was This Post Helpful? 2
  • +
  • -

#10 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1112
  • View blog
  • Posts: 4,619
  • Joined: 09-June 09

Re: Conditional Operator?

Posted 05 November 2012 - 04:27 PM

 private int MinNode(int a, int B)/> {
    return b < position ? nodes[a] < nodes[b] ? a : b : a < position ? a : position;
}


That look's disgusting. Readability is not in the "eye of the coder" , it's in the eyes of the "coders" that will be reading your code 1-2 years from now. Most of the time it will be you and you will be kicking yourself for trying to smash all of your code into a single line.
Was This Post Helpful? 0
  • +
  • -

#11 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1012
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Conditional Operator?

Posted 05 November 2012 - 05:01 PM

I disagree as I have no problem reading ?: statements, I've been doing it a long time.

But that was just an example from something I wrote for someone off the top of my head. Readability/Maintainability were not considerations.

The final version of this method (for my library of stuff) looks like this:
private int SelectNode(int n) {
    int m = n + 1;
    if (m < position) {
        return comparer.Compare(heap[n], heap[m]) < 0 ? m : n;
    } else if (n < position) {
        return n;
    } else {
        return position;
    }
}

Was This Post Helpful? 0
  • +
  • -

#12 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,472
  • Joined: 05-May 12

Re: Conditional Operator?

Posted 06 November 2012 - 07:11 AM

And how many out there are rocking the ?? operator? http://msdn.microsof...y/ms173224.aspx
Was This Post Helpful? 0
  • +
  • -

#13 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4577
  • View blog
  • Posts: 8,019
  • Joined: 08-June 10

Re: Conditional Operator?

Posted 06 November 2012 - 07:44 AM

I love using that one, but I rarely have a good chance to do so. Most often when I'm dealing with a bool?, I'll do b ?? false.

Side note, I've had a tutorial about these two operators up for quite some time:

http://www.dreaminco...ser-known-oper/
Was This Post Helpful? 1
  • +
  • -

#14 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,499
  • Joined: 29-May 08

Re: Conditional Operator?

Posted 06 November 2012 - 07:51 AM

vb.net also a Null-Coalescing conditional.
Dim x As Integer? = Nothing
Dim y = If(x,-1)


Was This Post Helpful? 0
  • +
  • -

#15 Julyuary  Icon User is offline

  • D.I.C Head

Reputation: 38
  • View blog
  • Posts: 109
  • Joined: 20-December 11

Re: Conditional Operator?

Posted 06 November 2012 - 08:17 AM

View PostCurtis Rutland, on 06 November 2012 - 02:44 PM, said:

I love using that one, but I rarely have a good chance to do so. Most often when I'm dealing with a bool?, I'll do b ?? false.

Side note, I've had a tutorial about these two operators up for quite some time:

http://www.dreaminco...ser-known-oper/

I'll definitely look at that Curtis Rutland. I can't believe I went so long without knowing about these operators :(
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1