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

Re: [PATCH 2/2] diff-filter: be more careful when looking for negative bits

From
Junio C Hamano <gitster@pobox.com>
Date
Jan 26, 2022, 23:36 UTC
Message-ID
<xmqqtudqnk7y.fsf@gitster.g>
In-Reply-To
<YfHRTrdwZVwxcPBK@nand.local>
Taylor Blau <me@ttaylorr.com> writes:
Show 15 quoted lines
>> +		int has_positive = 0;
>> +		int has_negative = 0;
>> +
>> +		for (i = 0; (optch = optarg[i]) != '\0'; i++) {
>> +			if (optch < 'a' || 'z' < optch)
>> +				has_positive++;
>> +			else
>> +				has_negative++;
>> +		}
>> +
>> +		if (!has_positive && has_negative) {
>
> (One small nit that I absolutely do not care about is using a variable
> that starts with 'has_'--which I would expect to be a boolean--to count
> the number of positive/negative filters. Perhaps calling these

These variables are indeed used in the boolean sense (see how they are used in the condition in the "if" statement).

If we wanted to short-circuit, we could do this:
	for (i = 0;
	     !has_positive && !has_negative &&
	     (optch = optarg[i]) != '\0';
	     i++) {
		if (isupper(optch))
			has_negative++;
		else
			has_positive++;
	}

and thanks to their names, nobody would be confused to find it is a bug that these do not count when the loop exits early. We shouldn't name these positive_nr or negative_nr because we are not interested in counting them.

It is just that "bool++" is more idiomatic than repeated assignment "bool = 1" when setter and getter both know it is merely used as a Boolean, and that is why they named as has_X, which clearly is a name for a Boolean, not a counter.

Having said that, I do not mind if an assignment is used instead of post-increment in new code. I just think it is waste of time to go find increments that toggle a Boolean to true and changing them to assignments, and it is even more waste having to review such a code churn, so let's not go there ;-)

But as I wrote in the big NEEDSWORK comment, this loop should disappear when the code is properly rewritten to correct the interaction between two or more "--diff-filter=" options, so it would not matter too much.

Thanks.
Previous: Taylor BlauNext: Johannes Schindelin
Message 9 of 21 in “Fix two --diff-filter bugs”
  1. 0/2 Fix two --diff-filter bugsJohannes Schindelin via GitGitGadget, Jan 25, 2022
  2. 1/2 docs(diff): lose incorrect claim about `diff-files --diff-filter=A`Johannes Schindelin via GitGitGadget, Jan 25, 2022
  3. Junio C HamanoJan 26, 2022
  4. 2/2 diff-filter: be more careful when looking for negative bitsJohannes Schindelin via GitGitGadget, Jan 25, 2022
  5. Taylor BlauJan 25, 2022
  6. Johannes SchindelinJan 28, 2022
  7. Junio C HamanoJan 26, 2022
  8. Taylor BlauJan 26, 2022
  9. Junio C HamanoJan 26, 2022
  10. Johannes SchindelinJan 28, 2022
  11. Ævar Arnfjörð BjarmasonJan 26, 2022
  12. 0/3 Fix two --diff-filter bugsJohannes Schindelin via GitGitGadget, Jan 27, 2022
  13. 1/3 docs(diff): lose incorrect claim about `diff-files --diff-filter=A`Johannes Schindelin via GitGitGadget, Jan 27, 2022
  14. 2/3 diff.c: move the diff filter bits definitions up a bitJohannes Schindelin via GitGitGadget, Jan 27, 2022
  15. 3/3 diff-filter: be more careful when looking for negative bitsJohannes Schindelin via GitGitGadget, Jan 27, 2022
  16. Junio C HamanoJan 28, 2022
  17. Johannes SchindelinJan 28, 2022
  18. 0/3 Fix two --diff-filter bugsJohannes Schindelin via GitGitGadget, Jan 28, 2022
  19. 1/3 docs(diff): lose incorrect claim about `diff-files --diff-filter=A`Johannes Schindelin via GitGitGadget, Jan 28, 2022
  20. 2/3 diff.c: move the diff filter bits definitions up a bitJohannes Schindelin via GitGitGadget, Jan 28, 2022
  21. 3/3 diff-filter: be more careful when looking for negative bitsJohannes Schindelin via GitGitGadget, Jan 28, 2022

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.