S
spibou
What's your opinion on something like "x += x < MAX_VALUE" where
x is an integral type ? Elegant or ugly and incomprehensible ?
x is an integral type ? Elegant or ugly and incomprehensible ?
What's your opinion on something like "x += x < MAX_VALUE" where
x is an integral type ? Elegant or ugly and incomprehensible ?
Mike said:Both ;-)
IMHO the "correctness" of a particular coding style can't be judged
objectively; there will always be personal bias as to which way is The
Right Way.
That being said, I'm not sure I like the specific example you gave.
It's mildly clever, but I would prefer to see something like
if (x < MAX_VALUE) ++x;
[snip]
[snip]It's mildly clever, but I would prefer to see something like
if (x < MAX_VALUE) ++x;
Personally I'd use either one depending among other factors on the
whim of the moment. And I have to say that I don't use it for
reasons of cleverness. I have simply developed a taste for
expressions of this sort where the numerical value of a logical
expression is used for numerical computations since my early BASIC
days because I read on some magazine back then that the interpreter
is likely to execute these faster than using if.
What's your opinion on something like "x += x < MAX_VALUE" where
x is an integral type ? Elegant or ugly and incomprehensible ?
What's your opinion on something like "x += x < MAX_VALUE" where
x is an integral type ? Elegant or ugly and incomprehensible ?
lovecreatesbeauty said:It's ok. The expression is same as:
y = x < MAX_VALUE;
x += y;
The operator '<' takes precedence over '=' in evaluation. I think
it's good to make the equality clear and nature. But something like
"x++ += x++ < MAX_VALUE" is a rather bad idea.
What's your opinion on something like "x += x < MAX_VALUE" where
x is an integral type ? Elegant or ugly and incomprehensible ?
Keith said:It's not just a bad idea, it's illegal (more precisely, it's a
constraint violation). "x++" does not yield an lvalue, so it can't
appear on the left side of an assignment.
(Even if that weren't the
case, of course, modifying x twice between sequence points would
invoke undefined behavior.)
What's your opinion on something like "x += x < MAX_VALUE" where
x is an integral type ? Elegant or ugly and incomprehensible ?
Malcolm said:Compileable gibberish.
At very worst make it x += (x < MAX_VALUE) ? 1 : 0;
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.