Bruno Haible via Gnulib discussion list <[email protected]> writes:
> In November 2025, I wrote:
>> - mix declarations and statements, so that variable declarations and
>> initializations get closer together,
>> - reduce the scope of local variables,
>
> This patch documents the preferred coding style w.r.t. local variables.
>
> Find attached also a draft of the changes, throughout the lib/ directory
> (excluding the 'regex' module, which I don't want to touch).
I don't blame you for not wanting to touch regex.
Maybe it is best to leave out all the files shared with glibc? I noticed
your patch changed argz, argp, glob, getopt, and probably some others I
didn't notice. It would be nice to get them in-sync again, which would
be a bit more difficult with this change.
> The changes are a mix of these:
> - Move variable declaration to its initialization.
> - Reduce scope.
> - Split scope, create separate variables from one variable.
> - Insert braces, creating a block scope.
> - Change order of initializations.
>
> I plan to review them and then commit them in a few days.
I skimmed some of the earlier changes in the patch and they looked good.
> +* Note: You can't put a label before a declaration, such as in
> + retry:
> + int foo = ...;
> + This is supported only in C23 or newer. Instead, add an empty statement:
> + retry: ;
> + int foo = ...;
> + Similarly, you can't put a declaration immediately after a case label, such
> + as in
> + case 1:
> + int foo = ...;
> + ...
> + break;
> + Again, this is supported only in C23 or newer. Instead, use a sub-block:
> + case 1:
> + {
> + int foo = ...;
> + ...
> + }
> + break;
+1, definitely worth mentioning. I think I have accidental committed the
declaration after label syntax a few times, because it is not obvious.
The rest of the document looks good to me too.
Thanks,
Collin