git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: Mike Hommey <mh@glandium.org>
Cc: "André Goddard Rosa" <andre.goddard@gmail.com>,
	"Git Mailing List" <git@vger.kernel.org>
Subject: Re: [PATCH] Simplify the code and avoid an attribution.
Date: Thu, 22 Nov 2007 12:29:11 -0800	[thread overview]
Message-ID: <7vk5oa3tyg.fsf@gitster.siamese.dyndns.org> (raw)
In-Reply-To: <20071122200157.GC19675@glandium.org> (Mike Hommey's message of "Thu, 22 Nov 2007 21:01:57 +0100")

Mike Hommey <mh@glandium.org> writes:

> On Wed, Nov 21, 2007 at 11:00:02PM -0200, André Goddard Rosa wrote:
>> --- a/config.c
>> +++ b/config.c
>> @@ -447,15 +447,16 @@ int git_config_from_file(config_fn_t fn, const
>> char *filename)
>>  	int ret;
>>  	FILE *f = fopen(filename, "r");
>> 
>> -	ret = -1;
>> -	if (f) {
>> -		config_file = f;
>> -		config_file_name = filename;
>> -		config_linenr = 1;
>> -		ret = git_parse_file(fn);
>> -		fclose(f);
>> -		config_file_name = NULL;
>> -	}
>> +	if (!f)
>> +		return -1;
>> +
>> +	config_file = f;
>> +	config_file_name = filename;
>> +	config_linenr = 1;
>> +	ret = git_parse_file(fn);
>> +	fclose(f);
>> +	config_file_name = NULL;
>> +
>>  	return ret;
>>  }
>
> Actually, since it is more likely that the file has been opened, the
> original code is more optimal because it doesn't generate a jump in most
> cases. And if you're worried about the ret variable, don't worry, it's
> most likely stripped out by the compiler optimizations.

I did not comment on this patch partly because I did not know
what "attribution" meant.  I think it is a good change from
readability, not micro-optimization, point of view.

If you structure your code this way,

	do this
        if (there is an error)
        	return error code;
	do that
	do the rest

it is much easier to read than

	do this
        set error code pessimistically
        if (it was successful) {
        	do that
                update error code
                do the rest
	}
        return error code

especially the more likely part that runs the real processing
("do that...do the rest") tends to grow over time, and even
acquire other error checks.

      reply	other threads:[~2007-11-22 20:29 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-11-22  1:00 [PATCH] Simplify the code and avoid an attribution André Goddard Rosa
2007-11-22 20:01 ` Mike Hommey
2007-11-22 20:29   ` Junio C Hamano [this message]

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=7vk5oa3tyg.fsf@gitster.siamese.dyndns.org \
    --to=gitster@pobox.com \
    --cc=andre.goddard@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=mh@glandium.org \
    /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).