J
jdallen2000
Natt said:... particularly irksome when it comes to issues of code layout style
Let me give my two-cents' worth. I'll start with a question
for the `indent' users.
One almost gets the impression that two guys in adjacent
cubicles run their own customized `indent' whenever sharing
code. If you get a paper printout, do you OCR it and `indent'
the result? I'm not being sarcastic; I just don't understand
the fascinated reliance on `indent.' (Below I mention some
problems `indent' will introduce.)
* * * * * * * * * * * * * * * *
In some ways, treatment of C coding style should be similar
to treatment of English writing style. When the N.Y. Times
hires a new editor, they give him a style guide -- I doubt
if it contains a lot of injunctions against things like
run-on sentences, rather it clarifies whether to leave
double-spaces between sentences, and whether to write
"Mr Jones" or to put a period after Mr.
Thus I found it odd that some suggested indentation and
white-space conventions were not a proper topic for a C style
guide. In English, when two words are separated by a comma,
the comma precedes the space, but the space would precede
a left parenthesis. Someone who insisted on reversing this
would be very irritating. Why should C be any different?
Someone taught me to write better English. "Place commas
inside the close quote," he said, "and don't start sentences
with `But.'" Even though I have a rebellious nature, I happily
adopted the first rule: defying punctuation conventions would
have no purpose. But for blunt emphasis, I still start some
sentences with `But.'
Similarly, in C we always write "if (foo(bar))" without
debating whether "if( foo (bar))" is better a priori.
We might *discourage* the nesting of (x ? y : z) operators
if afraid the code would be maintained by novices, but we
would not *forbid* it.
Because the C language and its programmers are flexible and
forgiving, some people may not realize that C white-space rules
are standardized, but the True Style for Coding C is no secret:
you can look at programming examples from Bill Joy or almost any
of the Great Names. Here's a small fragment, essentially in
True Style, from the Linux kernel:
if (!cap_issubset(inheritable,
cap_combine(target->cap_inheritable,
current->cap_permitted))) {
goto out;
}
In addition to showing the proper placement of braces, this example
is interesting because of the way a long expression is broken
into multiple lines, with spacing used to make a nested parameter
list more readable. (I mightn't do it quite like this, but only
because I mostly stick to TAB's for extended spacing.)
I know almost nothing about the `indent' utility. Can it produce
the spacing above automatically? And suppress it when shorter
names make it unnecessary? I know about `#ifndef lint'; is there
some sort of `#ifndef indent' when one wants to preserve a helpful
white-space arrangement?
Hmmm. When I nest expressions of (x ? y : z), I tend to use
line-breaks and spacing to make the logic easy-to-see.
Maybe that's why the `indent' fans seemed to disparage
such nesting -- in their environment the helpful spacing
will tend to be expunged!
It may seem I'm contradicting myself -- advocating strict
adherence to the True Style, yet condoning multi-line
expressions and other deviations. But a deviation and
its purpose will stand out clearly when the style is
otherwise consistent. Some of you are proud of your
non-True Styles and your consistency within that alien
universe, but this reminds me of a Richard Feynman anecdote.
Feynman invented his own trigonometric notation as a youngster.
He was a great genius and his notation was probably superior,
but he abandoned it as soon as he learned the standard and
realized his formulae were going to be read by others.
* * * * * * * * * * * * * * * *
Well, I'd planned on ranting on and on, but I see the ladies
and gentlemen of the jury are getting tired. As you deliberate,
remember that Simplicity is a premier virtue in programming.
The defendants are accused of making every white-space deviation
a legitimate code-style choice (to paraphrase Rush Limbaugh),
and of requiring me to run `indent' to avoid getting dizzy when
I need to look at their code. Since `indent' will obliterate
any special spacing, as in the above fragment or nested ternarys,
they acknowledge that they don't use spacing creatively.
Unless you find that these perverse ideas are indeed a mode
of Simplicity, you must vote to convict.
James Dow Allen
* * * * * * * * * * * * * * * *