While we're on the issue, I'll say a few things that are obvious to most people. *Always* indent the lowest-level block of a function. Some people don't indent the first level (it is flush-left); not only does it make the code unreadable and functions hard to find, but programs that don't parse C code well (such as ctags(1)) often make errors and think that "while" and "for" are functions.
Also, make sure that no lines wrap around to the next line. This is not only an aesthetic issue, but wrapping causes some text to appear at the beginning of the line, which makes scanning for the beginning and end of functions more difficult.
if (t >= 0.0) { while (!done) { ...; } }I'll (very) occasionally put the block on the same line as the "if", but only if it helps readability:
if (R > 1) { R = 1; } if (G > 1) { G = 1; } if (B > 1) { B = 1; }is clearer than:
if (R > 1) { R = 1; } if (G > 1) { G = 1; } if (B > 1) { B = 1; }Note that I still use braces around the block.
if (a == b || c <= d + 5) ...I make a few exceptions to this rule, especially when it makes the appearance reflect the meaning of the code better, e.g.:
dist = sqrt (x*x + y*y + z*z);which I think looks cleaner and better than:
dist = sqrt (x * x + y * y + z * z);I don't usually put spaces immediately inside parentheses because I find this makes it harder to read:
dist = sqrt ( x*x + y*y + z*z );or
if ( a == b || c <= d + 5 ) ...I also line up variables names in definition sections and structure declarations, e.g.:
int x, y, z; struct blah a, *b; unsigned int i, j;and:
struct point { int x, y, z; float r, g, b; };It's also a good idea to make good use of vertical spacing. Specifically, use one blank line between logical parts of functions and two blank lines between logical parts of source files (e.g., between functions).
/* * This is a comment. It can go on for a while and looks clean. * Even one-line comments should be written this way because they * stand out. If a comment doesn't need to stand out, then it's * probably not worth putting in at all. * * Several paragraphs can go in one comment. */These are easy to format with modern editors like vim. Exceptions are short descriptions of variables or structure elements:
struct node { char *name; /* Name of customer (or NULL) */ int age; /* Age in years */ struct node *left; /* Pointer to left child */ struct node *right; /* Pointer to right child */ };and little reminders:
xratio = yoffset / tanfov; /* Yes, this is yoffset */ yratio = -yoffset / tanfov;Avoid obvious comments:
/* * Return an error. */ return ERROR;Always document your data structures. The program will often explain itself once the data structures are clear.
int main ( int argc, char *argv[]) { ...but I still prefer this more compact form:
int main (int argc, char *argv[]) { ...both because you can cut-and-paste it to the header file and because it reflects more closely the way it will look when you call it. My jury's still out on this one. The former way is better when putting in-line comments for each parameter.