From: Junio C Hamano <gitster@pobox.com>
To: Stefan Beller <sbeller@google.com>
Cc: git@vger.kernel.org
Subject: Re: [PATCH 02/15] read-cache: Improve readability
Date: Fri, 20 Mar 2015 21:19:36 -0700 [thread overview]
Message-ID: <xmqqbnjnaso7.fsf@gitster.dls.corp.google.com> (raw)
In-Reply-To: <1426897692-18322-3-git-send-email-sbeller@google.com> (Stefan Beller's message of "Fri, 20 Mar 2015 17:27:59 -0700")
Stefan Beller <sbeller@google.com> writes:
> Signed-off-by: Stefan Beller <sbeller@google.com>
> ---
> read-cache.c | 4 +---
> 1 file changed, 1 insertion(+), 3 deletions(-)
>
> diff --git a/read-cache.c b/read-cache.c
> index f72ea9f..769897e 100644
> --- a/read-cache.c
> +++ b/read-cache.c
> @@ -703,9 +703,7 @@ int add_to_index(struct index_state *istate, const char *path, struct stat *st,
> !hashcmp(alias->sha1, ce->sha1) &&
> ce->ce_mode == alias->ce_mode);
>
> - if (pretend)
> - ;
> - else if (add_index_entry(istate, ce, add_option))
> + if (!pretend && add_index_entry(istate, ce, add_option))
> return error("unable to add %s to index",path);
> if (verbose && !was_same)
> printf("add '%s'\n", path);
I have a moderately strong feeling against this change, as the code
was done this way quite deliberately to keep it readable, namely, to
avoid using && to concatenate two boolean expressions that are in
totally different class inside condition part of if/while, where A
is a precondition guard for B (i.e. you cannot evaluate B unless A
holds) and B is called primarily for its side effect. The problem
is that, once you start liberally doing
if (A && B && C && D ...)
with booleans with mixed semantics (guards and actions), it will
quickly get harder to tell which one is which.
I could have written it as
if (!pretend) {
if (add_index_entry(...))
return error(...);
}
and that would have been just as readable as the original; it
clearly separates the guard (i.e. only do the add-index thing when
we are not pretending) and the operation that is done for the side
effect.
But I find the original tells you "if pretend mode, do *nothing*"
and "otherwise, try add_index_entry() and act on its error" very
clearly. Of course, I am biased as the original is my code from
38ed1d89 ("git-add -n -u" should not add but just report,
2008-05-21).
FYI, between preference and taste, I'd say this one is much closer
to the latter than the former.
By the way, aren't we leaking ce when we are merely pretending?
next prev parent reply other threads:[~2015-03-21 4:19 UTC|newest]
Thread overview: 44+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-03-21 0:27 [PATCH 00/15] Fixing memory leaks Stefan Beller
2015-03-21 0:27 ` [PATCH 01/15] read-cache: fix memleak Stefan Beller
2015-03-21 3:26 ` Junio C Hamano
2015-03-23 16:24 ` Stefan Beller
2015-03-23 17:57 ` [PATCH 1/2] " Stefan Beller
2015-03-23 17:57 ` [PATCH 2/2] read-cache.c: fix a memleak in add_to_index Stefan Beller
2015-03-23 18:07 ` Junio C Hamano
2015-03-23 18:11 ` [PATCH 1/2] read-cache: fix memleak Junio C Hamano
2015-03-21 0:27 ` [PATCH 02/15] read-cache: Improve readability Stefan Beller
2015-03-21 4:19 ` Junio C Hamano [this message]
2015-03-21 5:11 ` Stefan Beller
2015-03-22 19:26 ` Junio C Hamano
2015-03-21 0:28 ` [PATCH 03/15] read-cache: free cache entry in add_to_index in case of early return Stefan Beller
2015-03-21 3:31 ` Junio C Hamano
2015-03-21 5:10 ` Stefan Beller
2015-03-22 19:11 ` Junio C Hamano
2015-03-21 0:28 ` [PATCH 04/15] update-index: fix a memleak Stefan Beller
2015-03-21 3:40 ` Junio C Hamano
2015-03-23 16:53 ` [PATCH] update-index: Don't copy memory around Stefan Beller
2015-03-23 17:11 ` Junio C Hamano
2015-03-21 0:28 ` [PATCH 05/15] builtin/apply.c: fix a memleak Stefan Beller
2015-03-21 3:45 ` Junio C Hamano
2015-03-23 17:13 ` [PATCH] builtin/apply.c: fix a memleak (Fixup, squashable) Stefan Beller
2015-03-23 17:27 ` Junio C Hamano
2015-03-21 0:28 ` [PATCH 06/15] merge-blobs.c: Fix a memleak Stefan Beller
2015-03-21 0:28 ` [PATCH 07/15] merge-recursive: fix memleaks Stefan Beller
2015-03-21 3:48 ` Junio C Hamano
2015-03-21 0:28 ` [PATCH 08/15] http-push: Remove unneeded cleanup Stefan Beller
2015-03-21 0:28 ` [PATCH 09/15] http: release the memory of a http pack request as well Stefan Beller
2015-03-22 19:36 ` Junio C Hamano
2015-03-24 16:54 ` Stefan Beller
2015-03-24 17:48 ` Junio C Hamano
2015-03-21 0:28 ` [PATCH 10/15] commit.c: fix a memory leak Stefan Beller
2015-03-21 3:59 ` Junio C Hamano
2015-03-24 13:42 ` Duy Nguyen
2015-03-24 21:17 ` Re* " Junio C Hamano
2015-03-24 21:20 ` Stefan Beller
2015-03-21 0:28 ` [PATCH 11/15] builtin/check-attr: fix a memleak Stefan Beller
2015-03-21 4:02 ` Junio C Hamano
2015-03-21 0:28 ` [PATCH 12/15] builtin/merge-base: fix memleak Stefan Beller
2015-03-21 0:28 ` [PATCH 13/15] builtin/unpack-file: fix a memleak Stefan Beller
2015-03-21 0:28 ` [PATCH 14/15] builtin/cat-file: free memleak Stefan Beller
2015-03-21 0:28 ` [PATCH 15/15] ls-files: fix a memleak Stefan Beller
2015-03-21 3:21 ` [PATCH 00/15] Fixing memory leaks 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=xmqqbnjnaso7.fsf@gitster.dls.corp.google.com \
--to=gitster@pobox.com \
--cc=git@vger.kernel.org \
--cc=sbeller@google.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.