PDA

View Full Version : Not an OGL question but...............................



Andrea Rossini
09-30-2003, 02:05 AM
Sorry I disturb, but this is my favourite forum so.......

I have a colleague who hates this nice fry chip:

(x ? y : z)

it is the ternary operator and I like it to instantaneously solve big if statements.
Why that guy must hate and get pissed about it???

What do you think people??

Mazy
09-30-2003, 03:00 AM
as soon as you think of writing 'OT:' or 'offtopic' or 'Not an OGL question' in the header you really should post it somehere else..

i do know opengl, and im happy if im can help someone.. with opengl. I use delphi, we dont have that syntax so it doenst bother me, but this is just a matter of taste, what do you want as an anwer? a gallup?

Andrea Rossini
09-30-2003, 04:19 AM
Yes... but thinking about it... it's not a good idea.... mmmmmm..... sorry.....

http://www.opengl.org/discussion_boards/ubb/frown.gif http://www.opengl.org/discussion_boards/ubb/smile.gif http://www.opengl.org/discussion_boards/ubb/frown.gif http://www.opengl.org/discussion_boards/ubb/smile.gif

zeckensack
09-30-2003, 06:18 AM
Yes, it is the famous ternary operator. Yes, most people don't like it, because it's somewhat easier to read an explicit if-else statement with proper indentation (particularly so if you're just skimming over code).

There are cases where it can be used nicely.

Eg, instead of


if (whatever)
{
a[2*i*j+k*k-j+20+c][y*w+x]=0;
}
else
{
a[2*i*j+k*k-j+20+c][y*w+x]=1;
}
You could use


a[2*i*j+k*k-j+20+c][y*w+x]=(whatever?0:1);
In the first snippet it's not immediately clear that the left hand sides of the assignment are equal. You need to look carefully. The second version doesn't require any comparison as the left hand side exists only once.
Good for dodging typos.

09-30-2003, 10:47 AM
Yeah, most people who despise it just haven't used it much - it doesn't look like anything they've used before and it just doesn't sit well with them. Best thing to do is just suggest they give it a go for a while and see how they feel about it then. Everyone I've introduced it to has decided it's a very useful construct.

And to bring us back on topic, here's one good use for it:



glClear(GL_COLOR_BUFFER_BIT | (depthTestEnabled ? GL_DEPTH_BUFFER_BIT : 0)) ;

Deiussum
10-01-2003, 05:03 AM
I like that syntax. But then, I also enjoy using regular expressions in Perl... http://www.opengl.org/discussion_boards/ubb/smile.gif