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

Re: [PATCH v2] cache_tree_find(): remove redundant checks

From
Junio C Hamano <gitster@pobox.com>
Date
Mar 4, 2014, 23:18 UTC
Message-ID
<xmqqr46himd1.fsf@gitster.dls.corp.google.com>
In-Reply-To
<531652B9.4000001@alum.mit.edu>
Michael Haggerty <mhagger@alum.mit.edu> writes:
>> Isn't the above a strchrnul()?
>
> Oh, cool, I never realized that this GNU extension was blessed for use
> in Git.  Will change.

We do have our own fallbacks for non-glibc platforms, so it should be safe.

Show 11 quoted lines
>> Combining a freestanding decl with intializer assignment to lose one
>> line is sort of cheating on the line count, but replacing the three
>> lines with a single strchrnul() would be a real code reduction ;-)
>
> I suppose you are just teasing me, but for the record I consider line
> count only a secondary metric.  The reason for combining initialization
> with declaration is to reduce by one the number of times that the reader
> has to think about that variable when analyzing the code.
> ...
> I really wish we could mix declarations with statements because I think
> it is a big help to readability.
Unfortunately, I think we are in violent disagreement.

A variable declaration block with initializations on only some but not all variables is extremely annoying. If none of the variable declaration has initialization (or initialization to trivial values that do not depend on the logic flow), and the first statement is separated from the decl block, then I do not have to read the decl part when reading the code/logic *at all* (the compiler will find missing variables, variables declared as a wrong type, etc.).

In other words, a trivial initialization at the beginning of the block, if the logic flow only sometimes makes assignment to the variable, is perfectly fine, e.g.

	const char *string = NULL;
	if (...) {
        	string = ...
	}                
But I would wish people stop doing this:
	const char *string = strchrnul(name, ':');
	... the real logic of the block that uses string follows ...
and instead say
	const char *string;
	string = strchrnul(name, ':');
	... the real logic of the block that uses string follows ...
Previous: Michael HaggertyNext: Junio C Hamano
Message 7 of 11 in “cache_tree_find(): remove redundant checks”
  1. cache_tree_find(): remove redundant checksMichael Haggerty, Mar 4, 2014
  2. David KastrupMar 4, 2014
  3. Michael HaggertyMar 4, 2014
  4. David KastrupMar 4, 2014
  5. Junio C HamanoMar 4, 2014
  6. Michael HaggertyMar 4, 2014
  7. Junio C HamanoMar 4, 2014
  8. Junio C HamanoMar 5, 2014
  9. David KastrupMar 5, 2014
  10. Junio C HamanoMar 5, 2014
  11. [microproject idea]Junio C Hamano, Mar 4, 2014

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.