C style
Guy Harris
guy at sun.uucp
Fri Oct 4 16:40:39 AEST 1985
> > For Heaven's sake, the big problem with this code is that the conditional
> > is quite unreadable in in-line code like this. I'd prefer
> >
> > while (illegal (ch = getch()))
> >
> > (where "illegal" is defined as a function testing its character
> > argument).
>
> It may enhance readability, but it's not worthwhile. You've just managed
> to add a context switch per *character*. Now, imagine what that's going to
> do to a program like spell.
Yes. The System V "fgrep" uses a subroutine to do character comparisons.
The 4.2BSD version uses a macro instead. The 4.2BSD version is
substantially faster.
The function "illegal" in the above example could have been defined as a
macro. The code calling it would, of course, would have to be rewritten as
for (;;) {
ch = getch();
if (ILLEGAL(ch))
break;
or somesuch, since the macro uses its argument several times, and thus the
argument can't have side effects. Mesa, C++, and other languages have the
notion of an "inline function"; when such a function is called, the compiler
doesn't generate a procedure call, but generates code that acts as if the
procedure in question had been called. In the above example, the code as
written ("while (illegal(ch = getch))") would have compiled into code
equivalent to the macro version.
Guy Harris
More information about the Comp.lang.c
mailing list