TheMemberFormerlyKnownAsAWOL:
I dislike the comma operator.
Yes, I wasn't very interested in using this conditional operator and it's not so widely used but I thought to try it and improve my code.
But practically, is it the same as if statement ?
TheMemberFormerlyKnownAsAWOL:
Did you notice any difference in object code size?
Thanks for noting me to check that.
Well, yes I save 20 bytes with using the ternary operator. So now what's the deal, is it a good approach ? especially in this part of code where I have only two parts to execute.
For readability, I think the first version is better.
For space, the ternary is I guess better.
What's your opinion ?
Saving 20 bytes (I am surprised it saved even that) is not worth the downsides. Your code is now more difficult to read, understand, and debug, as this topic has demonstrated perfectly. It's not improved! Generally, shorter code is better, but as with so many things in life, you can take things too far. Today, you found that point, and learned something
Juraj:
the ternary operator should be used to calculate a value. a = (b) ? c : d, not as a replacement of if
Yes, in my case I have to check if (y < 64) if true execute part A else part B.
Is this ok that to execute multiple lines or it has to be like one line for part A and B ?
(y<32) ? (data_buf[0] = 0x80+y, data_buf[1] = 0x80+x) // part A
: (data_buf[0] = 0x80+y-32, data_buf[1] = 0x88+x); // part B
PaulRB:
Saving 20 bytes (I am surprised it saved even that) is not worth the downsides. Your code is now more difficult to read, understand, and debug
Yes, with the if statement it's better arranged and clear.
but as with so many things in life, you can take things too far.
you mean by taking things too far is by investigating something that is rather not so useful to spend time learning the differences of using ? like investigating the use of ternary operator ?
Today, you found that point, and learned something
what point you mean exactly ? I'm still not sure of the differences between using if or ternary operator because both work the same in this case.
but as you all cleared that it's not a clever thing to do, then I'm going back to my if version:
you mean by taking things too far is by investigating something that is rather not so useful to spend time learning the differences of using ? like investigating the use of ternary operator ?
Time spend learning and investigating is rarely wasted. Time spent making code shorter, simpler and easier to read is rarely wasted. Time spent crushing your code down so far that is becomes more complex and difficult to read is also not wasted, provided that you realise you have gone too far and back up to find that point of balance. That's what you have done, so congratulations. As experience grows you learn to recognise that point before you go past it, seeing it in your head before you type it.
Your "if" version is better than the ternary operator version, but there is still room for improvement I think. For example, what gets assigned to array indexes 2 & 3 is the same in each case, so that duplication could be removed.
What often gets called "the ternary operator" is simply a conditional expression.
(technically its a 'distfix' operator, "ternary operator" is unique to the C/C++ language definition
and not used elsewhere)
Use the conditional statement for statements, and the conditional expression for expressions.
Don't over-use side-effects in expressions, its confusing and poor style.
What you should have done is this, which correctly uses a conditional expression for the RHS: