[Klug-general] Obscure C at saturday's meeting
Mike Evans
mike at tandem.f9.co.uk
Mon Nov 29 12:44:51 UTC 2010
And for exactly this reason there has been a policy in places I have
worked of running any C code through the C beautifier automatically as
part of the version control check-in process. This has the advantage
that coders arbitrarily rearranging the code in their preferred style
whilst editing does not look like a change, and that all code retrieved
is always in the house style, which makes for easier debugging.
Incidentally the usual preference was _always_ to put the contents of
the for loop in curly braces on the basis that 1) it makes it absolutely
clear and 2) you might start off thinking you only want to do one thing
in the loop, but you invariably end up adding something else so you
might as well start off with the braces :)
Additionally, the use of a function with possible side effects in the
third clause of the for-loop would have earned you a public spanking at
code review stage. The approved coding of this segment would therefore be:
{
for (i = 0; i < 10; i++) {
stuff(i);
}
more_stuff();
}
or:
{
for (i = 0; i < 10; i++) {
more_stuff();
stuff(i);
}
}
depending on which functionality was the original intention.
I think it was Kernighan who said something along the lines of
"Debugging is at least twice as hard as writing the code in the first
place. Therefore if you write the smartest code you can you are, by
definition, incapable of debugging it."
More information about the Kent
mailing list