C Indentation Survey Results (long...)
Mark Brader
msb at lsuc.UUCP
Sun Apr 28 14:13:40 AEST 1985
There've been a couple of suggestions lately like this one from
mwm at ucbtopaz.UUCP (Praiser of Bob (!)):
> Whenever I read/write code, I prefer it in a format that I find easy to
> read. The AI community found the solution to the problem of different
> code formatting styles two (or more?) decades ago: customizable pretty
> printers (beautifiers, if you must). You read your code into the editor,
> notice that the format is "ugly", and tell the editor to format it the
> way you like it. The next person to come along goes through the same
> process.
The trouble with this is that prettyprinters don't know what you know.
Consider these two examples:
if(isdigit(c)) A(); else if(isupper(c)) B(); else if(islower(c)) C(); else D();
if (snowing) A(); else if (political) B(); else if (Saturday) C(); else D();
Of course, A(), B(), etc. stand for several lines of code in each case.
The first one is a case-type construct consisting of multiple tests of
the same variable. The second one, on the other hand, consists of a series
of completely independent tests and bears only a superficial resemblance to
the first. The second one might as well have been written:
if (!snowing) if (!political) if (!Saturday) D(); else C(); else B(); else A();
... except that this is less readable in most cases.
The distinction between the two constructs is often expressed by writing
them like this:
if (isdigit(c)) if (snowing)
A(); A();
else if (isupper(c)) else
B(); if (political)
else if (islower(c)) B();
C(); else
else if (Saturday)
D(); C();
else
D();
Now show me a prettyprinter that can tell these apart.
Remember, it has to be able to start from the compressed form I gave originally!
Then there are things like putting comments to the right of code or above it,
and whether */ /* indicates two comments or end-of-line inside a comment,
and carefully aligned lines to illustrate parallel operations....
prettyprinting just doesn't do it all.
Really, the only solution is to make your code look like that of the
people you work with.... or start your own company and make it a trade secret.
Mark Brader
More information about the Comp.lang.c
mailing list