Re: Malcolm's new book - Chapter 1 review



Malcolm McLean wrote, On 08/08/07 23:01:

"Keith Thompson" <kst-u@xxxxxxx> wrote in message news:lnodhh1z8j.fsf@xxxxxxxxxxxxxxxxxx
Mark McIntyre <markmcintyre@xxxxxxxxxxx> writes:
On Tue, 07 Aug 2007 16:26:52 -0700, in comp.lang.c , Keith Thompson
<kst-u@xxxxxxx> wrote:
I haven't looked at Malcolm's 'clamp' macro, but I understand that it
does evaluate some of its arguments more than once. It's for exactly
that reason that it should be named 'CLAMP' rather than 'clamp'.

If it evaluates its arguments more than once, it probably shouldn't be
a macro at all.

Perhaps so, but since it is a macro, its name should be 'CLAMP' rather
than 'clamp'. Perhaps there's an efficiency concern, and Malcolm
didn't want to assume the availability of inline functions.

Efficiency is one issue. Operations like clamping are so simple and so common that function overhead can be a real concern.

I thought your book was an algorithms book not a micro-optimisation book?

The other problem is that C has no way of taking a generic scalar. Since data of all types except maybe char can be clamped, there is no obvious type for it to take as a function. With 14 separate integer types and still counting, plus three real formats, the num ber of variants you would need is too large.

That is a potentially valid reason.

As for lower case, that is partly aesthetics,

Not, in my opinion, a good reason for going against long established conventions.

> partly saying "this is
really just another function".

Ah, but it is not!

> Of course the design of the macro
processor means that x = clamp(*ptr++, 0, 10); will compile but not work, which is a real nuisance,

Which is why you should make it clear by using upper case as is conventionally done.

> but then I discourage over-terseness
anyway.

I thought you were writing this stuff for other people? Remember that others might not share your opinion of how terse things should be.

> However the macro is not written to be called as
clamp(x, 0, 10);
with a side-effect,

Which is why it should be made crystal clear, anf the conventional method for this is to use upper case.

> even though most of the time you want to assign to
the variable you are clamping.
I also use uniform() and coin(), which have decorative parentheses purely to make them look like functions. Except that arguably it makes it easier to replace them with real functions if required. uniform() - generating a random number on 0-1 - in particular you might want to rewrite with a call to a better randomiser.

Personally I would say that in a book about algorithms using functions is better than using macros as you then don't have to deal with irrelevancies to your core material like pointing out the need to avoid side effects.

I would not reject your book for using lower case names for macros, although in my opinion it is a valid criticism. A lot of the other criticisms are far better reasons for rejecting it.
--
Flash Gordon
.



Relevant Pages