[PATCH v2] devtools: allow variable declaration inside for loop

Morten Brørup mb at smartsharesystems.com
Wed May 3 14:19:30 CEST 2023


> From: Bruce Richardson [mailto:bruce.richardson at intel.com]
> Sent: Wednesday, 3 May 2023 12.57
> 
> On Wed, May 03, 2023 at 11:30:53AM +0100, Ferruh Yigit wrote:
> > Declaring variable inside for loop is not supported via C89 and it was
> > checked in checkpatch.sh via commit [1].
> > But as DPDK supported C standard is becoming C99/C11 [2], declaring
> > variable inside loop can be allowed.
> >
> > [1]
> > Commit 43e73483a4b8 ("devtools: forbid variable declaration inside
> for")
> >
> > [2]
> > https://dpdk.org/patch/121912
> >
> > Signed-off-by: Ferruh Yigit <ferruh.yigit at amd.com>
> > ---
> > Cc: Bruce Richardson <bruce.richardson at intel.com>
> > Cc: David Marchand <david.marchand at redhat.com>
> >
> > v2:
> >  * Update coding convention too
> > ---
> 
> Acked-by: Bruce Richardson <bruce.richardson at intel.com>

Acked-by: Morten Brørup <mb at smartsharesystems.com>

[...]

> > @@ -558,6 +558,7 @@ Local Variables
> >
> >  * Variables should be declared at the start of a block of code rather
> than in the middle.
> 
> I'd love to see this restriction removed in future too. Having a
> variable
> declared on first use in the middle of block I find a far easier way of
> working as a) it saves scrolling to look for variable definitions and b)
> it
> makes it far easier when adding/removing blocks of code e.g. commenting
> out
> for testing,  to have all the code together rather than having variables
> at
> the top to add/remove also.

And c) Initializing the variables close to where they are used the first time reduces the risk of initializing them incorrectly. Especially when modifying a block of code, initialization of its variables might be missed if out of sight. (Although this is probably a consequence of "a)".)

I consider it old style to only declare variables at the start of a block of code, and this style of coding should be considered obsolete.

If you are really old (like me?), you might remember when function parameters were provided like this:

int main(argc, argv)
int argc;
char *argv[];
{
	return(0);
}

We have moved on from that to a more modern coding style a long time ago. We should also move on to a more modern coding style regarding variable declarations.

> 
> >    The exception to this is when the variable is ``const`` in which
> case the declaration must be at the point of first use/assignment.
> > +  Declaring variable inside a for loop is OK.
> >  * When declaring variables in functions, multiple variables per line
> are OK.
> >    However, if multiple declarations would cause the line to exceed a
> reasonable line length, begin a new set of declarations on the next line
> rather than using a line continuation.
> >  * Be careful to not obfuscate the code by initializing variables in
> the declarations, only the last variable on a line should be
> initialized.
> > --
> > 2.34.1
> >


More information about the dev mailing list