[CLN-list] doc: "Obfuscating operators"

Richard B. Kreckel kreckel at ginac.de
Wed Jan 23 23:13:22 CET 2008


Hi!

Joerg Arndt wrote:
> * Ron Garret <ron at flownet.com> [Jan 23. 2008 14:17]:
>> On Jan 22, 2008, at 3:07 PM, Bruno Haible wrote:
>>> Richard B. Kreckel wrote:
>>>>> For me the shortcut operators
>>>>> as in a+=b are more readable than the long a=a+b form.
>>>> I do admit that I find the shortcuts more readable, too.
>>> I think it depends. When you are summing up a power series,
>>>
>>>   sum += factor/(2*n+1)
>>>
>>> makes perfect sense. But I also have seen things like this:
>>>
>>>   x += x >> 2;
>>>
>>> which really is less readable than
>>>
>>>   x = 1.25 * x;
>> <puzzlement> Don't those two statements do different things? </puzzlement>

x >> 2 is the same as x / 4.

> yes-ish:
> the first should rather be used for integers (as a matter of style).
> 
> If used for floats then
> x += x >> 2;  // x = 1.25 * x;
> shows that comments are useful indeed.

But it is invalid for floats.

> And of course you should use
> x *= 1.25;

For floats? Sure.

For ints it will require the FPU. Besides generating a compiler warning.

> The shortcut versions are definitely nicer for
> the very simple cases as in  a += b;

I agree.

> In the case  x += x>>2;  (assume x integer) one might argue that
> a temporary is avoided to make the code more readable (ignoring
> what the compiler generates).

It is the use of the FPU which is avoided. That makes it attractive.

> Usage as in
>  a += (c *= ++b)>>2
> are of course horrible and only indicate the programmer is a l33t wannabe.

Well said.

I'm beginning to be convinced that it is best to enable these operators 
by default. They can be abused, yes. But C/C++ programmers have become 
so familiar with them that they actually find code using them more readable.

   -richy.
-- 
Richard B. Kreckel
<http://www.ginac.de/~kreckel/>


More information about the CLN-list mailing list