Ok, like I promised in another message, I will ask some questions
Here's the if code which only checks iFooOK[] array index 0 and 1 and set's variable iBar accordingly to index so if index 0 equals BAR_OK then I get index 0, else if index 1 equals BAR_OK then I get 1, if both are not _OK then -1.
Code:
if( BAR_OK == iFooOK[ 0 ] )
{
iBar = 0;
}
elseif( BAR_OK == iFooOK[ 1 ] )
{
iBar = 1;
}
else
{
iBar = -1;
}
I thought that's taking little too much space and squeezing space "misplacing" block marks didnt serve my appetite, I thought to try out " ? : "
So, here's the result.
Code:
iBar = (iFooOK[ 0 ] == BAR_OK ? iBar = 0 :
(iFooOK[ 1 ] == BAR_OK ? iBar = 1 : -1));
The fact is, readability of this shorter version is close to zero (though, I can write one line of comment to fully describe the intent of this piece of code)
So, I am again asking pro's to nudge me into right direction if there's a better way for this particular case.
Especially if in the far future I find myself in situation where there is not only 2 items in this array ("How could it be? I specified it's going to be only 2 forever and all of sudden there needs to be 3!!").
In that case, should I just #define array size:
Code:
#define MAX_BARS 3
iBar = -1;
for( i = 0; i < MAX_BARS; i++ )
{
if( BAR_OK == iFooOK[i] )
{
iBar = i;
break;
}
}
Now, ok.. for() aint that cool compared to ? : conditional operator. But at this point, I am thinking to just trash this post and move on with my coding and use for(). Everything is bound to change in future and are bound to break boundaries.
But I post this anyway (to increase my post count... NONO! I mean, to show an example.)
And a hope of an opinion