git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] Simplify the code and avoid an attribution.

From
Mike Hommey <mh@glandium.org>
Date
Nov 22, 2007, 20:01 UTC
Message-ID
<20071122200157.GC19675@glandium.org>
In-Reply-To
<b8bf37780711211700m1aa32006t6302f134f9c3fb7e@mail.gmail.com>
On Wed, Nov 21, 2007 at 11:00:02PM -0200, André Goddard Rosa wrote:
Show 28 quoted lines
> --- 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.

> -	if (f == NULL)
> +	if (!f)
> -	if ((f = fopen(filename, "rb")) == NULL)
> +	if (!(f = fopen(filename, "rb")))
It's a matter of taste
Mike
Previous: André Goddard RosaNext: Junio C Hamano
Message 2 of 3 in “Simplify the code and avoid an attribution.”
  1. Simplify the code and avoid an attribution.André Goddard Rosa, Nov 22, 2007
  2. Mike HommeyNov 22, 2007
  3. Junio C HamanoNov 22, 2007

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.