git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Johannes Schindelin <Johannes.Schindelin@gmx.de>
To: Patrick Steinhardt <ps@pks.im>
Cc: Johannes Schindelin via GitGitGadget <gitgitgadget@gmail.com>,
	 git@vger.kernel.org, Philip Oakley <philipoakley@iee.email>,
	 Phillip Wood <phillip.wood123@gmail.com>,
	 Karthik Nayak <karthik.188@gmail.com>, Jeff King <peff@peff.net>
Subject: Re: [PATCH v2 06/10] diff-delta: explicitly mark intentional use of the comma operator
Date: Wed, 26 Mar 2025 08:20:32 +0100 (CET)	[thread overview]
Message-ID: <050f5d65-32eb-fd26-1461-579b06018dc6@gmx.de> (raw)
In-Reply-To: <Z-OWoK-DlvnxXPkO@pks.im>

Hi Patrick,

On Wed, 26 Mar 2025, Patrick Steinhardt wrote:

> On Tue, Mar 25, 2025 at 11:32:10PM +0000, Johannes Schindelin via GitGitGadget wrote:
> > From: Johannes Schindelin <johannes.schindelin@gmx.de>
> >
> > The comma operator is a somewhat obscure C feature that is often used by
> > mistake and can even cause unintentional code flow. That is why the
> > `-Wcomma` option of clang was introduced: To identify unintentional uses
> > of the comma operator.
> >
> > Intentional uses include situations where one wants to avoid curly
> > brackets around multiple statements that need to be guarded by a
> > condition. This is the case here, as the repetitive nature of the
> > statements is easier to see for a human reader this way.
> >
> > To mark this usage as intentional, the return value of the statement
> > before the comma needs to be cast to `void`, which we do here.
> >
> > Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
> > ---
> >  diff-delta.c | 12 ++++++------
> >  1 file changed, 6 insertions(+), 6 deletions(-)
> >
> > diff --git a/diff-delta.c b/diff-delta.c
> > index a4faf73829b..a03ba10b2be 100644
> > --- a/diff-delta.c
> > +++ b/diff-delta.c
> > @@ -439,18 +439,18 @@ create_delta(const struct delta_index *index,
> >  			i = 0x80;
> >
> >  			if (moff & 0x000000ff)
> > -				out[outpos++] = moff >> 0,  i |= 0x01;
> > +				(void)(out[outpos++] = moff >> 0),  i |= 0x01;
> >  			if (moff & 0x0000ff00)
> > -				out[outpos++] = moff >> 8,  i |= 0x02;
> > +				(void)(out[outpos++] = moff >> 8),  i |= 0x02;
> >  			if (moff & 0x00ff0000)
> > -				out[outpos++] = moff >> 16, i |= 0x04;
> > +				(void)(out[outpos++] = moff >> 16), i |= 0x04;
> >  			if (moff & 0xff000000)
> > -				out[outpos++] = moff >> 24, i |= 0x08;
> > +				(void)(out[outpos++] = moff >> 24), i |= 0x08;
> >
> >  			if (msize & 0x00ff)
> > -				out[outpos++] = msize >> 0, i |= 0x10;
> > +				(void)(out[outpos++] = msize >> 0), i |= 0x10;
> >  			if (msize & 0xff00)
> > -				out[outpos++] = msize >> 8, i |= 0x20;
> > +				(void)(out[outpos++] = msize >> 8), i |= 0x20;
>
> Hm. I think the end result is even more confusing than before. Why don't
> we introduce curly braces here, same as in preceding commits?

The interleaved -/+ lines make it admittedly hard to see what I meant.
I'll unwind it a bit (presenting only the `moff` part, the same
consideration applies to `msize`):

		if (moff & 0x000000ff)
			(void)(out[outpos++] = moff >> 0),  i |= 0x01;
		if (moff & 0x0000ff00)
			(void)(out[outpos++] = moff >> 8),  i |= 0x02;
		if (moff & 0x00ff0000)
			(void)(out[outpos++] = moff >> 16), i |= 0x04;
		if (moff & 0xff000000)
			(void)(out[outpos++] = moff >> 24), i |= 0x08;

