On Mon, Jan 26, 2026 at 10:38:06AM +0800, Kevin J. McCarthy wrote:
> About 7 years ago, I did some basic code style cleanup.
> You'll notice in the second commit I declined at that time to do anything
> about the tabs vs spaces indentation split.
My grain-of-salt suggestions:
- in a rare confluence, I agree with everything Vincent said. =8^)
- many, many developers (myself included, but also almost everyone
I've worked with) find 2 spaces inadequate for indentation, most
especially (but not exclusively) in longer functions with more
than 2 or 3 levels of indentation. If you're not one of those,
bully for you, but don't make the rest of us suffer. Please, use
a minimum of 4.
- In that same vein, more K&R, less GNU. FWIW I think the perfect
style is W. Richard Stevens, Advanced Programming in the Unix
Environment. (He uses ts=8 but it only really works if you're
VERY conscientious about writing small, well-factored functions or
aren't averse to very long lines.) GNU is an abomination, which
is why you pretty much never see it outside GNU projects or
projects of MIT grads... Pretty much never in books.
- sizeof IS NOT A FUNCTION[*]. Do not format it like one, i.e. use:
✓ sizeof varname // no, doesn't need parens
✓ sizeof (type name) // parens needed, but not a function call!
✓ struct foo *x; size_t s = sizeof *x; // doesn't need parens or to be
initialized/non-null!
Ideally, an indent rule file would be used, and published, with the
desired formatting rules. From that people could work up their own
editor configs, and some nice folks could share theirs on the list.
-=-=-=-=-=-
* OK, the sizeof thing is a pet peeve of mine; I've found many
C/C++ programmers don't know that it's an operator rather than a
function, and people coding it with formatting like a function just
spreads the disease, leading to it often being used inefficiently
(waste of typing, at least). Sadly, there's probably no way to write
an indent rule for that, unless indent is a lot better than I remember
it being... =8^(
And yes, the third example I provided DOES WORK. For the incredulous:
$ cat sizeof.c
#include <stdio.h>
struct foo {
int i;
char *foop;
};
int main(void)
{
struct foo *a = NULL;
printf("Size of *a is %zu\n", sizeof *a);
return 0;
}
$ gcc -Wall -Werror -o sizeof sizeof.c
$ ./sizeof
Size of *a is 16
It's perfectly valid code--doesn't even generate warnings. Weeeee!
--
Derek D. Martin http://www.pizzashack.org/ GPG Key ID: 0xDFBEAD02
-=-=-=-=-
This message is posted from an invalid address. Replying to it will result in
undeliverable mail due to spam prevention. Sorry for the inconvenience.