git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: Jeff King <peff@peff.net>
Cc: "Denton Liu" <liu.denton@gmail.com>,
	"Git Mailing List" <git@vger.kernel.org>,
	"Chris Torek" <chris.torek@gmail.com>,
	"Đoàn Trần Công Danh" <congdanhqx@gmail.com>
Subject: Re: [PATCH v3 1/3] remote-curl: use strlen() instead of magic numbers
Date: Tue, 23 Jun 2020 12:39:40 -0700	[thread overview]
Message-ID: <xmqqtuz1bmgz.fsf@gitster.c.googlers.com> (raw)
In-Reply-To: <20200623185458.GD1444619@coredump.intra.peff.net> (Jeff King's message of "Tue, 23 Jun 2020 14:54:58 -0400")

Jeff King <peff@peff.net> writes:

> On Tue, Jun 23, 2020 at 01:55:32PM -0400, Denton Liu wrote:
>
>> When we are dealing with a packet-line length header, we use the magic
>> literal `4`, representing the length of "0000" and "0001", the packet
>> line length headers. Use `strlen("000x")` so that we do not have to use
>> the magic literal.
>
> I'm not a huge fan of using strlen() for this, because it's _still_
> magical (you cannot change "0000" in one place without changing it in
> another"). And while it helps with understanding that "4" matches the
> length of that string, IMHO it's harder to read because now I have to
> make sure that those much longer strings all match up.

Yup.  There are two instances of recurring pattern with two memcpy,
where three copies of the same string must appear.  Unless the whole
thing is abstracted away so that these two instances are calls to
a macro/function that takes a single "0000" (and "0001"), I do not
think it is an improvement.

> This refactoring also implies to me that if you changed all of "0000" on
> one line you'd be fine, but that's emphatically not true. The magic
> number "4" is used to size the buffer earlier in the function, and would
> have to match (and of course since this is a network protocol, it's not
> like you could even change those in isolation).

Thanks.  That's even more important point.

> So I dunno. I kind of think the raw "4" is the most readable. It's quite
> obvious to me in the context of a memcpy() what's going on. I don't mind
> memcpy_literal() or similar that hides the repetition, but I think it's
> hard to do here because of the arithmetic on the destination.
>
> -Peff

  reply	other threads:[~2020-06-23 19:39 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-06-13 13:43 [PATCH] pkt-line: extract out PACKET_HEADER_SIZE Denton Liu
2020-06-13 14:23 ` Đoàn Trần Công Danh
2020-06-13 14:39   ` Denton Liu
2020-06-13 16:51   ` Junio C Hamano
2020-06-14 18:24     ` Junio C Hamano
2020-06-14  7:31 ` Denton Liu
2020-06-14  7:31 ` [PATCH v2 0/3] pkt-line: war on magical `4` literal Denton Liu
2020-06-14  7:31   ` [PATCH v2 1/3] remote-curl: use strlen() instead of magic numbers Denton Liu
2020-06-14  7:31   ` [PATCH v2 2/3] pkt-line: use string versions of functions Denton Liu
2020-06-14  8:31     ` Đoàn Trần Công Danh
2020-06-14  9:07       ` [PATCH v2] " Denton Liu
2020-06-14 21:35         ` Junio C Hamano
2020-06-14 22:28           ` Denton Liu
2020-06-15 12:32         ` Đoàn Trần Công Danh
2020-06-14  7:32   ` [PATCH v2 3/3] pkt-line: extract out PACKET_HEADER_SIZE Denton Liu
2020-06-14 21:32   ` [PATCH v2 0/3] pkt-line: war on magical `4` literal Junio C Hamano
2020-06-23 17:55   ` [PATCH v3 " Denton Liu
2020-06-23 17:55     ` [PATCH v3 1/3] remote-curl: use strlen() instead of magic numbers Denton Liu
2020-06-23 18:54       ` Jeff King
2020-06-23 19:39         ` Junio C Hamano [this message]
2020-06-23 17:55     ` [PATCH v3 2/3] pkt-line: use string versions of functions Denton Liu
2020-06-23 19:11       ` Jeff King
2020-06-23 17:55     ` [PATCH v3 3/3] pkt-line: extract out PACKET_HEADER_SIZE Denton Liu

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=xmqqtuz1bmgz.fsf@gitster.c.googlers.com \
    --to=gitster@pobox.com \
    --cc=chris.torek@gmail.com \
    --cc=congdanhqx@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=liu.denton@gmail.com \
    --cc=peff@peff.net \
    /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).