In this form, it is very obvious (from comparing the right-side half of
the lines) that a shifted version of `moff` is appended to `out` and `i`
gets a bit set, and the correlation between shift width and the set bit
is relatively easy to see and validate (at least my brain has an easy time
here, thanks to the alignment and thanks to visual similarity between the
non-blank parts).

It is admittedly quite a bit harder not to be distracted by the repetitive
`(void)(out[...` parts to understand and validate the `if` conditions on
the left-hand side, but thanks to those repetitive parts being identical,
and being only one line between those `if` lines, I can bring my brain to
focus only on the differences of the bitmask and understand and verify
them with relatively little effort.

When I compared this form to the following, the cognitive load to wrap my
head around the code is quite a bit higher there:

		if (moff & 0x000000ff) {
			out[outpos++] = moff >> 0;
			i |= 0x01;
		}
		if (moff & 0x0000ff00) {
			out[outpos++] = moff >> 8;
			i |= 0x02;
		}
		if (moff & 0x00ff0000) {
			out[outpos++] = moff >> 16;
			i |= 0x04;
		}
		if (moff & 0xff000000) {
			out[outpos++] = moff >> 24;
			i |= 0x08;
		}

The reason is the visual distance between the near-identical code.

Having said that, I do realize that my brain quite possibly works in
special ways here and that the general preference is to go with the latter
form.

Do you have a strong opinion which form to use?

Ciao,
Johannes

  reply	other threads:[~2025-03-26  7:20 UTC|newest]

