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

Re: [PATCH] Use ^=1 to toggle between 0 and 1

From
PWPhillip Wood <phillip.wood123@gmail.com>
Date
Dec 18, 2023, 16:18 UTC
Message-ID
<ff4a6abc-8abf-4dbf-a787-e4895a78b048@gmail.com>
In-Reply-To
<xmqqo7erl7er.fsf@gitster.g>
Hi Junio
On 15/12/2023 17:09, Junio C Hamano wrote:
Show 15 quoted lines
> Phillip Wood <phillip.wood123@gmail.com> writes:
> 
>> Even if it unlikely that we would directly compare a boolean variable
>> to "true" or "false" it is certainly conceivable that we'd compare two
>> boolean variables directly. For the integer fallback to be safe we'd
>> need to write
>>
>> 	if (!cond_a == !cond_b)
>>
>> rather than
>>
>> 	if (cond_a == cond_b)
> 
> Eek, it defeats the benefit of using true Boolean type if we had to
> train ourselves to write the former, doesn't it?

Yes, it's horrible - if for some reason it turns out that we cannot use "#include <stdbool.h>" everywhere I think we should drop it rather than providing a subtly incompatible fallback

Show 25 quoted lines
>> A weather-balloon seems like the safest route forward. We have been
>> requiring C99 for two years now [1], hopefully there aren't any
>> compilers out that claim to support C99 but don't provide
>> "<stdbool.h>" (I did check online and the compiler on NonStop does
>> support _Bool).
>>
>> Best Wishes
>>
>> Phillip
>>
>> [1] 7bc341e21b (git-compat-util: add a test balloon for C99 support,
>> 2021-12-01)
> 
> Nice to be reminded of this one.
> 
> The cited commit does not start to use any specific feature from
> C99, other than that we now require that the compiler claims C99
> conformance by __STDC_VERSION__ set appropriately.  The commit log
> message says C99 "provides a variety of useful features, including
> ..., many of which we already use.", which implies that our wish was
> to officially allow any and all features in C99 to be used in our
> codebase after a successful flight of this test balloon.
> 
> Now, I think we saw a successful flight of this test balloon by now.
> Is allowing all the C99 the next step we really want to take?
 >
Show 7 quoted lines
> I still personally have an aversion against decl-after-statement and
> //-comments, not due to portability reasons at all, but because I
> find that the code is easier to read without it. But in principle,
> it is powerful to be able to say "OK, as long as the feature is in
> C99 you can use it", instead of having to decide on individual
> features, and I am not fundamentally against going that route if it
> is where people want to go.

I'm not sure we necessarily want to say "use anything that is in C99" for several reasons.

  - Some features such as C99's variable length arrays are known to be
    problematic.
  - As you say above there maybe features that we think harm the
    readability of our code.
  - As René points out not all compilers necessarily support all
    features.

I think using _Bool could be useful for the reasons Peff outlined. As for other features I've written code that I think would have benefited from compound literals, but off the top of my head I can't think of any other C99 features that I personally wish we were using. I think that decl-after-statement is occasionally useful to declare a variable near where it is used in a long function body but it is much simpler just to ban it altogether and encourage people to break up long functions to make them more readable.

Best Wishes
Phillip
> Thanks.
> 
> 
Previous: René ScharfeNext: René Scharfe
Message 12 of 26 in “Use ^=1 to toggle between 0 and 1”
  1. Use ^=1 to toggle between 0 and 1AtariDreams via GitGitGadget, Dec 12, 2023
  2. Dragan SimicDec 12, 2023
  3. Jeff KingDec 12, 2023
  4. René ScharfeDec 12, 2023
  5. Jeff KingDec 13, 2023
  6. Junio C HamanoDec 13, 2023
  7. René ScharfeDec 14, 2023
  8. Jeff KingDec 14, 2023
  9. Phillip WoodDec 15, 2023
  10. Junio C HamanoDec 15, 2023
  11. René ScharfeDec 16, 2023
  12. Phillip WoodDec 18, 2023
  13. git-compat-util: convert skip_{prefix,suffix}{,_mem} to boolRené Scharfe, Dec 16, 2023
  14. Phillip WoodDec 18, 2023
  15. Junio C HamanoDec 18, 2023
  16. René ScharfeDec 19, 2023
  17. Jeff KingDec 21, 2023
  18. Jeff KingDec 21, 2023
  19. phillip.wood123@gmail.comDec 21, 2023
  20. Use ^=1 to toggle between 0 and 1AreaZR via GitGitGadget, Dec 18, 2024
  21. git: use ^=1 to toggle between 0 and 1AreaZR via GitGitGadget, Dec 18, 2024
  22. git: use ^=1 to toggle between 0 and 1AreaZR via GitGitGadget, Dec 18, 2024
  23. git: use ^=1 to toggle between 0 and 1AreaZR via GitGitGadget, Dec 18, 2024
  24. git: use logical-not operator to toggle between 0 and 1AreaZR via GitGitGadget, Dec 18, 2024
  25. Junio C HamanoDec 19, 2024
  26. Junio C HamanoDec 18, 2024

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.