From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id A09B942A4F; Wed, 3 May 2023 14:19:39 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 2C2BC4114B; Wed, 3 May 2023 14:19:39 +0200 (CEST) Received: from dkmailrelay1.smartsharesystems.com (smartserver.smartsharesystems.com [77.243.40.215]) by mails.dpdk.org (Postfix) with ESMTP id 706E341144 for ; Wed, 3 May 2023 14:19:37 +0200 (CEST) Received: from smartserver.smartsharesystems.com (smartserver.smartsharesys.local [192.168.4.10]) by dkmailrelay1.smartsharesystems.com (Postfix) with ESMTP id 6DAF32032C; Wed, 3 May 2023 14:19:34 +0200 (CEST) Content-class: urn:content-classes:message MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Subject: RE: [PATCH v2] devtools: allow variable declaration inside for loop X-MimeOLE: Produced By Microsoft Exchange V6.5 Date: Wed, 3 May 2023 14:19:30 +0200 Message-ID: <98CBD80474FA8B44BF855DF32C47DC35D878DC@smartserver.smartshare.dk> In-Reply-To: X-MS-Has-Attach: X-MS-TNEF-Correlator: Thread-Topic: [PATCH v2] devtools: allow variable declaration inside for loop Thread-Index: Adl9rg28y7ZJ0MVnS8aFzLatxVy4LAAB7K2w References: <20230503095018.1710769-1-ferruh.yigit@amd.com> <20230503103053.3087445-1-ferruh.yigit@amd.com> From: =?iso-8859-1?Q?Morten_Br=F8rup?= To: "Bruce Richardson" , "Ferruh Yigit" Cc: "Thomas Monjalon" , , "David Marchand" X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org > From: Bruce Richardson [mailto:bruce.richardson@intel.com] > Sent: Wednesday, 3 May 2023 12.57 >=20 > 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 > > --- > > Cc: Bruce Richardson > > Cc: David Marchand > > > > v2: > > * Update coding convention too > > --- >=20 > Acked-by: Bruce Richardson Acked-by: Morten Br=F8rup [...] > > @@ -558,6 +558,7 @@ Local Variables > > > > * Variables should be declared at the start of a block of code = rather > than in the middle. >=20 > 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. >=20 > > 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 > >