Thread overview: 64+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-03-25  8:01 [PATCH 0/2] Avoid the comma operator Johannes Schindelin via GitGitGadget
2025-03-25  8:01 ` [PATCH 1/2] remote-curl: avoid using the comma operator unnecessarily Johannes Schindelin via GitGitGadget
2025-03-25 16:28   ` Phillip Wood
2025-03-25 16:55     ` Jeff King
2025-03-25  8:01 ` [PATCH 2/2] rebase: " Johannes Schindelin via GitGitGadget
2025-03-25 14:35   ` Phillip Wood
2025-03-25 11:41 ` [PATCH 0/2] Avoid the comma operator Philip Oakley
2025-03-25 14:12   ` Johannes Schindelin
2025-03-26 17:29     ` Taylor Blau
2025-03-25 12:22 ` Patrick Steinhardt
2025-03-25 14:13   ` Johannes Schindelin
2025-03-26 20:17     ` Taylor Blau
2025-03-25 14:37   ` Karthik Nayak
2025-03-25 19:34 ` Junio C Hamano
2025-03-25 23:32 ` [PATCH v2 00/10] " Johannes Schindelin via GitGitGadget
2025-03-25 23:32   ` [PATCH v2 01/10] remote-curl: avoid using the comma operator unnecessarily Johannes Schindelin via GitGitGadget
2025-03-25 23:32   ` [PATCH v2 02/10] rebase: " Johannes Schindelin via GitGitGadget
2025-03-25 23:32   ` [PATCH v2 03/10] kwset: " Johannes Schindelin via GitGitGadget
2025-03-25 23:32   ` [PATCH v2 04/10] clar: " Johannes Schindelin via GitGitGadget
2025-03-26  5:54     ` Patrick Steinhardt
2025-03-26  7:03       ` Johannes Schindelin
2025-03-25 23:32   ` [PATCH v2 05/10] xdiff: " Johannes Schindelin via GitGitGadget
2025-03-25 23:32   ` [PATCH v2 06/10] diff-delta: explicitly mark intentional use of the comma operator Johannes Schindelin via GitGitGadget
2025-03-26  5:54     ` Patrick Steinhardt
2025-03-26  7:20       ` Johannes Schindelin [this message]
2025-03-26 10:17         ` Phillip Wood
2025-03-26 20:33           ` Taylor Blau
2025-03-27  1:31             ` Chris Torek
2025-03-25 23:32   ` [PATCH v2 07/10] wildmatch: " Johannes Schindelin via GitGitGadget
2025-03-26  5:54     ` Patrick Steinhardt
2025-03-26  7:46       ` Johannes Schindelin
2025-03-26  7:49     ` Junio C Hamano
2025-03-26 10:14     ` Phillip Wood
2025-03-26 10:34       ` Junio C Hamano
2025-03-25 23:32   ` [PATCH v2 08/10] compat/regex: " Johannes Schindelin via GitGitGadget
2025-03-26 20:35     ` Taylor Blau
2025-03-27 10:29       ` Johannes Schindelin
2025-03-27 21:51         ` Taylor Blau
2025-03-25 23:32   ` [PATCH v2 09/10] clang: warn when the comma operator is used Johannes Schindelin via GitGitGadget
2025-03-26  5:54     ` Patrick Steinhardt
2025-03-26  7:50       ` Johannes Schindelin
2025-03-26  8:33         ` Patrick Steinhardt
2025-03-27 10:18           ` Johannes Schindelin
2025-03-25 23:32   ` [PATCH v2 10/10] detect-compiler: detect clang even if it found CUDA Johannes Schindelin via GitGitGadget
2025-03-26 17:41     ` Jeff King
2025-03-26 18:07       ` Eric Sunshine
2025-03-27  5:14         ` Jeff King
2025-03-27  5:21           ` Eric Sunshine
2025-03-27  6:35             ` Jeff King
2025-03-26  5:54   ` [PATCH v2 00/10] Avoid the comma operator Patrick Steinhardt
2025-03-26 17:50   ` Jeff King
2025-03-27 11:52   ` [PATCH v3 " Johannes Schindelin via GitGitGadget
2025-03-27 11:52     ` [PATCH v3 01/10] remote-curl: avoid using the comma operator unnecessarily Johannes Schindelin via GitGitGadget
2025-03-27 11:52     ` [PATCH v3 02/10] rebase: " Johannes Schindelin via GitGitGadget
2025-03-27 11:52     ` [PATCH v3 03/10] kwset: " Johannes Schindelin via GitGitGadget
2025-03-27 11:52     ` [PATCH v3 04/10] clar: " Johannes Schindelin via GitGitGadget
2025-03-27 11:52     ` [PATCH v3 05/10] xdiff: " Johannes Schindelin via GitGitGadget
2025-03-27 11:52     ` [PATCH v3 06/10] diff-delta: avoid using the comma operator Johannes Schindelin via GitGitGadget
2025-03-27 11:53     ` [PATCH v3 07/10] wildmatch: avoid using of " Johannes Schindelin via GitGitGadget
2025-03-27 11:53     ` [PATCH v3 08/10] compat/regex: explicitly mark intentional use " Johannes Schindelin via GitGitGadget
2025-03-27 11:53     ` [PATCH v3 09/10] clang: warn when the comma operator is used Johannes Schindelin via GitGitGadget
2025-03-27 11:53     ` [PATCH v3 10/10] detect-compiler: detect clang even if it found CUDA Johannes Schindelin via GitGitGadget
2025-03-27 15:07     ` [PATCH v3 00/10] Avoid the comma operator Phillip Wood
2025-03-29  0:39       ` Junio C Hamano

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=050f5d65-32eb-fd26-1461-579b06018dc6@gmx.de \
    --to=johannes.schindelin@gmx.de \
    --cc=git@vger.kernel.org \
    --cc=gitgitgadget@gmail.com \
    --cc=karthik.188@gmail.com \
    --cc=peff@peff.net \
    --cc=philipoakley@iee.email \
    --cc=phillip.wood123@gmail.com \
    --cc=ps@pks.im \
    /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).