From: "Morten Brørup" <mb@smartsharesystems.com>
To: "Konstantin Ananyev" <konstantin.ananyev@huawei.com>,
"Bruce Richardson" <bruce.richardson@intel.com>,
"David Marchand" <david.marchand@redhat.com>
Cc: <dev@dpdk.org>, <bluca@debian.org>, <stable@dpdk.org>,
"Konstantin Ananyev" <konstantin.v.ananyev@yandex.ru>,
"David Christensen" <drc@linux.ibm.com>,
"Wathsala Vithanage" <wathsala.vithanage@arm.com>
Subject: RE: [PATCH] acl: fix build with GCC 15 on aarch64
Date: Thu, 27 Mar 2025 13:30:14 +0100 [thread overview]
Message-ID: <98CBD80474FA8B44BF855DF32C47DC35E9FB7C@smartserver.smartshare.dk> (raw)
In-Reply-To: <cc6776a7e8ad4f519c66d0b204b30f08@huawei.com>
> From: Konstantin Ananyev [mailto:konstantin.ananyev@huawei.com]
> Sent: Thursday, 27 March 2025 13.10
>
> > > >
> > > > > From: David Marchand <david.marchand@redhat.com>
> > > > > Sent: Thursday, March 27, 2025 10:37 AM
> > > > >
> > > > > On Thu, Mar 27, 2025 at 9:55 AM Bruce Richardson
> > > > > <bruce.richardson@intel.com> wrote:
> > > > > >
> > > > > > On Wed, Mar 26, 2025 at 11:39:28AM +0100, David Marchand
> wrote:
> > > > > > > @@ -176,6 +176,8 @@ acl_set_flow(struct acl_flow_data
> *flows,
> > > struct completion *cmplt,
> > > > > > > uint32_t cmplt_size, const uint8_t **data, uint32_t
> > > *results,
> > > > > > > uint32_t data_num, uint32_t categories, const
> uint64_t
> > > *trans)
> > > > > > > {
> > > > > > > + unsigned int i;
> > > > > > > +
> > > > > > > flows->num_packets = 0;
> > > > > > > flows->started = 0;
> > > > > > > flows->trie = 0;
> > > > > > > @@ -187,6 +189,9 @@ acl_set_flow(struct acl_flow_data
> *flows,
> > > struct completion *cmplt,
> > > > > > > flows->data = data;
> > > > > > > flows->results = results;
> > > > > > > flows->trans = trans;
> > > > > > > +
> > > > > > > + for (i = 0; i < cmplt_size; i++)
> > > > > > > + cmplt[i].count = 0;
> > > > > > > }
> > > > > >
> > > > > > Minor nit, but since we are using c11 standard, is it not
> better
> > > to declare
> > > > > > "i" inside the "for" statement. Keeps diffs simpler for
> > > adding/removing
> > > > > > code, I think.
> > > > >
> > > > > I still have this (bad) habit but yes, it looks nicer with
> > > declaring
> > > > > in for() itself.
> > > >
> > > > My vote would be to keep it in an old fashioned way.
> > > > Nothing is wrong in defining variable to use at the start of the
> > > function :)
> > > >
> > >
> > > No, there isn't. However, there is also a reason why later GCC
> > > revisions
> > > and modern languages allow use of a temporary variable defined
> within
> > > the
> > > loop itself. Cognitively, it's easier to have variables defined at
> > > point of
> > > use, as it saves the user having to mentally track them or move up
> and
> > > down the
> > > code. Furthermore, when debugging or reworking the code, it's far
> > > easier to
> > > have the variable inside the "for" statement as it means that as we
> > > comment/uncomment, or remove/re-add, the code block, the variable
> > > definition
> > > also gets commented/uncommented too, without having to constantly
> > > scroll up
> > > to make changes in two places. Lastly, it makes for smaller git
> diffs
> > > too.
>
> I understand that it is probably more convenient, though from my
> perspective it is also more error prone.
> I saw several times people unintentionally defined new variable (in a
> local scope) with the same name
> that was already used in an outer scope, especially when function
> becomes large and clunky.
Yes, it used to be a problem, causing bugs that were difficult to find by the developer who wrote the code.
But modern compilers warn about that, so not a problem anymore.
> Personally, I think it is a good practice to do a 'mental track' of
> your variables when writing the code,
> and having all of them defined in one place definitely helps with that.
I partially agree with this. Variables declared in the middle of a code block are difficult to keep mental track of.
However, minimizing the scope of variables reduces the mental load when reviewing code.
If some variable is only used within a block of code, it should not be declared outside that block.
As always, there are exceptions to all rules. E.g. a local variable used at the end of a code block, e.g. as a helper for temporarily cleaning up, is OK to declare there.
> Anyway, I am not about to stop people to define variables inside the
> for() if it is more convenient for them,
> but I am against to force people to write code that way.
Coding style is a balance between readability/reviewability, bug prevention and preferences.
And with our current coding style, either way of declaring "i" in this case should be acceptable.
>
> > >
> > > /Bruce
> >
> > I agree with Bruce.
> > Also, minimizing the scope of local variables reduces the risk of
> bugs caused by unintended reuse without re-initialization. Reducing
> > the risk of bugs is important.
> >
> > BTW: The Coding Style documentation [CODINGSTYLE] is still based on
> an ancient C version, and needs to be updated.
> >
> > [CODINGSTYLE]:
> https://doc.dpdk.org/guides/contributing/coding_style.html#local-
> variables
next prev parent reply other threads:[~2025-03-27 12:30 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-03-26 10:39 David Marchand
2025-03-27 8:17 ` David Marchand
2025-03-27 8:55 ` Bruce Richardson
2025-03-27 10:36 ` David Marchand
2025-03-27 10:39 ` Konstantin Ananyev
2025-03-27 10:51 ` Bruce Richardson
2025-03-27 11:17 ` Morten Brørup
2025-03-27 12:10 ` Konstantin Ananyev
2025-03-27 12:24 ` Bruce Richardson
2025-03-27 12:43 ` Konstantin Ananyev
2025-03-27 12:30 ` Morten Brørup [this message]
2025-03-27 10:30 ` Konstantin Ananyev
2025-03-27 18:06 ` Bruce Richardson
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=98CBD80474FA8B44BF855DF32C47DC35E9FB7C@smartserver.smartshare.dk \
--to=mb@smartsharesystems.com \
--cc=bluca@debian.org \
--cc=bruce.richardson@intel.com \
--cc=david.marchand@redhat.com \
--cc=dev@dpdk.org \
--cc=drc@linux.ibm.com \
--cc=konstantin.ananyev@huawei.com \
--cc=konstantin.v.ananyev@yandex.ru \
--cc=stable@dpdk.org \
--cc=wathsala.vithanage@arm.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).