Git development
 help / color / mirror / Atom feed
From: Jeremiah Mahler <jmmahler@gmail.com>
To: Eric Sunshine <sunshine@sunshineco.com>
Cc: git@vger.kernel.org
Subject: Re: [PATCH/RFC v1 4/5] fast-import.c: cleanup using strbuf_set operations
Date: Mon, 9 Jun 2014 15:00:39 -0700	[thread overview]
Message-ID: <20140609220039.GB18783@hudson.localdomain> (raw)
In-Reply-To: <CAPig+cRPPN7N+KUHULXxoGHetN8WPtASxnC7L1fe5rFoBFQ4KA@mail.gmail.com>

Eric,

On Mon, Jun 09, 2014 at 06:12:12AM -0400, Eric Sunshine wrote:
> On Mon, Jun 9, 2014 at 4:36 AM, Jeremiah Mahler <jmmahler@gmail.com> wrote:
> > Subject: fast-import.c: cleanup using strbuf_set operations
> 
...
> 
> > Signed-off-by: Jeremiah Mahler <jmmahler@gmail.com>
> > ---
> >  fast-import.c | 19 ++++++-------------
> >  1 file changed, 6 insertions(+), 13 deletions(-)
> >
> > diff --git a/fast-import.c b/fast-import.c
> > index e8ec34d..c23935c 100644
> > --- a/fast-import.c
> > +++ b/fast-import.c
> > @@ -2741,8 +2741,7 @@ static void parse_new_commit(void)
> >         hashcpy(b->branch_tree.versions[0].sha1,
> >                 b->branch_tree.versions[1].sha1);
> >
> > -       strbuf_reset(&new_data);
> > -       strbuf_addf(&new_data, "tree %s\n",
> > +       strbuf_setf(&new_data, "tree %s\n",
> >                 sha1_to_hex(b->branch_tree.versions[1].sha1));
> >         if (!is_null_sha1(b->sha1))
> >                 strbuf_addf(&new_data, "parent %s\n", sha1_to_hex(b->sha1));
> 
> Unlike the cases in patches 3/5 and 5/5 where the strbuf is used or
> returned immediately following the strbuf_set() call, I am not
> convinced that this change is an improvement. This code has the
> general form:
> 
>     strbuf_reset(...);
>     strbuf_add(...);
>     if (condition)
>         strbuf_add(...);
>     strbuf_add(...);
> 
> in which it is clear that the string is being built piecemeal, and
> it's easy for a programmer to insert, remove, or re-order strbuf_add()
> calls.
> 
> Replacing the first two lines with strbuf_set() somewhat obscures the
> fact that the string is going to be built up piecemeal. Plus, the
> change makes it more difficult to insert, remove, or re-order the
> strbuf_add() invocations.
> 
> This isn't a strong objection, but the benefit of the change seems
> minimal or non-existent.
> 
> Ditto for several remaining cases in this patch.
> 
...

This is a great observation that I certainly did overlook.  Using
strbuf_add or strbuf_set to help make it more obvious what the code is
doing.

By the same token, strbuf_set can be used to replace strbuf_add to make
it clear that nothing important was being added to and that the entire
buffer is being replaced.

  struct strbuf mybuf = STRBUF_INIT;

  strbuf_add(&mybuf, ...);  /* Was something there before? */

  strbuf_set(&mybuf, ...);  /* Replace everything. */

-- 
Jeremiah Mahler
jmmahler@gmail.com
http://github.com/jmahler

  reply	other threads:[~2014-06-09 22:00 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-06-09  8:36 [PATCH/RFC v1 0/5] add strbuf_set operations Jeremiah Mahler
2014-06-09  8:36 ` [PATCH/RFC v1 1/5] " Jeremiah Mahler
2014-06-09  8:36 ` [PATCH/RFC v1 2/5] add strbuf_set operations documentation Jeremiah Mahler
2014-06-09  9:53   ` Eric Sunshine
2014-06-09 21:49     ` Jeremiah Mahler
2014-06-09  8:36 ` [PATCH/RFC v1 3/5] sha1_name.c: cleanup using strbuf_set operations Jeremiah Mahler
2014-06-09  8:36 ` [PATCH/RFC v1 4/5] fast-import.c: " Jeremiah Mahler
2014-06-09 10:12   ` Eric Sunshine
2014-06-09 22:00     ` Jeremiah Mahler [this message]
2014-06-09  8:36 ` [PATCH/RFC v1 5/5] builtin/remote.c: " Jeremiah Mahler
2014-06-09 10:39 ` [PATCH/RFC v1 0/5] add " Duy Nguyen
2014-06-09 22:06   ` Jeremiah Mahler

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=20140609220039.GB18783@hudson.localdomain \
    --to=jmmahler@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=sunshine@sunshineco.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